泰安网络营销网站建设,wordpress纯静态化插件,品牌推广方案策划书,重庆网站网站建设Autowired注解能用在static属性吗#xff1f;
答案是否定的#xff0c;我们来测试下#xff1a;
日志信息已经很明确了#xff0c;表示static不能被Autowired进行复制。为什么呢#xff1f;我们现在就来一起了解其原因。
首先将我们的测试环境搭建好#xff0c;
Use…Autowired注解能用在static属性吗
答案是否定的我们来测试下
日志信息已经很明确了表示static不能被Autowired进行复制。为什么呢我们现在就来一起了解其原因。
首先将我们的测试环境搭建好
User1类
Componentpublic
class User1 { Autowired private static User2 user2; Autowired private User2 user3;
}User2类
Componentpublic
class User2 {}AppConfig类
ConfigurationComponentScan(staticDemo)
/*EnableAspectJAutoProxy*/
/*Import(MyImportBeanDefinitionRegistrar.class)*/
public class AppConfig {}Test类
public class Test { public static void main(String[] args) { AnnotationConfigApplicationContext annotationConfigApplicationContextnew AnnotationConfigApplicationContext(AppConfig.class); }
}然后将代码定位在依赖注入的入口
此时我们的beanName是User1类我们需要注入User1类然后我们进入这个方法
protected void populateBean(String beanName, RootBeanDefinition mbd, Nullable BeanWrapper bw) { if (bw null) { if (mbd.hasPropertyValues()) { throw new BeanCreationException( mbd.getResourceDescription(), beanName, Cannot apply property values to null instance); } else { // Skip property population phase for null instance. return; } } // Give any InstantiationAwareBeanPostProcessors the opportunity to modify the // state of the bean before properties are set. This can be used, for example, // to support styles of field injection. //Spring判断你是不是自己实现了InstantiationAwareBeanPostProcessor如果实现了而且返回false // Spring就不会继续执行了一般不会用到 if (!mbd.isSynthetic() hasInstantiationAwareBeanPostProcessors()) { for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp (InstantiationAwareBeanPostProcessor) bp; if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) { return; } } } } //判断是否已经对属性赋值 PropertyValues pvs (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null); //判断属性植入的模式默认NO int resolvedAutowireMode mbd.getResolvedAutowireMode(); if (resolvedAutowireMode AUTOWIRE_BY_NAME || resolvedAutowireMode AUTOWIRE_BY_TYPE) { MutablePropertyValues newPvs new MutablePropertyValues(pvs); // Add property values based on autowire by name if applicable. if (resolvedAutowireMode AUTOWIRE_BY_NAME) { autowireByName(beanName, mbd, bw, newPvs); } // Add property values based on autowire by type if applicable. if (resolvedAutowireMode AUTOWIRE_BY_TYPE) { autowireByType(beanName, mbd, bw, newPvs); } pvs newPvs; } boolean hasInstAwareBpps hasInstantiationAwareBeanPostProcessors(); boolean needsDepCheck (mbd.getDependencyCheck() ! AbstractBeanDefinition.DEPENDENCY_CHECK_NONE); PropertyDescriptor[] filteredPds null; if (hasInstAwareBpps) { if (pvs null) { pvs mbd.getPropertyValues(); } //这里才是通过后置处理器完成属性注入的工作 for (BeanPostProcessor bp : getBeanPostProcessors()) { if (bp instanceof InstantiationAwareBeanPostProcessor) { InstantiationAwareBeanPostProcessor ibp (InstantiationAwareBeanPostProcessor) bp; PropertyValues pvsToUse ibp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName); if (pvsToUse null) { if (filteredPds null) { filteredPds filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } pvsToUse ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName); if (pvsToUse null) { return; } } pvs pvsToUse; } } } if (needsDepCheck) { if (filteredPds null) { filteredPds filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching); } checkDependencies(beanName, mbd, filteredPds, pvs); } if (pvs ! null) { applyPropertyValues(beanName, mbd, bw, pvs); } }这里有两个for循环我们关注第二个for循环找到后置处理器是
然后点进去 这里第一行代码就是获取我们的数据信息并且用InjectionMetadata的方式进行封装获取InjectionMetadata后就可以通过反射的方式进行属性注入了这里在前面的Spring源码章节我们已经详细介绍了就不再叙述。我们进入这个findAutowiringMetadata方法瞧一瞧他是如何获取的 我们看到InjectionMetadata封装了哪些信息其中包含了我们的user3这个属性注意这个user3是没有static修饰的user2是被static修饰了的而这里确没有user2这个属性先记住这一点然后我们再看我们的metadata是通过injectionMetadataCache缓存获取的说明一个我们的static是在存放缓存的时候就将我们的static属性屏蔽掉接下来我们就验证我们的猜想。
Spring何时屏蔽static属性
我们将代码定位在AbstractAutowireCapableBeanFactory.doCreateBean方法 跟踪进去 再跟踪 可以看到已经回到了我们find方法点进去注意下面的红线表示我们这里是找的user1类的属性 然后这里我们关注的是injectionMetadataCache缓存 此时缓存里并没有user1的元信息我们再次跟进 首先看到这里ReflectionUtils.doWithLocalFields我们点进去
可以看到这里获取了两个我们的属性一个是静态属性一个是常态属性然后我们再回到buildAutowiringMetadata方法注意这里的filed是函数试变成它会回调这里的代码
我们就可以看到如果我们的属性是static那么Spring会直接抛弃
可以看到我们已经得到了验证Spring就是在这里抛弃了static的属性至于为什么要抛弃static属性呢笔者猜测这里需要回到JVM的一些知识我们都知道static是面向类级别而Spring通常是面向普通的单例对象如果我们将单例的对象中static属性赋值那么就会影响我们这个类的其他对象的值个人认为这是Spring不希望看到的这也是不科学的。