在src目录下,新建test.properties配置文件,内容如下

name=root
password=123456
logArchiveCron=0/5 * * * * ? 

一种是使用spring提供的一个标签,在spring-config.xml中配置单个properties,如下

<context:property-placeholder location="classpath:test.properties"/>

配置多个properties通过分号隔开在后面添加即可,例如

<context:property-placeholder location="classpath:test.properties,classpath:jdbc.properties"/>

另一种是通过注到bean的属性来进行配置,这里也是配置多个,如下

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:test.properties</value>
            </list>
        </property>
</bean> 

取值方式如下:

在spring-config.xml中获取properties文件中的值:

<task:scheduled-tasks>
  <task:scheduled ref="testJob" method="logArchiveBak" cron="${logArchiveCron}" />
</task:scheduled-tasks>

在java类里面获取properties文件里面的值,通过注解@value(${key})来获取

@Value("${name}")
private String name;
@Value("${password}")
private String password;
亲测可用。

相关文章:

  • 2021-12-25
  • 2021-11-15
  • 2022-12-23
  • 2022-12-23
  • 2021-11-03
  • 2022-02-08
猜你喜欢
  • 2022-02-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-16
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案