导入表格做地图中热力网站,活动推广方式都有哪些,工程项目外包平台,东吴钢结构网架公司1、为什么Autowired不能注入static成员属性
扫描Class类需要注入的元数据的时候#xff0c;直接选择忽略掉了static成员#xff08;包括属性和方法#xff09;
Spring 依赖注入是依赖set方法, set方法是普通的对象方法,static变量是类的属性
AutowiredAnnotationBeanPostP…1、为什么Autowired不能注入static成员属性
扫描Class类需要注入的元数据的时候直接选择忽略掉了static成员包括属性和方法
Spring 依赖注入是依赖set方法, set方法是普通的对象方法,static变量是类的属性
AutowiredAnnotationBeanPostProcessor// 构建Autowired注入元数据方法
// 简单的说就是找到该Class类下有哪些是需要做依赖注入的
private InjectionMetadata buildAutowiringMetadata(final Class? clazz) {...// 循环递归因为父类的也要管上do {// 遍历所有的字段包括静态字段ReflectionUtils.doWithLocalFields(targetClass, field - {if (Modifier.isStatic(field.getModifiers())) {logger.info(Autowired annotation is not supported on static fields: field);}return;...});// 遍历所有的方法包括静态方法ReflectionUtils.doWithLocalMethods(targetClass, method - {if (Modifier.isStatic(method.getModifiers())) {logger.info(Autowired annotation is not supported on static methods: method);}return;...});...targetClass targetClass.getSuperclass();} while (targetClass ! null targetClass ! Object.class);...
}
2、static方法里用Autowire或者Resource注入的属性 首先 类加Component注解使当前类成为bean 然后 定义statis类成员 然后 创建 init()方法用PostConstruct注解修饰 最后init()方法中把需要加载的类复制给static类
Component
public class DemoCode {Autowiredprivate DemoService demoService;public static DemoService demoServiceNew;/*** 解决 static方法调用 注入的service为null*/PostConstructpublic void init(){demoServiceNew demoService;}}3、处理string类型的json串中的反斜杠
导入commons-lang3的jar包 dependency groupIdorg.apache.commons/groupId artifactIdcommons-lang3/artifactId version3.8.1/version /dependency public static void main(String[] args) {String s{\\\displayName\\\:\\\\\\,\\\id\\\:1401524465412907010,\\\name\\\:\\\名称\\\,\\\source\\\:\\\\\\,\\\type\\\:\\\text\\\,\\\value\\\:\\\红细胞计数\\\};String tmp StringEscapeUtils.unescapeJava(s);System.out.println(tmp);}//输出结果
//{displayName:,id:1401524465412907010,name:名称,source:,type:text,value:红细胞计数}
4、对象拷贝的方法 import org.springframework.beans.BeanUtils; BeanUtils.copyProperties(原始对象, 新对象);