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

    故乡明
  • 相关阅读:
    C# 文件类的操作---删除
    C#实现Zip压缩解压实例
    UVALIVE 2431 Binary Stirling Numbers
    UVA 10570 meeting with aliens
    UVA 306 Cipher
    UVA 10994 Simple Addition
    UVA 696 How Many Knights
    UVA 10205 Stack 'em Up
    UVA 11125 Arrange Some Marbles
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/luweiweicode/p/15203240.html
Copyright © 2011-2022 走看看