在IoC容器中装配Bean(基于注解配置)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liouyi250/article/details/79346934

一、 基于注释的配置

1.1 使用注释定义Bean
@Component("userDao")
public class UserDao{
    ...
}
//等同于
<bean id="userDao" class="com.smart.dao.UserDao"/>
<!--
以下3个功能基本和@Component等效的注解:
@Repository:用于对DAO实现类进行标注
@Service:用于对Service实现类进行标注
@Controller:用于对Controller实现类进行标注
-->
1.2 扫描注解定义的Bean

spring提供了一个context命名空间,他提供了通过扫描类包以医用注解定义bean的方式<xmlns:context="....">

<context:component-scan base-package="com.smart.dao" resource-pattern="anno/*.class"/>

<context:component-scan base-package="com.smart.dao">
    <context:include-filter type="regex" expression="com\.smart\.anno.*"/>
    <context:exclude-filter type="aspectj"
     expression="com.smart..*Controller"/>
 </context:component-scan>
<!--
resource-pattern属性可以按资源名称对基类包中的类进行过滤,<context:include-filter>表示要包含的目标类,<context:exclude-filter>表示要排除的目标类,他们支持多种类型的过滤表达式。一个<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>。<context:component-scan>的use-default-filters属性,其默认值为true,表示会对标注@Component,@Controller,@Service,@Repository的bean进行扫描。
-->
<context:component-scan base-package="com.smart.dao" use-default-filters="false">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

//上述代码可以仅扫描@Controller的Bean
1.3 自动装配Bean

1.3.1 使用@Autowired进行自动注入

@Service
public class LoginService{
    @Autowired(required=false)//@Autowired注释按类型注入,required属性使得spring即使找不到匹配的bean,不会抛出异常
    @Qualifier("logDao")//如果有两个以上的相同类型,@Qualifier注释限定Bean的名称
    private LogDao logDao;

    //自动将名为userDao的bean传给方法入参
    @Autowired
    public void init(@Qualifier("userDao") UserDao userDao,LogDao logDao){...}

    //Spring容器会将容器中所有类型为Plugin的Bean注入这个变量中
    @Autowired
    private List<Plugin> plugins;

    //将实现Plugin接口的Bean注入map集合中,4.0后新增
    @Autowired
    private Map<String,Plugin> pluginMaps;
}

1.3.2 对延迟依赖注入的支持

@Lazy
@Repository
public class LogDao{
    @Lazy
    @Autowired(required=false)
    public void setLogDao(LogDao logDao)
    {...
    }
}
//@Lazy注解必须同时标注在属性及目标Bean上,否则延迟注入无效。
1.4 Bean作用范围及声明过程方法
//指定Bean的作用范围为prototype
@Scope("prototype")
@Component
public class Car{
//相当于xml配置中的init-method属性
    @PostConstruct
    private void init1(){...}

    //相当于xml配置中destory-method属性
    @PreDestroy
    private void destory1(){...}
}

省略基于Java类和Groovy DSL配置。

二、基于XML和注解配置的比较

基于XML配置 基于注解配置
适用场景 (1)bean的定义来源域第三方类库,如DataSource、JdbcTemplate等,因无法在类中标注注解,所用通过XML配置方式较好。
(2)命名空间的配置,如aop、context等,只能基于XML配置。
Bean的实现类是当前项目开发的,可以直接在Java类中使用基于注解的配置
Bean的定义 在XML元素中通过<bean>元素定义Bean,如:<bean class=”com.smart.simple.Car”> 在Bean实现类处通过标注@Component或衍型类(@Repository、@Service、@Controller)定义类
Bean的名称 通过<bean>的id或name属性定义,<bean i=”car” class=”com.smart.simple.Car” / >
默认名称为com.smart.simple.Car#0
通过注解的value属性定义,如@Component(“userDao”)。默认名称为小写字母开头的类名(不带包名)userDao。
Bean注入 通过<property>子元素或通过p命名空间的动态属性。如p:userDao-ref=”userDao”进行注入。 通过在成员变更或方法入参处标注@Autowired,按类型匹配自动注入,还可以配合使用@Qualifier按名称匹配方式注入。
Bean声明过程方法 通过<bean>的init-method和destory-method属性指定Bean的实现类的方法名,最多只能指定一个初始化方法和一个销毁方法。 通过在目标方法上标注@PostContruct和@PreDestroy注解指定初始化和销毁方法,可以定义任意多个。
Bean的作用范围 通过<bean>的scope属性指定。<bean i=”car” class=”com.smart.simple.Car” scope=”prototype” / > 通过在类定义处标注@Scope指定,如@Scope(“prototype”)
Bean的延迟初始化 通过<bean>的lazy-init属性指定,默认为default,继承于<beans>的default-lazy-init设置,该值默认为false 通过在类定义处标注@Lazy指定,如@Lazy(true)

猜你喜欢

转载自blog.csdn.net/liouyi250/article/details/79346934