spring读取properties文件

小咪咪 2022-06-12 13:46 459阅读 0赞

spring读取properties文件

配置文件中加载properties文件

​ 标签,可以用来加载properties配置文件,location是配置文件的路径。

  1. jdbc.driverClassName=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/test?user=root&password=123456&useUnicode=true&characterEncoding=UTF-8
  3. <context:property-placeholder location="classpath:mysql.properties"/>
  4. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  5. <property name="driverClassName" value="${jdbc.driverClassName}"/>
  6. <property name="url" value="${jdbc.url}"/>
  7. ...
  8. </bean>

代码中取properties文件中的值

​ 使@Value注解取值。

  1. task.cron.clean_token=0 0 3 * * ?
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <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">
  4. <bean id="appProperty" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  5. <property name="locations">
  6. <array>
  7. <value>classpath:config/property/task_schedule.properties</value>
  8. </array>
  9. </property>
  10. </bean>
  11. <!-- 或者用下面的简写 -->
  12. <!--<context:property-placeholder-->
  13. <!--location="classpath:config/property/task_schedule.properties"-->
  14. </beans>
  15. public class TokenClearJob implements SchedulingConfigurer {
  16. @Value("${task.cron.clean_token}")
  17. private String TASK_CRON_CLEAN_TOKEN;
  18. ...
  19. }

发表评论

表情:
评论列表 (有 0 条评论,459人围观)

还没有评论,来说两句吧...

相关阅读