Spring学习(三)Spring Bean装配(常用注解)

注册与管理Bean
=======================================
·从 Spring3.0开始, Spring Java Config项目提供了很多特性包括使用ava而不是XML定义bean,比如
@ Configuration, @Bean, @Import, @Dependson
·@ Componenti是一个通用注解,可用于任何bean
·@ Repository,@ Service,@ Controller是更有针对性的注解
-@ Repository通常用于注解DAO类,即持久层
-@ Servicei通常用于注解 Service类,即服务层
-@ Controller通常用于 Controller类,即控制层(MVC)

元注解(Meta-annotations)

======================================
·许多 Spring提供的注解可以作为自己的代码,即“元数据注解元注解是一个简单的注解,可以应用到另ー个注解
·

类的自动检测及Bean的注册

=====================================
·Spring可以自动检测类并注册Bean到 Application Context中
如:@Service @Repository @Autovired

<context:annotation-config/>

·通过在基于XML的 Spring配置如下标签(请注意包含上下文命名空间)
·< context: annotation- config/>仅会查找在同一个application Context中的bean注解

<beans xmlns="...">
    <context:annotation-config/>
</beans>
<beans xmlns="...">
    <context:component-scan base-package="com.demo"/>
</beans>

·< context:: component-scan>包含< context: annotationconfig>,通常在使用前者后,不用再使用后者
· AutowiredannotationBeanPostprocessor和Commonannotation Bean Postprocessor也会被包含进来


使用过滤器进行自定义扫描

=================================
·默认情况下,类被自动发现并注册bean的条件是:使用@Component, @Repository, @Service, @Controller 注解或者使用 @Component的自定义注解
·可以通过过器修改上面的行为,如:下面例子的XML配置忽略所有的@ Repositoryi注解并用"Stub"代替

<beans>
    <context:component-scan base-package="com.demo">
        <context:include-filter type="regex"             expression=".*Stub.*Repository"/>
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
    </context:component-scan>
</beans>        

定义Bean

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

1 @Service("Mydemo")
2 public class Demo{}
3 //在不使用自定义BeanID时,会自动类第一个字母小写当ID demo
4 @Service
5 public class Demo{}

·可自定义bean命名策略,实现 Beannamegenerator接口并一定要包含一个无参数构造函器

<beans>
    <context:component-scan base-package="com.demo" name-generator="com.demo.MyDemo" />
</beans>

作用域(Scope)

==================================

·通常情况下自动查找的 Spring组件,其 scope是 E singletonSpring2.5提供了ー个标识 scope的注解@ Scope

1 @Scope("prototype")
2 @Repository
3 public class Demo{}

·也可以自定义 scope策陥,实现 Scopemetadata Resolverf接口并提供一个无参构造器

<beans>
    <context:component-scan base-package="com.demo" scope-resolver="com.demo.Mydemo" />
</beans>

代理方式

=====================================
可以使用 scoped-proxy属性指定代理,有三个值可选:no, interfaces, targetClass

<beans>
    <context:component-scan base-package="com.demo" scope-proxy="interfaces" />
</beans>

@Required

=====================================
·@ Required注解适用于bean属性的 setter方法

·这个注解仅仅表示,受影响的bean属性必须在配置时被填充通过在bean定义就通过自动装配一个明确的属性值

public class Demo{
     private Lizhi lizhi;

    @Required
    public void setLizhi(Lizhi lizhi){
        this.lizhi = lizhi;
    }
}

@ Autowired

======================

·可以将@ Autowiredi注解为“传统”的 setter方法

1 public class Demo{
2      private Lizhi lizhi;
3 
4     @Required
5     public void setLizhi(Lizhi lizhi){
6         this.lizhi = lizhi;
7     }
8 }

·可用于构造器或成员变量

1 @Autovired
2 private Lizhi lizhi;
3 private LizhiDao lizhiDao;
4 @Autovired
5 public LizhiFan(LizhiDao lizhiDao){
6     this.lizhiDao = lizhiDao;
7 }

·默认情况下,如果因找不到合适的bean将会导致 autowiring失败抛出异常,可以通过下面的方式避兔

1 public class SimpleDemo{
2     private Demo demo;
3     
4     @Autowired(required=false)
5     public void setDemo(Demo demo){
6         this.demo = demo;
7     }
8 }

·每个类只能有一个构道器被标记为 required=true

· @Autowired的必要属性,建议使用@Required注解

·可以使用@Autowiredi注解那些众所周知的解析依赖性接口,比如: Beanfactory, Applicationcontext, Environment,Resourceloader, Applicationeventpublisher, andMessagesource

猜你喜欢

转载自www.cnblogs.com/ic710/p/11027169.html