【設定檔節點】java世界設定檔節點

來源:互聯網
上載者:User

標籤:

Spring <context:property-placeholder/>

期望:能不能有一種解決方案可以方便我們在一個階段內不需要頻繁書寫一個參數的值,而在不同階段間又可以方便的切換參數配置資訊解決:spring3中提供了一種簡便的方式就是context:property-placeholder/元素只需要在spring的設定檔裡添加一句:<context:property-placeholder location="classpath:jdbc.properties"/> 即可,這裡location值為參數設定檔的位置,參數設定檔通常放在src目錄下,而參數設定檔的格式跟java通用的參數設定檔相同,即索引值對的形式,例如:#jdbc配置test.jdbc.driverClassName=com.mysql.jdbc.Drivertest.jdbc.url=jdbc:mysql://localhost:3306/testtest.jdbc.username=roottest.jdbc.password=root行內#號後面部分為注釋應用:1.這樣一來就可以為spring配置的bean的屬性設定值了,比如spring有一個jdbc資料來源的類DriverManagerDataSource在設定檔裡這麼定義bean:<bean id="testDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${test.jdbc.driverClassName}"/> <property name="url" value="${test.jdbc.url}"/> <property name="username" value="${test.jdbc.username}"/> <property name="password" value="${test.jdbc.password}"/></bean>http://stamen.iteye.com/blog/1926166

Spring <context:annotation-config/>

在基於主機方式配置Spring的設定檔中,你可能會見到<context:annotation-config/>這樣一條配置,他的作用是式地向 Spring 容器註冊AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 這 4 個BeanPostProcessor。註冊這4個 BeanPostProcessor的作用,就是為了你的系統能夠識別相應的註解。例如:如果你想使用@Autowired註解,那麼就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。傳統聲明方式如下<bean class="org.springframework.beans.factory.annotation. AutowiredAnnotationBeanPostProcessor "/> 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等註解就必須聲明CommonAnnotationBeanPostProcessor如果想使用@PersistenceContext註解,就必須聲明PersistenceAnnotationBeanPostProcessor的Bean。如果想使用 @Required的註解,就必須聲明RequiredAnnotationBeanPostProcessor的Bean。同樣,傳統的聲明方式如下:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 一般來說,這些註解我們還是比較常用,尤其是Antowired的註解,在自動注入的時候更是經常使用,所以如果總是需要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,於是spring給我們提供<context:annotation-config/>的簡化配置方式,自動幫你完成聲明。 不過,呵呵,我們使用註解一般都會配置掃描包路徑選項<context:component-scan base-package=”XX.XX”/> 該配置項其實也包含了自動注入上述processor的功能,因此當使用 <context:component-scan/> 後,就可以將 <context:annotation-config/> 移除了。

 

<context:component-scan base-package="com.javaniu" />

 

