spring學(xué)習(xí)之util:properties的使用
<util:properties>可用于注入Properties對(duì)象,也可以用于讀取properties文件
1、手動(dòng)配置內(nèi)容
<util:properties id="properties"> <prop key="name">zhangsan</prop> <prop key="age">age</prop> <prop key="gender">gender</prop> </util:properties> <bean id="phone" class="org.spring.teach.Phone"> <property name="properties" ref="prop"/> </bean>
public class Phone { private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Phone{" + "properties=" + properties + '}'; } }
public class TestCase { private AbstractApplicationContext app; @Before public void before() { app = new ClassPathXmlApplicationContext("classpath:spring.xml"); } @After public void after() { app.close(); } @Test public void test() { Phone phone = app.getBean("phone", Phone.class); System.out.println(phone); } }
一月 25, 2019 10:53:26 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 10:53:26 CST 2019]; root of context hierarchy
一月 25, 2019 10:53:26 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
Phone{properties={age=age, name=zhangsan, gender=gender}}
一月 25, 2019 10:53:27 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 10:53:26 CST 2019]; root of context hierarchy
Process finished with exit code 0
2、通過(guò)讀取文件獲取內(nèi)容
<util:properties id="properties" location="classpath:db.properties"/> <bean id="phone" class="org.spring.teach.Phone"> <property name="properties" ref="prop"/> </bean>
public class Phone { private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Phone{" + "properties=" + properties + '}'; } }
public class TestCase { private AbstractApplicationContext app; @Before public void before() { app = new ClassPathXmlApplicationContext("classpath:spring.xml"); } @After public void after() { app.close(); } @Test public void test() { Phone phone = app.getBean("phone", Phone.class); System.out.println(phone); } }
"C:\Program Files\Java\jdk1.8.0_191\bin\java" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\Program Files\ideaIU-2018.1.win\lib\idea_rt.jar=57490:D:\Program Files\ideaIU-2018.1.win\bin" -Dfile.encoding=UTF-8 -classpath "D:\Program Files\ideaIU-2018.1.win\lib\idea_rt.jar;D:\Program Files\ideaIU-2018.1.win\plugins\junit\lib\junit-rt.jar;D:\Program Files\ideaIU-2018.1.win\plugins\junit\lib\junit5-rt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_191\jre\lib\rt.jar;F:\Workspace_idea\springday01\target\test-classes;F:\Workspace_idea\springday01\target\classes;F:\Repository\org\springframework\spring-webmvc\4.3.18.RELEASE\spring-webmvc-4.3.18.RELEASE.jar;F:\Repository\org\springframework\spring-aop\4.3.18.RELEASE\spring-aop-4.3.18.RELEASE.jar;F:\Repository\org\springframework\spring-beans\4.3.18.RELEASE\spring-beans-4.3.18.RELEASE.jar;F:\Repository\org\springframework\spring-context\4.3.18.RELEASE\spring-context-4.3.18.RELEASE.jar;F:\Repository\org\springframework\spring-core\4.3.18.RELEASE\spring-core-4.3.18.RELEASE.jar;F:\Repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;F:\Repository\org\springframework\spring-expression\4.3.18.RELEASE\spring-expression-4.3.18.RELEASE.jar;F:\Repository\org\springframework\spring-web\4.3.18.RELEASE\spring-web-4.3.18.RELEASE.jar;F:\Repository\junit\junit\4.11\junit-4.11.jar;F:\Repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;F:\apache-tomcat-8.5.37\lib\jsp-api.jar;F:\apache-tomcat-8.5.37\lib\servlet-api.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 test.TestCase,test04
一月 25, 2019 10:57:57 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 10:57:57 CST 2019]; root of context hierarchy
一月 25, 2019 10:57:57 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
一月 25, 2019 10:57:58 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
Phone{properties={user=root, url=jdbc:oracle:thin:@localhost:1521:orcl, driver=oracle.jdbc.driver.OracleDriver, initSize=1, pwd=root, maxSize=3}}
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 10:57:57 CST 2019]; root of context hierarchy
Process finished with exit code 0
3、手動(dòng)配置內(nèi)容和讀取文件一起使用
<util:properties id="properties" location="classpath:db.properties"> <prop key="name">zhangsan</prop> <prop key="age">age</prop> <prop key="gender">gender</prop> </util:properties> <bean id="phone" class="org.spring.teach.Phone"> <property name="properties" ref="prop"/> </bean>
public class Phone { private Properties properties; public void setProperties(Properties properties) { this.properties = properties; } @Override public String toString() { return "Phone{" + "properties=" + properties + '}'; } }
public class TestCase { private AbstractApplicationContext app; @Before public void before() { app = new ClassPathXmlApplicationContext("classpath:spring.xml"); } @After public void after() { app.close(); } @Test public void test() { Phone phone = app.getBean("phone", Phone.class); System.out.println(phone); } }
一月 25, 2019 10:59:32 上午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 10:59:32 CST 2019]; root of context hierarchy
一月 25, 2019 10:59:32 上午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring.xml]
Phone{properties={name=zhangsan, url=jdbc:oracle:thin:@localhost:1521:orcl, initSize=1, user=root, pwd=root, gender=gender, maxSize=3, age=age, driver=oracle.jdbc.driver.OracleDriver}}一月 25, 2019 10:59:33 上午 org.springframework.context.support.ClassPathXmlApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@7c53a9eb: startup date [Fri Jan 25 10:59:32 CST 2019]; root of context hierarchy
Process finished with exit code 0
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決spring cloud zuul與nginx的域名轉(zhuǎn)發(fā)問(wèn)題
這篇文章主要介紹了spring cloud zuul與nginx的域名轉(zhuǎn)發(fā)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07Spring實(shí)戰(zhàn)之搜索Bean類操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之搜索Bean類操作,結(jié)合實(shí)例形式分析了Spring搜索Bean類的相關(guān)配置、接口實(shí)現(xiàn)與操作技巧,需要的朋友可以參考下2019-12-12JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了JDBC實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02詳解SpringBoot如何優(yōu)雅的進(jìn)行前后端通信
現(xiàn)在的項(xiàng)目基本上都是前后端分離的項(xiàng)目,如何打通前后端,接收前端傳過(guò)來(lái)的參數(shù)呢,下面小編就來(lái)和大家詳細(xì)介紹一下SpringBoot如何優(yōu)雅的進(jìn)行前后端通信2024-03-03淺析Java的Spring框架中IOC容器容器的應(yīng)用
這篇文章主要介紹了Java的Spring框架中IOC容器容器的應(yīng)用,包括BeanFactory容器和ApplicationContext容器的介紹,需要的朋友可以參考下2015-12-12基于<aop:aspect>與<aop:advisor>的區(qū)別
這篇文章主要介紹了<aop:aspect>與<aop:advisor>的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Java連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)方式
文章講述了Java連接MySQL數(shù)據(jù)庫(kù)的詳細(xì)步驟,包括下載和導(dǎo)入JDBC驅(qū)動(dòng)、創(chuàng)建數(shù)據(jù)庫(kù)和表、以及編寫(xiě)連接和讀取數(shù)據(jù)的代碼2024-11-11Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)畫(huà)畫(huà)板
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單畫(huà)畫(huà)畫(huà)板,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06Java工程中使用Mybatis (工程結(jié)合Mybatis,數(shù)據(jù)結(jié)合Swing使用))
這篇文章主要介紹了Java工程中使用Mybatis (工程結(jié)合Mybatis,數(shù)據(jù)可以結(jié)合Swing使用),需要的朋友可以參考下2017-04-04