Spring 学习—— Bean装配(Bean 管理的注解)

一、Bean管理的注解

    1、Classpath 扫描与组件管理

    2、类的自动检测与注册 Bean

    3、<context:annotation-config/>

    4、@Component,@Repository,@Service,@Controller

    5、@Required

    6、@Autowired

    7、@Qualifier

    8、Resource    

二、Classpath 扫描与组件管理

    从 Spring3.0 开始,Spring javaConfig 项目提供了很多特性,包括使用 Java 定义 Bean

扫描二维码关注公众号,回复: 1669402 查看本文章

例如 @Configuration,@Bean,@Import,@DependsOn

    @Component 是一个通用注解,可以用于任何 Bean

    @Repository,@Service,@Controller是更具有针对性的注解

    1、@Repository 通常用于注解 DAO 类,即持久层。

    2、@Service 通常用于注解 Service 类,即服务类

    3、@Controller 通常用于 Controller 类,即控制层 

    

    4、自定义注解

    

三、<context:annotation-config/>

    1、<context:component-scan> 包含了 <context:annotation-config/>通常使用前者,不再使用后者。

     <context:component-scan>  会完成组件的扫描,即那些类进行了注解,并将这些类注册到 ICO 容器中,base-package 定义了类的限制。

    

    默认情况下,类被自动发现并注册到 Bean 的条件是:使用 @Component,@Repository,@Service,@Conntroller注解,或者使用 @Component 的自定义注解。

    可以通过过滤器修改上面的行为,例如下面 的例子 XML 忽略了所以的@Repositor 注解的类,并且包含 *Stub*Repositor的类

四、定义 Bean 

    扫描过程中组件被自动检测,那么 Bean 的名称是由 BeanNameGenerator 生成的          (@Component,@Repository,@Service,@Controller 都会有个 name 属性用于显式设置 Bean Name)

    

    

五、作用域

    通常情况下自动查找 Spring 组件,其中 Scope 是 Singleton,Spring 提供了一个注解 @Scope

    

    

六、代理

    

七、实例

    

    

    

    

    


猜你喜欢

转载自blog.csdn.net/qq_28043563/article/details/80740590