spring沒有采用約定優於配置的策略,spring要求顯示指定搜索哪些路徑下的Java文件。spring將會把合適的java類全部注冊成spring Bean。
問題:spring怎么知道把哪些Java類當初bean類處理?
這就需要使用annotation,spring使用一些特殊的annotation來標注bean類。
@Component:標准一個普通的spring Bean類。
@Controller:標注一個控制器組件類。
@Service:標注一個業務邏輯組件類。
@Repository:標注一個DAO組件類。
Bean實例的名稱默認是Bean類的首字母小寫,其他部分不變。
在spring未來的版本中,@Controller,@Service,@Repository會攜帶更多語義。盡量考慮使用@Controller,@Service,@Repository代替通用的@Component。
指定了某些類可作為Spring Bean類使用后,最好還需要讓spring搜索指定路徑,此時需要在spring配置文件中導入context Schema,並指定一個簡單的搜索路徑。
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 自動掃描指定包及其子包下的所有Bean類 --> <context:component-scan base-package="org.crazyit.app.service"/> </beans>
我們可以通過為<context:component-scan>添加<include-filter...>或<exclude-filter...>子元素來指定spring bean類,只要位於指定路徑下的java類滿足這種規則,即使這些java類沒有使用任何annotation標注,spring一樣會將他們當初bean類來處理。
<include-filter...>:滿足該規則的java類會被當初bean類處理。
<exclude-filter...>:指定滿足該規則的java類不會被當初bean類處理。
這兩個子元素有兩個屬性:
type:指定過濾器類型。
expression:指定過濾器所需要的表達式。
spring內建支持如下四種過濾器:
annotation:該過濾器要指定一個annotation名,如lee.AnnotationTest。
assignable:類名過濾器,該過濾器直接指定一個java類。
regex:正則表達式過濾器,該過濾器指定一個正則表達式,匹配該正則表達式的java類將滿足該過濾規則,如org\.example\.default.*。
aspectj:如org.example..*service+。
<!-- 自動掃描指定包及其子包下的所有Bean類 --> <context:component-scan base-package="org.crazyit.app.service"> <!-- 只將以Chinese、Axe結尾的類當成Spring容器中的Bean --> <context:include-filter type="regex" expression=".*Chinese"/> <context:include-filter type="regex" expression=".*Axe"/> </context:component-scan>
@Resource位於java.annotation包下,來自於java EE規范的一個annotation。使用該annotation為目標bean指定協作者Bean。
@Resource詳細用法見經典javaEE企業應用實戰。
@Resource有一個name屬性,在默認情況下,spring將這個值解釋為需要被注入的Bean實例的名字。
@Controller public class demo { @Resource(name="user") private User user; @Resource(name="user") public void setUser(User user) { this.user = user; } public User getUser() { return user; } }
@Resource也可以直接修飾Filed,
如果@Resource修飾Field,這時候連該屬性的setter方法就不需要了。
使用@Resource可以省略name屬性。
修飾方法時,省略name屬性,則該name值是該setter方法去掉前面的set字符串,首字母小寫后得到的子串。
修飾Field時,省略name屬性,則該name與該Field同名。
指定Bean實例的作用域。
@Scope:注解也可以指定Bean實例的作用域。
@Controller("demo") @Scope("prototype") public class demo { }
作用范圍就那4中填寫,不知道的等我博客,最近在回顧spring知識。
@PostConstruct和@PreDestory位於java.annotation包下。
在spring中用於定制spring容器中bean的生命周期行為。
@PostConstruct修飾的方法是bean的初始化之前的方法。
@PreDestory修飾的方法是bean銷毀之前的方法。
深刻理解該類使用了@PostConstruct修飾init方法,那么spring就會在該bean的依賴關系注入完成之后回調該方法。
@Component public class SteelAxe { public SteelAxe() { System.out.println("創建SteelAxe類對象實例..."); } }
demo1:
@Component public class Chinese { // 執行Field注入 @Resource(name="steelAxe") private SteelAxe steeAxe; public Chinese() { super(); System.out.println("創建Chinese類對象實例..."); } @PostConstruct public void init() { System.out.println("正在執行初始化的init方法..."); } @PreDestroy public void close() { System.out.println("正在執行銷毀之前的close方法..."); } }
// 創建Spring容器 AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); // 注冊關閉鈎子 ctx.registerShutdownHook();
創建Chinese類對象實例...
創建SteelAxe類對象實例...
正在執行初始化的init方法...
正在執行銷毀之前的close方法...
如果注釋掉chinese的依賴注入,那么結果如下:
@Component public class Chinese { // 執行Field注入 //@Resource(name="steelAxe") //private SteelAxe steeAxe; public Chinese() { super(); System.out.println("創建Chinese類對象實例..."); } @PostConstruct public void init() { System.out.println("正在執行初始化的init方法..."); } @PreDestroy public void close() { System.out.println("正在執行銷毀之前的close方法..."); } }
打印:
創建Chinese類對象實例...
正在執行初始化的init方法...
創建SteelAxe類對象實例...
正在執行銷毀之前的close方法...