Spring實戰之Bean的自動裝配和檢測

  • Bean的自動裝配:讓spring自動識別如何裝配Bean的依賴關係

自動裝配的四種策略:

 

類型

策略

舉例

byName

把與Bean屬性具有相同名字的其他Bean自動裝配到Bean的對應屬性中,沒有匹配的不裝配

<bean id ="beanDemo" 

class="com.spring.demo.DemoBean"    autowire="byName">

<property name="name"  value="caka"/>

</beans>

byType

把與Bean屬性具有相同類型的其他Bean自動裝配到Bean的對應屬性中,沒有匹配的不裝配。spring提供兩種裝配設置,primary是true的優先裝配,但該屬性默認爲true,所以設置了某個bean爲優先裝配時,需要將其他的bean的primary設置爲false。若在自動裝配時,希望排除某些bean,可以設置autowire-candidate爲false

<bean id ="beanDemo" class="com.spring.demo.DemoBean"  

primary="false"/>

 

<bean id ="beanDemo" class="com.spring.demo.DemoBean"   

autowire-candidate=“false”/>

 

constructor

把與Bean的構造器入參具有相同類型的其他Bean自動裝配到Bean的對應屬性中,沒有匹配的不裝配。

<bean id ="beanDemo" class="com.spring.demo.DemoBean"   

autowire=“constructor”/>

autodetect

首先嚐試constructor裝配,如果失敗,再使用byType裝配

<bean id ="beanDemo" class="com.spring.demo.DemoBean"   

autowire=“autodetect”/>

注意:使用constructor自動裝配策略時,必須讓spring自動裝配構造器的所有入參,不能混合使用constructor自動裝配策略和<constructor-arg>元素。

使用註解裝配之spring自帶註解@Autowired:

spring容器默認禁用註解裝配的。我們需要在spring的配置在啓動它纔可以基於註解的自動裝配。<context:annotation-config>元素表明使用基於註解的自動裝配。

1.標註setter方法,使用註解就可以移除配置文件裏對應的<property>元素了

@Autowired
public void setDemoBean(DemoBean demoBean){
this.demoBean=demoBean
}

2.標註構造器

@Autowired
public DemoAnnotation(DemoBean demoBean){
this.demoBean=demoBean
}

3.標註自動裝配Bean引用的任意方法

@Autowired
public void otherMethod(DemoBean demoBean){
this.demoBean=demoBean
}

4.直接標註屬性,並刪除setter方法

@Autowired
private DemoBean demoBean;

關於@Autowired使用的注意點:

1.裝配時沒有匹配的Bean,會拋出NoSuchBeanDefinitionException異常,比如有時候屬性不一定必須要裝配,可以爲null,設置@Autowired的required屬性爲false,裝配時如果沒有找到匹配的Bean會設爲null。

@Autowired(required=false)
private DemoBean demoBean;

2.匹配到多個Bean時,使用@Qualifier註解裝配指定Id的Bean。

@Autowired
@Qualifier(“demoId”)
private DemoBean demoBean;

3.構造器裝配,只能有一個Bean的構造器可以將@Autowired的required屬性設置爲true,其他的需設置爲false。

使用註解裝配之@Inject:

@Inject與@Autowired一樣,只是@Inject沒有@required屬性,因此@Inject標註的依賴關係必須存在,否則會拋出異常。

相對於@Autowired對應的@Qualifier,@Inject對應的是@Named

@Autowired
@Name(“demoId”)
private DemoBean demoBean;
  • Bean的自動檢測:讓spring能自動識別哪些類需要配置成Spring Bean,從而減少對<bean>元素的使用。配置spring的自動檢測,需要使用<context:componenet-scan>元素。

<?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:context=" http://www.springframework.org/schema/context"
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">

<context:componenet-scan   base-package="com.spring.demo"
</context:componenet-scan> 

</beans>

<context:componenet-scan>元素會掃描指定的包及其子包,並查找出能自動註冊爲spring  bean的類。但是問題來了:<context:componenet-scan>是如何確定哪些類需要註冊爲spring Bean呢?

基於註解的自動檢測:

默認情況下,<context:componenet-scan>查找使用構造型註解所標註的類。

  • @Component:通用的構造器註解,標識該類爲spring組件。

  • @Controller: 標識該類定義爲SpringMVC的controller

  • @Repository:標識該類定義爲數據倉庫

  • @Service: 標識該類爲服務類

可以通過平配置<context:componenet-scan>,並使用@Component註解標註DemoBean,來消除顯示的<bean>定義。

package com.spring.demo;
import org.springframework.stereotype.Component;
@Component
//如果要指定bean的Id,可以在註解上直接指定如:@Component(“defaultBean”)
public class DemoBean{
//省略
}

spring掃描com.spring.demo包時,會發現使用@Component註解所標註的類DemoBean,並自動將它註冊爲Spring Bean。Bean的Id默認爲無限定類名:demoBean,如果要指定bean的Id,可以在註解上直接指定如:@Component(“defaultBean”)。

組件掃描的自動檢測:

通過爲<context:componenet-scan>配置<context:include-filter>或者<context:exclude-filter>子元素,自動註冊所有實現某spring bean接口的實現類。

<context:componenet-scan   base-package="com.spring.demo"
<context:include-filter type="assignable" expression="com.spring.demo.Instrument">
<context:exclude-filter type="annotation" expression="com.spring.demo.skiptBean">
</context:componenet-scan> 

<context:include-filter>的type和expression屬性一起來協作定義組件掃描策略。所有派生於Instrument的所有類自動註冊爲spring Bean。而<context:exclude-filter>來告知spring哪些類不需要註冊爲Spring bean。

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章