spring核心配置文件引入外部properties文件和另外的xml配置文件
spring核心配置文件引入外部properties文件和另外的xml配置文件
為什么要引入外部文件
我們使用jdbc的時(shí)候,會(huì)創(chuàng)建一個(gè)jdbc.properties配置文件,如果我需要在spring的IOC中存入連接池對(duì)象呢?
是不是需要給鏈接對(duì)象的屬性u(píng)rl username password driver ,我們應(yīng)該如何在spring的配置文件中引入外部文件呢?
引入properties文件
? Spring提供了讀取外部properties文件的機(jī)制,使用讀取到的數(shù)據(jù)為bean的屬性賦值
? 操作步驟
xmlns:context=“http://www.springframework.org/schema/context”
< context:property-placeholder location=“classpath:filename.properties”>
< property name=“propertyName” value="propertiesName"/>?注意:如果需要加載所有的properties文件,可以使用?.properties表示加載所有的properties文件?注意:讀取數(shù)據(jù)使用{propertiesName}"/> ? 注意:如果需要加載所有的properties文件,可以使用*.properties表示加載所有的properties文件 ? 注意:讀取數(shù)據(jù)使用propertiesName"/>?注意:如果需要加載所有的properties文件,可以使用?.properties表示加載所有的properties文件?注意:讀取數(shù)據(jù)使用{propertiesName}格式進(jìn)行,其中propertiesName指properties文件中的屬性名
代碼演示
實(shí)體類準(zhǔn)備
我使用一個(gè)User類,有兩個(gè)屬性,我這個(gè)兩個(gè)屬性的值,從properties配置文件中讀取值,通過IOC 的DI依耐注入值
package com.fs.demo;public class User {private String username;private String password;// set方法讓spring可以DI依耐注入屬性public void setUsername(String username) {this.username = username;}public void setPassword(String password) {this.password = password;}@Overridepublic String toString() {return "User{" +"username='" + username + '\'' +", password='" + password + '\'' +'}';} }properties配置文件
username=xiaofu
password=1234
**注意:**配置文件中username前面要加個(gè)前綴,不然賦值的時(shí)候會(huì)把你當(dāng)前計(jì)算機(jī)的用戶名取出來,而不是從properties中讀取的,原因是因?yàn)橛?jì)算機(jī)的環(huán)境變量中有個(gè)值也是username,而spring啟動(dòng)的時(shí)候會(huì)讀取環(huán)境變量中的值,從而導(dǎo)致和properties中的值沖突
applicationContext.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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- 加載context命名空間 xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd --><!-- 引入外部properties文件properties中的key不要和本機(jī)的環(huán)境變量中的值沖突,因?yàn)閟pring在加載的時(shí)候獲取計(jì)算機(jī)的環(huán)境變量中的值若與環(huán)境中的值沖突,讀取出來的值就是計(jì)算機(jī)環(huán)境變量中設(shè)置的值所以,我們配置jdbc的時(shí)候一般就是jdbc.username.不會(huì)直接username注意:若要導(dǎo)入多個(gè)properties,需要在主配置文件中配置,classpath:*.properties<context:property-placeholder location="classpath:*.properties"/>--><context:property-placeholder location="classpath:data.properties"/><!-- 把user交給ioc管理--><bean id="user" class="com.fs.demo.User"><!-- ${properties中的key名} --><property name="username" value="${user.username}"/><property name="password" value="${user.password}"/></bean> </beans>測(cè)試代碼
@Testpublic void testProperties(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");User user = (User) applicationContext.getBean("user");System.out.println(user);/*properties配置信息username=xiaofupassword=1234User{username='93422', password='1234'}若輸出結(jié)果不為properties中配置的值,這個(gè)值是我的電腦用戶名,因?yàn)閟pring在啟動(dòng)的時(shí)候會(huì)讀取計(jì)算機(jī)的環(huán)境變量,環(huán)境變量中會(huì)有這個(gè)username所以拿到的值是你環(huán)境變量中配置的值,而不是你properties中設(shè)置的值所以,我們?cè)趯憄roperties的key的時(shí)候,一般會(huì)jdbc.username,以免和系統(tǒng)的環(huán)境變量中的username沖突,導(dǎo)致取出的值不是properties中的值*//*properties配置信息是這樣的就會(huì)正常讀取到值user.username=xiaofuuser.password=1234//打印輸出User{username='xiaofu', password='1234'}*/}控制臺(tái)運(yùn)行結(jié)果
spring核心配置文件中導(dǎo)入其他的.xml配置文件
團(tuán)隊(duì)開發(fā)
? 名稱:import
? 類型:標(biāo)簽
? 歸屬:beans標(biāo)簽
? 作用:在當(dāng)前配置文件中導(dǎo)入其他配置文件中的項(xiàng)
? 格式:
< beans>
< import />
< /beans>
? 基本屬性:
< import resource=“config.xml"/>
◆ resource:加載的配置文件名
? Spring容器加載多個(gè)配置文件
new ClassPathXmlApplicationContext(“config1.xml”,“config2.xml”);
? Spring容器中的bean定義沖突問題
◆ 同id的bean,后定義的覆蓋先定義的
◆ 導(dǎo)入配置文件可以理解為將導(dǎo)入的配置文件復(fù)制粘貼到對(duì)應(yīng)位置
◆ 導(dǎo)入配置文件的順序與位置不同可能會(huì)導(dǎo)致最終程序運(yùn)行結(jié)果不同
代碼演示
實(shí)體類
創(chuàng)建一個(gè)類,這個(gè)類有個(gè)實(shí)體類屬性,一個(gè)set方法,用于DI依耐注入
package com.fs.demo;public class Demo {private TestDemo testDemo;public void setTestDemo(TestDemo testDemo) {this.testDemo = testDemo;}@Overridepublic String toString() {return "Demo{" +"testDemo=" + testDemo +'}';} }創(chuàng)建一個(gè)類,這個(gè)類是上一個(gè)類的屬性
package com.fs.demo;public class TestDemo {public void testDemo(){System.out.println("TestDemo測(cè)試語句輸出");} }applicationContext.xml配置文件
applicationContext.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"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttps://www.springframework.org/schema/context/spring-context.xsd"><!-- 引入外部的spring的配置文件--><import resource="classpath:applicationContext02.xml"/><bean id="demo" class="com.fs.demo.Demo"> <!-- 這里就體現(xiàn)了引入外部的applicationContext02.xml文件,因?yàn)閞ef="testDemo這就是在applicationContext02配置的--><property name="testDemo" ref="testDemo"/></bean> </beans>applicationContext02.xml其他的.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.xsd"><!-- 把test交給springIOC管理--><bean id="testDemo" class="com.fs.demo.TestDemo"/></beans>測(cè)試代碼
//測(cè)試引入其他的配置文件@Testpublic void testDemo(){ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//在主配置文件中獲取出daoDemo demo = (Demo) applicationContext.getBean("demo");//Demo{testDemo=com.fs.demo.TestDemo@544a2ea6}因?yàn)樵赼pplicationContext02.xml中吧testDemo交給IOC管理了,所以demo中的set注入成功System.out.println(demo);}控制臺(tái)運(yùn)行效果
總結(jié)
以上是生活随笔為你收集整理的spring核心配置文件引入外部properties文件和另外的xml配置文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringIOC的依耐注入DI---s
- 下一篇: 浅谈:数据结构之双链表结构与代码模拟双链