關於spring自動檢測組件的使用方式網上太多了,而且也不是我記錄的重點,我想說下一點可能你還不知道的經驗我們知道如果不想在xml檔案中配置bean,我們可以給我們的類加上spring組件註解,只需再配置下spring的掃描器就可以實現bean的自動載入。 先寫一個小例子,剩下的在下面解釋<!-- 定義掃描根路徑為leot.test,不使用預設的掃描方式 --><context:component-scan base-package="leot.test" use-default-filters="false">  <!-- 掃描符合@Service @Repository的類 -->  <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />  <context:include-filter type="annotation" expression="org.springframework.stereotype.Repository" /></context:component-scan> 下面是引用spring framework開發手冊中的一段話“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已經能在持久化層中進行異常轉換時被作為標記使用了。” 下面是網上目前關於組件掃描最詳細的介紹Spring applicationContext.xml的<context:component-scan>標籤用途比我想像的還要實用。而且後來才知道,有了<context:component-scan>,另一個<context:annotation-config/>標籤根本可以移除掉,因為被包含進去了。原本我survery Spring3通常只配置成<context:component-scan base-package="com.foo.bar"/>,意即在base-package下尋找有@Component和@Configuration的target Class。而現在如下的飯粒:<context:component-scan base-package="com.foo" use-default-filters="false"><context:include-filter type="regex" expression="com.foo.bar.*Config"/><context:include-filter type="regex" expression="com.foo.config.*"/></context:component-scan>  <context:component-scan>提供兩個子標籤:<context:include-filter>和<context:exclude-filter>各代表引入和排除的過濾。而上例把use-default-filters屬性設為false,意即在base-package所有被宣告為@Component和@Configuration等target Class不予註冊為bean,由filter子標籤代勞。  filter標籤在Spring3有五個type,如下:Filter Type    Examples Expression    Descriptionannotation    org.example.SomeAnnotation    符合SomeAnnoation的target classassignable    org.example.SomeClass    指定class或interface的全名aspectj    org.example..*Service+    AspectJ語法regex    org\.example\.Default.*    Regelar Expressioncustom    org.example.MyTypeFilter    Spring3新增自訂Type,實作org.springframework.core.type.TypeFilter  所以上例用的regex就有個語病,com.foo.config.* 可以找到com.foo.config.WebLogger,但也可以找到com1fool2config3abcde,因為小數點在Regex是任意字元,是故要用\.把小數點跳脫為佳。(2010/3/15補充:但要使用\.方式,其use-default-filters不能為false,否則抓不到,感覺是Bug)  Spring3提供豐富的Filter支援,有益配置策略,不需面臨Configuration Hell,比如Regex的com\.foo\.*\.action\.*Config,這樣就可以找到com.foo package下所有action子package的*Config的target class。 我按他的例子,配置了我自己的如下:<context:component-scan base-package="com.xhlx.finance.budget"  >  <context:include-filter type="regex" expression="com.lee.finance.budget.service.*"/>  <context:include-filter type="regex" expression="com.lee.finance.budget.security.*"/> </context:component-scan>但是死活掃描不到,網上又沒有更好的講解,沒辦法只好自己試,改成<context:component-scan base-package="com.xhlx.finance.budget" >  <context:include-filter type="regex" expression="com.lee.finance.budget.*"/></context:component-scan>這樣連沒有加註解的類也掃描並執行個體化,結果報錯,因為有的類根本就沒有預設的建構函式不能執行個體化,能不報錯嗎改成<context:component-scan base-package="com.xhlx.finance.budget"  >  <context:include-filter type="regex" expression="com\.lee\.finance\.budget\.service.*"/>  </context:component-scan>問題依舊<context:component-scan base-package="com.xhlx.finance.budget" >  <context:include-filter type="regex" expression="com.lee.finance.budget.service.TestService"/></context:component-scan>嘿,這次可以了,寫全限定名就可以,運算式卻不行,但是如果我有成千上百個還得一個一個這樣配置啊?不行,繼續研究,我想有個base-package的配置,從表面意思來看這是個“基本包”,是否表示下面的過濾路徑是基於這個包的呢?於是試著改成<context:component-scan base-package="com.xhlx.finance.budget" >  <context:include-filter type="regex" expression=".service.*"/></context:component-scan>嘿,行了。 

 

<mvc:annotation-driven />

 

<mvc:annotation-driven />說明 據spring api 文檔的解釋<mvc:annotation-driven /> 主要是解析@Controller @RequestMapping 等註解的。  官方說明:  <mvc:annotation-driven /> declares explicit support for annotation-driven MVC controllers (i.e. @RequestMapping, @Controller, although support for those is the default behaviour), as well as adding support for declrative validation via @Valid and message body marshalling with @RequestBody/ResponseBody.  翻譯: <mvc:annotation-driven/>聲明明確支援註解驅動的MVC控制器(即@RequestMapping, @Controller,支援那些雖然是預設行為),以及增加支援declrative驗證通過@有效和郵件內文編組@ RequestBody/ ResponseBody。  下面是我個人認為:文檔中提及主要是申明註解,@Controller,@RequestMapping但是後面加了一句預設行為,個人認為一般情況下 申明此類註解是預設的,但是在一些特殊的情況下是需要手動設定申明的。為了求真以上結果是否真確,我做了以下3個測試。  1一般.預設情況 無載入靜態資源 無配置<mvc:annotation-driven /> 一般情況下 當使用sprin MVC 時候若沒有載入靜態資源 比如 (<mvc:resources mapping="/css/**" location="/css/" />) 的時候系統預設會幫我們綁定路勁

 

【設定檔節點】java世界設定檔節點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.