日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

spring配置xml文件_XML配置文件中的Spring配置文件

發布時間:2023/12/3 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring配置xml文件_XML配置文件中的Spring配置文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

spring配置xml文件

我的上一個博客非常簡單,因為它涵蓋了我從Spring 3.0.x到Spring 3.1.x的輕松升級,最后我提到可以將Spring模式升級到3.1,以利用Spring的最新功能。 在今天的博客中,我將介紹這些功能中最酷的功能之一:Spring配置文件。 但是,在談論如何實現Spring配置文件之前,我認為探索它們正在解決的問題是一個好主意,這是需要為不同的環境創建不同的Spring配置。 之所以會出現這種情況,通常是因為您的應用程序在其開發生命周期中需要連接到多個類似的外部資源,并且連接頻率通常更高,并且這些“外部資源”通常不是數據庫,盡管它們可以是JMS隊列,Web服務,遠程EJB等。

您的應用程序在運行之前必須工作的環境數量通常取決于幾件事,包括您組織的業務流程,應用程序的規模以及它的“重要性”(即,如果您正在編寫稅收表)系統為您的國家/地區提供稅收服務,則測試過程可能比為本地商店編寫電子商務應用程序時更為嚴格。 為使您有所了解,以下是想到的所有不同環境的快速列表(可能不完整):

  • 本地開發人員機器
  • 開發測試機
  • 測試團隊功能測試機
  • 集成測試機
  • 克隆環境(實時副本)
  • 生活
  • 這不是一個新問題,通常可以通過為每個環境創建一組Spring XML和屬性文件來解決。 XML文件通常包含一個導入其他環境特定文件的主文件。 然后在編譯時將它們耦合在一起以創建不同的WAR或EAR文件。 這種方法已經使用了多年,但確實存在一些問題:

  • 這是非標準的。 每個組織通常都有自己的解決方案,沒有兩種方法可以完全相同/
  • 很難實現,為錯誤留出了很大的空間。
  • 必須為每個環境創建一個不同的WAR / EAR文件并將其部署在每個環境上,這需要花費時間和精力,這可能會花費更多的時間來編寫代碼。
  • Spring bean配置中的差異通常可以分為兩部分。 首先,存在特定于環境的屬性,例如URL和數據庫名稱。 通常使用PropertyPlaceholderConfigurer類和相關的$ {}標記將它們注入到Spring XML文件中。

    <bean id='propertyConfigurer' class='org.springframework.beans.factory.config.PropertyPlaceholderConfigurer'><property name='locations'><list><value>db.properties</value></list></property></bean>

    其次,有特定于環境的bean類,例如數據源,它們通常根據您連接數據庫的方式而有所不同。

    例如,在開發中,您可能具有:

    <bean id='dataSource' class='org.springframework.jdbc.datasource.DriverManagerDataSource'><property name='driverClassName'><value>${database.driver}</value></property><property name='url'><value>${database.uri}</value></property><property name='username'><value>${database.user}</value></property><property name='password'><value>${database.password}</value></property></bean>

    …無論是測試還是現場直播,您只要寫:

    <jee:jndi-lookup id='dataSource' jndi-name='jdbc/LiveDataSource'/>

    Spring準則指出,僅應在上面的第二個示例中使用Spring概要文件:特定于Bean的類,并且您應繼續使用PropertyPlaceholderConfigurer初始化簡單的Bean屬性; 但是,您可能希望使用Spring配置文件將特定于環境的PropertyPlaceholderConfigurer注入到Spring上下文中。

    話雖如此,我將在示例代碼中打破這一約定,因為我想用最簡單的代碼來演示Spring配置文件的功能。

    Spring配置文件和XML配置
    在XML配置方面,Spring 3.1在spring-beans模式的bean元素中引入了新的profile屬性:

    <beans profile='dev'>

    在不同環境中啟用和禁用配置文件時,此配置文件屬性充當開關。

    為了進一步解釋所有這些,我將使用一個簡單的想法,即您的應用程序需要加載一個人員類,并且該人員類包含不同的屬性,具體取決于您的程序在其上運行的環境。

    Person類非常簡單,看起來像這樣:

    public class Person {private final String firstName;private final String lastName;private final int age;public Person(String firstName, String lastName, int age) {this.firstName = firstName;this.lastName = lastName;this.age = age;}public String getFirstName() {return firstName;}public String getLastName() {return lastName;}public int getAge() {return age;}}

    …并在以下XML配置文件中定義:

    <?xml version='1.0' encoding='UTF-8'?> <beans xmlns='http://www.springframework.org/schema/beans'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd'profile='test1'><bean id='employee' class='profiles.Person'><constructor-arg value='John' /><constructor-arg value='Smith' /><constructor-arg value='89' /></bean> </beans>

    …和

    <?xml version='1.0' encoding='UTF-8'?> <beans xmlns='http://www.springframework.org/schema/beans'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd'profile='test2'><bean id='employee' class='profiles.Person'><constructor-arg value='Fred' /><constructor-arg value='ButterWorth' /><constructor-arg value='23' /></bean> </beans>

    …分別稱為test-1-profile.xml和test-2-profile.xml (請記住這些名稱,它們在以后很重要)。 如您所見,配置的唯一區別是名字,姓氏和年齡屬性。

    不幸的是,僅僅定義個人資料還不夠,您必須告訴Spring您正在加載哪個個人資料。 這意味著遵循舊的“標準”代碼現在將失敗:

    @Test(expected = NoSuchBeanDefinitionException.class)public void testProfileNotActive() {// Ensure that properties from other tests aren't setSystem.setProperty('spring.profiles.active', '');ApplicationContext ctx = new ClassPathXmlApplicationContext('test-1-profile.xml');Person person = ctx.getBean(Person.class);String firstName = person.getFirstName();System.out.println(firstName);}

    幸運的是,有幾種選擇配置文件的方法,在我看來,最有用的方法是使用“ spring.profiles.active”系統屬性。 例如,以下測試現在將通過:

    System.setProperty('spring.profiles.active', 'test1');ApplicationContext ctx = new ClassPathXmlApplicationContext('test-1-profile.xml');Person person = ctx.getBean(Person.class);String firstName = person.getFirstName();assertEquals('John', firstName);

    顯然,您不想像我上面那樣對代碼進行硬編碼,最佳實踐通常意味著將系統屬性定義與應用程序分開。 這使您可以選擇使用簡單的命令行參數,例如:

    -Dspring.profiles.active='test1'

    …或通過添加

    # Setting a property value spring.profiles.active=test1

    Tomcat的catalina.properties

    因此,僅此而已:您可以使用bean元素配置文件屬性創建Spring XML配置文件,并通過將spring.profiles.active系統屬性設置為配置文件的名稱來打開要使用的配置文件 。

    獲得一些額外的靈活性

    但是,這還不是故事的結局,因為Spring的Guy已添加了許多以編程方式加載和啟用配置文件的方式-如果您愿意的話。

    @Testpublic void testProfileActive() {ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext('test-1-profile.xml');ConfigurableEnvironment env = ctx.getEnvironment();env.setActiveProfiles('test1');ctx.refresh();Person person = ctx.getBean('employee', Person.class);String firstName = person.getFirstName();assertEquals('John', firstName);}

    在上面的代碼中,我使用了新的ConfigurableEnvironment類來激活“ test1”配置文件。

    @Testpublic void testProfileActiveUsingGenericXmlApplicationContextMultipleFilesSelectTest1() {GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();ConfigurableEnvironment env = ctx.getEnvironment();env.setActiveProfiles('test1');ctx.load('*-profile.xml');ctx.refresh();Person person = ctx.getBean('employee', Person.class);String firstName = person.getFirstName();assertEquals('John', firstName);}

    但是,Spring專家們建議您使用GenericApplicationContext類而不是ClassPathXmlApplicationContext和FileSystemXmlApplicationContext,因為這樣可以提供更大的靈活性。 例如,在上面的代碼中,我已經使用GenericApplicationContext的load(...)方法使用通配符來加載許多配置文件:

    ctx.load('*-profile.xml');

    還記得以前的文件名嗎? 這將同時加載test-1-profile.xml和test-2-profile.xml 。

    配置文件還具有額外的靈活性,可讓您一次激活多個。 如果看下面的代碼,您會看到我正在激活我的test1和test2配置文件:

    @Testpublic void testMultipleProfilesActive() {GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();ConfigurableEnvironment env = ctx.getEnvironment();env.setActiveProfiles('test1', 'test2');ctx.load('*-profile.xml');ctx.refresh();Person person = ctx.getBean('employee', Person.class);String firstName = person.getFirstName();assertEquals('Fred', firstName);}

    請注意,在本示例中,我有兩個ID為“ employee”的bean,并且無法分辨哪個是有效的,并且應該優先。 從我的測試中,我猜想讀取的第二個將覆蓋或屏蔽對第一個的訪問。 沒關系,因為您不應該擁有多個具有相同名稱的bean –激活多個配置文件時需要提防。

    最后,可以做的更好的簡化之一是使用嵌套的<beans />元素。

    <?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'xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd'><beans profile='test1'><bean id='employee1' class='profiles.Person'><constructor-arg value='John' /><constructor-arg value='Smith' /><constructor-arg value='89' /></bean></beans><beans profile='test2'><bean id='employee2' class='profiles.Person'><constructor-arg value='Bert' /><constructor-arg value='William' /><constructor-arg value='32' /></bean></beans></beans>

    盡管以最小的靈活性為代價,但是這消除了通配符和加載多個文件的所有繁瑣處理。

    我的下一個博客通過查看與新的@Profile注釋結合使用的@Configuration注釋,總結了我對Spring概要文件的了解,因此,稍后會有更多介紹。

    參考:來自Captain Debug博客博客的JCG合作伙伴 Roger Hughes提供的在XML Config中使用Spring Profiles 。


    翻譯自: https://www.javacodegeeks.com/2012/08/spring-profiles-in-xml-config-files.html

    spring配置xml文件

    總結

    以上是生活随笔為你收集整理的spring配置xml文件_XML配置文件中的Spring配置文件的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

    主站蜘蛛池模板: 国产www免费观看 | 青青操网站 | 欧美xxxx中国 | 欧美乱妇高清无乱码 | 精品免费久久久 | 国产精品毛片久久 | 老师上课夹震蛋高潮了 | 久射久 | 欧美性色黄 | 成人久久久久久久 | 成人亚洲欧美 | 青苹果av | 在线无遮挡 | 激情小说激情视频 | 黄网在线观看视频 | 乌克兰性极品xxxhd | 六月婷婷综合 | 国产精品欧美亚洲 | 国产第99页 | 国产孕交| 国产精品99在线观看 | 欧美亚洲第一页 | 人乳videos巨大吃奶 | 国产又粗又猛又黄视频 | 日本韩国在线播放 | 龚玥菲一级淫片 | 色综合色综合色综合 | 黄色九九 | 欧美视频一区二区三区在线观看 | 朝鲜女人性猛交 | 久久国产精品无码网站 | 精品久久久久久久久久久久久久久久 | 特黄三级 | 亚洲成人免费网站 | 天堂аⅴ在线最新版在线 | 深爱激情av | 懂色中文一区二区在线播放 | 国产综合在线观看视频 | 一级黄色片免费在线观看 | 国产在视频线精品视频 | 久久久久久久久久久丰满 | 欧美一级在线观看视频 | 九九这里只有精品视频 | 欧美精品久久久久a | 久久日视频 | 激情爱爱网站 | 日韩一区二区三区视频 | 五月天丁香婷 | 丝袜诱惑一区 | 国产午夜一级 | 欧美精品一区二区免费 | 国产精品无码久久久久高潮 | 猛1被调教成公厕尿便失禁网站 | 人人干干| 亚洲中文字幕无码爆乳av | 日韩av手机在线播放 | 男人狂揉女人下部视频 | 91成人在线播放 | 国产另类精品 | 久久久精品一区二区涩爱 | 91国偷自产一区二区三区女王 | 成年丰满熟妇午夜免费视频 | 欧美性猛交ⅹ乱大交3 | 欧美性生交xxxxx | 亚洲精品免费观看 | 久久精品免费一区二区 | 在线亚洲天堂 | 97精品超碰一区二区三区 | 亚洲精品喷潮一区二区三区 | 免费看h网站 | 亚洲天堂资源网 | 亚洲 另类 春色 国产 | 免费看国产黄色片 | 亚洲午夜毛片 | 99嫩草| 国产理论片在线观看 | 欧美日韩国产成人 | av老司机在线观看 | 动漫美女无遮挡免费 | 亚洲熟妇一区二区三区 | 日本高清无吗 | 国产a一区 | 国产av剧情一区 | 久久午夜鲁丝片午夜精品 | 奇米狠狠去啦 | 99热网 | 天天色天天插 | 韩国av电影在线观看 | 国产精品日韩在线 | 中国av一级片 | 激情av在线播放 | 久久久久久久久久免费 | 亚洲男女一区二区三区 | 精品久久久视频 | 神马福利视频 | 韩国精品一区二区 | 日韩欧美国产精品综合嫩v 国产小毛片 | 免费在线激情视频 | xvideos永久免费入口 |