<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:component-scan base-package="com.vrv.paw.dao,com.vrv.paw.service,com.vrv.paw.action" /> </beans>
1.如果不想在xml文件中配置bean,我們可以給我們的類加上spring組件注解,只需再配置下spring的掃描器就可以實現bean的自動載入。
<context:component-scan base-package="com.vrv.paw.dao,com.vrv.paw.service,com.vrv.paw.action" />
Spring 2.5引入了更多典型化注解(stereotype annotations): @Component、@Service和 @Controller。
@Component是所有受Spring管理組件的通用形式;而@Repository、@Service和 @Controller則是@Component的細化,用來表示更具體的用例(例如,分別對應了持久化層、服務層和表現層)。
也就是說,你能用@Component來注解你的組件類,但如果用@Repository、@Service 或@Controller來注解它們,你的類也許能更好地被工具處理,或與切面進行關聯。
例如,這些典型化注解可以成為理想的切入點目標。當然,在Spring Framework以后的版本中, @Repository、@Service和 @Controller也許還能攜帶更多語義。
如此一來,如果你正在考慮服務層中是該用@Component還是@Service,那@Service顯然是更好的選擇。
同樣的,就像前面說的那樣, @Repository已經能在持久化層中進行異常轉換時被作為標記使用了。
3.有了<context:component-scan>,另一個<context:annotation-config/>標簽根本可以移除掉,因為已經被包含進去了。
4.<context:component-scan>提供兩個子標簽:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。
<context:component-scan base-package="com.xhlx.finance.budget" > <context:include-filter type="regex" expression=".service.*"/> </context:component-scan>
5.filter標簽在Spring3有五個type,如下:
Filter Type
| Examples Expression | Description | |
| annotation | org.example.SomeAnnotation | 符合SomeAnnoation的target class |
| assignable | org.example.SomeClass | 指定class或interface的全名 |
| aspectj | org.example..*Service+ | AspectJ語法 |
| regex | org\.example\.Default.* | Regelar Expression |
| custom | org.example.MyTypeFilter | Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter |
