Spring筆記——零配置(註解)

1.搜索Bean類

Spring通過使用一些特殊的Annotation來標註Bean類,Spring提供瞭如下幾個Annotation來標註Spring Bean:
@Component:標註一個普通的Spring Bean類
@Controller:標註一個控制器組件類
@Service:標註一個業務邏輯組件類
@Repository:標註一個DAO組件類

如果我們需要定義一個普通的Spring Bean,則直接使用@Component標註即可。但如果用@Repository、@Controller或@Service來標註這些Bean類,這些Bean類將被作爲特殊的Java EE組件對待,也許能更好的被工具處理,或與切面進行關聯。@Repository、@Controller和@Service也許還能攜帶更多的語義,因此,如果需要在Java EE應用中使用這些標註時,儘量考慮使用@Repository、@Controller和@Service來代替通用的@Component標註。
指定了某些類可作爲Spring Bean類使用後,最後還需要讓Spring搜索指定路徑,此時需要在Spring配置文件中導入context Schema,並指定一個簡單的搜索路徑。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 自動掃描指定包及其子包下的所有Bean類 -->
    <context:component-scan base-package="main.java.service.impl" />
</beans>

在這種基於Annotation的方式下,Spring採用約定的方式來爲這些Bean實例指定名稱,這些Bean實例的名稱默認是Bean類的首字母小寫,其他部分不變。

除此之外,我們還可以通過爲

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    <!-- 自動掃描指定包及其子包下的所有Bean類 -->
    <context:component-scan base-package="main.java.service.impl">
        <!-- 只包含以Axe結尾的類 -->
        <context:include-filter type="regex" expression=".*Axe" />
        <!-- 但是StoneAxe類不包含其中 -->
        <context:exclude-filter type="assignable" expression="main.java.service.impl.StoneAxe"/>
    </context:component-scan>
</beans>

2.指定Bean的作用域

當使用註解方式來管理Bean類時,可使用@Scope來標註Bean的作用域,只要在該Annotation中提供作用域的名稱即可。例如,我們可以定義如下Java類:

@Scope("prototype")
@Component
public class SteelAxe implements Axe {
    ......
}

3.使用@Resource配置依賴

@Resource位於java.annotation包下,是來自Java EE規範的一個Annotation,Spring直接借鑑了該Annotation,通過使用該Annotation爲目標制定協作者Bean。

@Resource有一個name屬性,在默認情況下,Spring將這個值解釋爲需要被注入的Bean實例的名字。換句話說,使用@Resource與

package main.java.service.impl;

import javax.annotation.Resource;

import main.java.service.Axe;
import main.java.service.Person;

import org.springframework.stereotype.Component;

@Component
public class Chinese implements Person {

    private Axe axe;

    @Resource(name = "stoneAxe")
    public void setAxe(Axe axe) {
        this.axe = axe;
    }

    @Override
    public void useAxe() {
        System.out.println(this.axe.chop());
    }
}

該Annotation指定將stoneAxe注入該setAxe()方法,也就是將容器中的stoneAxe Bean作爲setAxe()的參數傳入。

@Resource不僅可以修飾setter方法,也可以直接修飾Field,使用@Resource時還可以省略name屬性。如果使用@Resource修飾Field將會更加簡單,此時連setter方法都可以不要。例如:

package main.java.service.impl;

import javax.annotation.Resource;

import main.java.service.Axe;
import main.java.service.Person;

import org.springframework.stereotype.Component;

@Component
public class Chinese implements Person {

    @Resource(name = "stoneAxe")
    private Axe axe;

    @Override
    public void useAxe() {
        System.out.println(this.axe.chop());
    }
}

☞ 當使用@Resource修飾setter方法時,如果省略name屬性,則name屬性默認是該setter方法去掉前面的set子串、首字母小寫後得到的字符串。
☞ 當使用@Resource修飾Field時,如果省略name屬性,則name屬性默認與該Filed同名;如果不存在,則會查找實現其接口的Bean類

4.使用@PostConstruct和@PreDestory定製生命週期行爲

@PostConstruct和@PreDestory同樣位於java.annotation包下,也是來自Java EE規範的兩個Annotation,Spring直接借鑑了它們,用於定製Spring容器中Bean的生命週期行爲。
@PostConstruct與

5.Spring3.0新增的Annotation

Spring3.0新增了兩個Annotation:@DependsOn和@Lazy,其中用於強制初始化其他Bean,而則用於指定該Bean是否取消預初始化

6.自動裝配和精確裝配

Spring提供了@Autowired註解來指定自動裝配,使用@Autowired可以標註setter方法、普通方法、Field和構造器等

@Autowired標註setter方法時,默認採用的是byType的自動裝配策略。
@Autowired標註Field時,默認採用的是byType策略,如果實例多餘一個,則會拋出異常
@Autowired甚至可以用於修飾數組類型的Field,如下代碼所示:

@Component
public class Chinese implements Person {

    @Autowired
    private Axe[] axes;

    @Override
    public void useAxe() {
        for (Axe axe : axes) {
            System.out.println(axe.chop());
        }
    }
}

被@Autowired修飾的axes屬性是Axe[]數組,在這種情況下,Spring會自動搜索容器中所有的Axe實例,並以這些axe實例作爲數組元素來創建數組。
與此類似的是,@Autowired也可以標註集合Field,或標註形參類型是集合的方法,Spring對這種集合屬性、集合形參的處理與前面數組類型的處理是完全相同的,對於這種集合類型的參數而言,程序代碼中必須使用泛型。

@Qualifier註解
@Autowired總是採用byteType的自動裝配策略,在這種策略下,符合自動裝配類型的候選Bean實例常常有多個,這個時候就可能引起異常了;爲了實現精確的自動裝配,Spring提供了@Qualifier註解,通過使用@Qualifier,允許根據Bean標識來指定自動裝配。例如:

@Component
public class Chinese implements Person {

    @Autowired
    @Qualifier("stoneAxe")
    private Axe axe;

    @Override
    public void useAxe() {
        System.out.println(this.axe.chop());
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章