spring配置文件詳解--註解方式

先來一段配置示例

<?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"
	<!--啓用AOP功能時的命名空間-->
	xmlns:aop="http://www.springframework.org/schema/aop" 
	<!--啓用聲明事務時的命名空間-->
	xmlns:tx="http://www.springframework.org/schema/tx"
	<!--p名稱空間,詳解請看我的另一篇博客“Spring使用p名稱空間配置屬性”-->
	xmlns:p="http://www.springframework.org/schema/p"
	<!--與上述命名空間相配套的schema定義文件的裝載路徑-->
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context
		http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/tx
		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">
		
	<!--開啓註解處理器-->
	<context:annotation-config />
	
	<!--開啓組件自動掃描,掃描路徑由base-package屬性指定,配置了此項後,就無需配置<context:annotation-config>-->
	<context:component-scan base-package="com.yoon">
		<!--子節點表示要包含的目標類-->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
		<!--子節點表示要排除在外的目標類-->
		<context:exclude-filter>
	</context:component-scan>
	<!--開啓基於@AspectJ切面的註解處理器-->
	<aop:aspectj-autoproxy />
	<!--支持事務註解的(@Transactional), 需要注意的是,spring 是使用 aop 通過 asm 操作java字節碼的方式來實現對方法的前後事務管理的-->
	 <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
	<!--事務管理器-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!--dataSourceId數據源beanId-->
		<property name="dataSource" ref="dataSourceId" />
	</bean>
</beans>
注意:
1、組件掃描(component scanning):Spring是從classpath下自動掃描,偵測和實例化具有特定註解的組件
特定的組件包括:
    -@Component:基本註解,標識了一個受Spring管理的組件
    -@Responsitory:標識持久層組件
    -@Service:標識服務層(業務層)組件
    -@Controller:標識表現層組件
2、對於掃描到的組件,Spring有默認的命名策略:使用非限定類名,第一個字母小寫
3、<context:component-sacn>下可以擁有若干個<context:include-filter>和<context:exclude-filter>子節點
<context:include-filter>和<context:exclude-filter>子節點支持多種類型的過濾表達式:
類別 示例 說明
annotation  com.yl.XxxAnnotation 所有標註了XxxAnnotation的類,該類型採用目標類是否標註了某個註解進行過濾
assinable  com.yl.XxxService 所有繼承或擴展XxxService的類,該類型採用了目標類是否繼承或擴展某個特定類進行過濾
aspectj  com.yl.*Service 所有類名義Service結束的類及繼承或擴展它們的類,該類型採用AspectJ表達式進行過濾
regex  com.yl.anno.* 所有com.yl.anno包下的類。該類型採用正則表達式,根據類的類名進行過濾
custom  com.yl.XxxTypeFilter  採用XxxTypeFilter通過代碼的方式定義過濾原則。該類必須實現org.springframework.core.type.TypeFilter接口

組件裝配
<context:component-scan>元素還會自動註冊AutowiredAnnotationBeanPostProcessor實例,該實例可以自動裝配具有@Autowired和@Resource註解的屬性,還有其它的註解,不是重點,這裏不講解。

使用@Autowired、@Resource自動裝配bean
在java代碼中使用@Autowired或@Resource註解方式進行裝配 ,這兩個註解的區別是:@Autowired默認按類型裝配,@Resource默認按名稱裝配,當找不到名稱匹配的bean纔會按類型裝配。
@Autowired一般裝配在set方法之上,也可以裝配在屬性上邊,但是在屬性上邊配置,破壞了java的封裝,所以一般不建議使用

@Autowired是根據類型進行自動裝配的。如果當Spring上下文中存在不止一個所要裝配類型的bean時,就會拋出BeanCreationException異常;如果Spring上下文中不存在所要裝配類型的bean,也會拋出BeanCreationException異常。



參考:http://blog.csdn.net/zzjjiandan/article/details/22922847/

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