1. 程式人生 > >在IoC容器中裝配Bean(基於註解配置)

在IoC容器中裝配Bean(基於註解配置)

一、 基於註釋的配置

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)