其他分享
首页 > 其他分享> > 每天进步一点点~注解篇02

每天进步一点点~注解篇02

作者:互联网

watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=

 

收藏从未停止,学习从未开始,

与其长篇大论,不如简短精悍。

每天进步一点点,目标距离缩小点。

每天进步一点点,成功就会在眼前。

 

本人水平和能力有限,文章如有不对或有需要修改的地方,还望各位指明纠正

 

 

2.@Autowired

@Autowired是一种注解,可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作,@Autowired标注可以放在成员变量上,也可以放在成员变量的set方法上,也可以放在任意方法上表示,自动执行当前方法,如果方法有参数,会在IOC容器中自动寻找同类型参数为其传值。

 

传统的开发中要实现我们的程序。需要这样来处理: 

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

例如:

<property name="属性名" value=" 属性值"/>    

通过这种方式来,配置比较繁琐,而且代码比较多。在Spring 2.5 引入了 @Autowired 注释来解决这个问题。

 

在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法

 

import org.springframework.beans.factory.annotation.Autowired;    

  public class Factory {    

    @Autowired   
    private Car car;    

    @Autowired   
    private Bus bus;  

    ...
}

 

经常见到的是我们会这样使用:

@Service
public class UserService {
  ...
}

@RestController
@PostMapping("/api/users")
public class UserController {

   @Autowired
   private UserService userService;
   ...
}

 

工作原理:

 

public class AutowiredAnnotationBeanPostProcessor extends InstantiationAwareBeanPostProcessorAdapter
        implements MergedBeanDefinitionPostProcessor, PriorityOrdered, BeanFactoryAware {

 

 

public interface MergedBeanDefinitionPostProcessor extends BeanPostProcessor {

    /**
     * Post-process the given merged bean definition for the specified bean.
     * @param beanDefinition the merged bean definition for the bean
     * @param beanType the actual type of the managed bean instance
     * @param beanName the name of the bean
     * @see AbstractAutowireCapableBeanFactory#applyMergedBeanDefinitionPostProcessors
     */
    void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);

    /**
     * A notification that the bean definition for the specified name has been reset,
     * and that this post-processor should clear any metadata for the affected bean.
     * <p>The default implementation is empty.
     * @param beanName the name of the bean
     * @since 5.1
     * @see DefaultListableBeanFactory#resetBeanDefinition
     */
    default void resetBeanDefinition(String beanName) {
    }

}

 

public class InjectionMetadata {

    private static final Log logger = LogFactory.getLog(InjectionMetadata.class);

    private final Class<?> targetClass;

    private final Collection<InjectedElement> injectedElements;

    @Nullable
    private volatile Set<InjectedElement> checkedElements;

    ...
}

 

@Autowired注解的作用是由AutowiredAnnotationBeanPostProcessor实现的,查看该类的源码会发现它实现了MergedBeanDefinitionPostProcessor接口,进而实现了接口中的postProcessMergedBeanDefinition方法,@Autowired注解正是通过这个方法实现注入类型的预解析,将需要依赖注入的属性信息封装到InjectionMetadata类中。

 

 

 

标签:02,Autowired,private,public,一点点,bean,param,注解,class
来源: https://blog.51cto.com/u_14602625/2963519