Spring 之@Value注解原理
2021-03-05 20:28
标签:depend beans link failed gmetad def poi actor throw @Value和@Autowired这两个注解都是由AutoWiredAnnotationBeanPostProcessor来处理的,这两个注解被处理的地方也是一样的,就是在一个bean被new出来之后,要填充属性的populateBean方法里。 会调用 AutoWiredAnnotationBeanPostProcessor.postProcessPropertyValues metadata.inject还是会遍历处理每一个 AutowiredFieldElement 或者 AutowiredMethodElement 继续 DefaultListableBeanFactory.resolveDependency 总结 如果一个bean里面的某个字段有注释@Value 注释的值如果是默认的占位符标志,就会调用 PropertyPlaceholderConfigurer 里的方法获取配置文件里的值 Spring 之@Value注解原理 标签:depend beans link failed gmetad def poi actor throw 原文地址:https://www.cnblogs.com/juniorMa/p/14317242.htmlpublic PropertyValues postProcessPropertyValues(
PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeanCreationException {
InjectionMetadata metadata = findAutowiringMetadata(beanName, bean.getClass(), pvs);
//findAutowiringMetadata表示找到该bean中所有的@Value和@Autowired注解,然后组成InjectionMetadatatry {
metadata.inject(bean, beanName, pvs);
}
catch (BeanCreationException ex) {
throw ex;
}
catch (Throwable ex) {
throw new BeanCreationException(beanName, "Injection of autowired dependencies failed", ex);
}
return pvs;
}
protected void inject(Object bean, String beanName, PropertyValues pvs) throws Throwable {
Field field = (Field) this.member;
Object value;
if (this.cached) {
value = resolvedCachedArgument(beanName, this.cachedFieldValue);
}
else {
DependencyDescriptor desc = new DependencyDescriptor(field, this.required);
desc.setContainingClass(bean.getClass());
Set
public Object resolveDependency(DependencyDescriptor descriptor, String requestingBeanName,
Set
public Object doResolveDependency(DependencyDescriptor descriptor, String beanName,
Set
public String resolveEmbeddedValue(String value) {
if (value == null) {
return null;
}
String result = value;
for (StringValueResolver resolver : this.embeddedValueResolvers) {//[org.springframework.beans.factory.config.PropertyPlaceholderConfigurer$PlaceholderResolvingStringValueResolver@17d88132]
result = resolver.resolveStringValue(result);//这里就取到了真实的值
if (result == null) {
return null;
}
}
return result;
}
@Component
public class CommonPo {
@Value("${driverClassName}")
private String driver;
文章标题:Spring 之@Value注解原理
文章链接:http://soscw.com/index.php/essay/60605.html