Spring組件掃描 使用詳解


1.如果不想在xml文件中配置bean,我們可以給我們的類加上spring組件注解,只需再配置下spring的掃描器就可以實現bean的自動載入。

<!-- 注解注入 -->
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.liantuo.hotel.common.service.impl" />
<context:component-scan base-package="com.liantuo.hotel.common.dao.ibatis" />
<context:component-scan base-package="com.liantuo.hotel.app.dao.ibatis" />
<context:component-scan base-package="com.liantuo.hotel.app.service" />
<context:component-scan base-package="com.liantuo.hotel.app.service.ibatis" />
@Component(組件)@Service(服務層)@Controller(控制層)@Repository(數據庫訪問層)
3.有了<context:component-scan>,另一個<context:annotation-config/>標簽根本可以移除掉,因為已經被包含進去了。
<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>

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM