spring读取properties文件
配置文件中加载properties文件
标签,可以用来加载properties配置文件,location是配置文件的路径。
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8
<context:property-placeholder location="classpath:mysql.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
...
</bean>
代码中取properties文件中的值
使@Value注解取值。
task.cron.clean_token=0 0 3 * * ?
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="appProperty"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<array>
<value>classpath:config/property/task_schedule.properties</value>
</array>
</property>
</bean>
<!-- 或者用下面的简写 -->
<!--<context:property-placeholder-->
<!--location="classpath:config/property/task_schedule.properties"-->
</beans>
public class TokenClearJob implements SchedulingConfigurer {
@Value("${task.cron.clean_token}")
private String TASK_CRON_CLEAN_TOKEN;
...
}