spring学习之util:properties的使用

 更新时间:2024年01月13日 14:20:00   作者:@Hmily@  
这篇文章主要介绍了spring学习之util:properties的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

<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 = 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、手动配置内容和读取文件一起使用

<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

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 解决spring cloud zuul与nginx的域名转发问题

    解决spring cloud zuul与nginx的域名转发问题

    这篇文章主要介绍了spring cloud zuul与nginx的域名转发问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-07-07
  • Spring实战之搜索Bean类操作示例

    Spring实战之搜索Bean类操作示例

    这篇文章主要介绍了Spring实战之搜索Bean类操作,结合实例形式分析了Spring搜索Bean类的相关配置、接口实现与操作技巧,需要的朋友可以参考下
    2019-12-12
  • JDBC实现学生管理系统

    JDBC实现学生管理系统

    这篇文章主要为大家详细介绍了JDBC实现学生管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • 详解SpringBoot如何优雅的进行前后端通信

    详解SpringBoot如何优雅的进行前后端通信

    现在的项目基本上都是前后端分离的项目,如何打通前后端,接收前端传过来的参数呢,下面小编就来和大家详细介绍一下SpringBoot如何优雅的进行前后端通信
    2024-03-03
  • 浅析Java的Spring框架中IOC容器容器的应用

    浅析Java的Spring框架中IOC容器容器的应用

    这篇文章主要介绍了Java的Spring框架中IOC容器容器的应用,包括BeanFactory容器和ApplicationContext容器的介绍,需要的朋友可以参考下
    2015-12-12
  • 基于<aop:aspect>与<aop:advisor>的区别

    基于<aop:aspect>与<aop:advisor>的区别

    这篇文章主要介绍了<aop:aspect>与<aop:advisor>的区别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-11-11
  • Java连接数据库实现方式

    Java连接数据库实现方式

    文章讲述了Java连接MySQL数据库的详细步骤,包括下载和导入JDBC驱动、创建数据库和表、以及编写连接和读取数据的代码
    2024-11-11
  • Java背包问题求解实例代码

    Java背包问题求解实例代码

    这篇文章主要介绍了Java背包问题求解实例代码,其中涉及两种背包:01和完全背包。分别讲述了两种背包的思路和实现方法,具有一定参考价值,需要的朋友可以了解下。
    2017-10-10
  • Java实现简单画画画板

    Java实现简单画画画板

    这篇文章主要为大家详细介绍了Java实现简单画画画板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Java工程中使用Mybatis (工程结合Mybatis,数据结合Swing使用))

    Java工程中使用Mybatis (工程结合Mybatis,数据结合Swing使用))

    这篇文章主要介绍了Java工程中使用Mybatis (工程结合Mybatis,数据可以结合Swing使用),需要的朋友可以参考下
    2017-04-04

最新评论