spring常用註解使用解析,spring註解解析
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/beanshttp://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 自動掃描指定包及其子包下的所有Bean類 --><context:component-scanbase-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:Regex過濾器,該過濾器指定一個Regex,匹配該Regex的java類將滿足該過濾規則,如org\.example\.default.*。aspectj:如org.example..*service+。
<!-- 自動掃描指定包及其子包下的所有Bean類 --><context:component-scanbase-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執行個體的名字。
@Controllerpublic 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的依賴關係注入完成之後回調該方法。
@Componentpublic class SteelAxe{public SteelAxe(){System.out.println("建立SteelAxe類對象執行個體...");}} demo1:
@Componentpublic class Chinese{// 執行Field注入@Resource(name="steelAxe")private SteelAxe steeAxe; public Chinese() { super();System.out.println("建立Chinese類對象執行個體...");}@PostConstructpublic void init(){System.out.println("正在執行初始化的init方法...");}@PreDestroypublic void close(){System.out.println("正在執行銷毀之前的close方法...");}}
// 建立Spring容器AbstractApplicationContext ctx = newClassPathXmlApplicationContext("beans.xml");// 註冊關閉鉤子ctx.registerShutdownHook();列印:
建立Chinese類對象執行個體...
建立SteelAxe類對象執行個體...
正在執行初始化的init方法...
正在執行銷毀之前的close方法... 如果注釋掉chinese的依賴注入,那麼結果如下:
@Componentpublic class Chinese{// 執行Field注入//@Resource(name="steelAxe")//private SteelAxe steeAxe; public Chinese() {super();System.out.println("建立Chinese類對象執行個體...");}@PostConstructpublic void init(){System.out.println("正在執行初始化的init方法...");}@PreDestroypublic void close(){System.out.println("正在執行銷毀之前的close方法...");}}列印:
建立Chinese類對象執行個體...
正在執行初始化的init方法...
建立SteelAxe類對象執行個體...
正在執行銷毀之前的close方法...