码迷,mamicode.com
首页 > 编程语言 > 详细

在Spring中读取properties文件

时间:2017-10-15 14:27:14      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:ssi   log   col   val   coding   localhost   res   odi   images   

1、配置文件(*.properties)往往通过以下方式注册在Spring IOC中。

    <!-- JDBC配置 -->
    <context:property-placeholder location="classpath:mybatis/db.properties"
        ignore-unresolvable="true" />

    <!-- 微信公众号信息 -->
    <context:property-placeholder
        location="classpath:wechat/official-account.properties"
        ignore-unresolvable="true" />

    <!-- 触发器表达式 -->
    <context:property-placeholder
        location="classpath:wechat/cron-expression.properties"
        ignore-unresolvable="true" />

    <!-- 其他配置(分页尺寸、访问频率等) -->
    <context:property-placeholder
        location="classpath:wechat/other.properties"
        ignore-unresolvable="true" />

 

2、在控制层或业务层的类中,通过@Value注解引入其中的某个属性。

/src/main/resources/mybatis/db.properties

# db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8&useUnicode=true&useSSL=false&allowMultiQueries=true
jdbc.username=root
jdbc.password=root

 

io.spldeolin.demo.controller.OneController

@Controller
public class DepartmentController {

    @Value("${jdbc.password}")
    private String a;

    // Others ignore

}

 

DEBUG

技术分享

 

在Spring中读取properties文件

标签:ssi   log   col   val   coding   localhost   res   odi   images   

原文地址:http://www.cnblogs.com/deolin/p/7670360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!