开发者

spring学习之util:properties的使用

目录
  • 1、手动配置内容
  • 2、通过读取文件获取内容
  • 3、手动配置内容和读取文件一起使用
  • 总结

<util:properties>可用于注入Properties对象,也可以用于读取properties文件

1、手动配置内容

<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、通过读取文件获取内容

<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 = propertie编程客栈s;
    }
 
    @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\Jphpava\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\jrewww.devze.comlib\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:\Programphp 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、手动配置内容和读取文件一起使用

<util:properties id="properties" location="classpath:db.properties">
    <prop key="name">zhangsan</prop>
    &l编程t;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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程客栈(www.devze.com)。

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