zoukankan      html  css  js  c++  java
  • Spring的@PropertySource注解使用

    @PropertySource注解是Spring用于加载配置文件,默认支持.properties.xml两种配置文件。@PropertySource属性如下:

    • name:默认为空,不指定Spring自动生成
    • value:配置文件
    • ignoreResourceNotFound:没有找到配置文件是否忽略,默认false,4.0版本加入
    • encoding:配置文件编码格式,默认UTF-8 4.3版本才加入
    • factory:配置文件解析工厂,默认:PropertySourceFactory.class 4.3版本才加入,如果是之前的版本就需要手动注入配置文件解析Bean

    接下来就使用@PropertySource来加载.properties.xml配置文件。这里模拟连接MySQL数据库。
    首先添加依赖:

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.1.6.RELEASE</version>
    </dependency>
    
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.26</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.20</version>
    </dependency>

    准备属性配置文件jdbc.properties

    jdbc.driver=com.mysql.cj.jdbc.Driver
    jdbc.url=jdbc:mysql://127.0.0.1:3306
    jdbc.userName=root
    jdbc.password=xiaohu

    创建属性实体类来加载配置文件JdbcProperties

    @Data
    @Repository
    @PropertySource(value = "classpath:jdbc.properties")
    public class JdbcProperties {
        @Value("${jdbc.driver}")
        private String driver;
        @Value("${jdbc.url}")
        private String url;
        @Value("${jdbc.userName}")
        private String userName;
        @Value("${jdbc.password}")
        private String password;
    }

    https://www.cnblogs.com/tenghu/p/15159414.html

    故乡明
  • 相关阅读:
    Android fill_parent和wrap_content分析
    美亚退保
    房子
    回家看
    Interface小例子
    做网页 推荐
    转发;Dota英文名
    【转】meta标签中的http-equiv属性使用介绍
    【转】php 操作数组(合并,拆分,追加,查找,删除等)
    【转】服务器.htaccess 详解以及 .htaccess 参数说明
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/15203240.html
Copyright © 2011-2022 走看看