Spring讀取外部的資源配置文件—@PropertySource和@Value實現資源文件配置


通過@PropertySource可以指定讀取的配置文件,通過@Value注解獲取值;
@PropertySource注解主要是讓Spring的Environment接口讀取屬性配置文件用的,標識在@Configuration配置類上;
@Value注解可以用在字段和方法上,通常用於從屬性配置文件中讀取屬性值,也可以設置默認值。

具體用法:

@PropertySource(value = { "classpath:config.properties" }, ignoreResourceNotFound = true)
public class UserSpringConfig {
    ...
}

a、配置多個配置文件

@PropertySource(value = { "classpath:jdbc.properties", "classpath:config.properties"})
public class UserSpringConfig {
    ...
}

b、忽略不存在的配置文件

@PropertySource(value = { "classpath:jdbc.properties","classpath:config.properties"}, ignoreResourceNotFound = true)
public class UserSpringConfig {
    ...
}

 

資源文件配置示例
1、創建配置文件,${project}/src/main/resources/config.properties

username=zhangsan
password=123456
age=20

 

2、讀取外部的資源配置文件

package com.lynch.javaconfig;

import org.springframework.beans.factory.annotation.Value;

public class User {
    @Value("${username}")
    private String username;
    @Value("${password}")
    private String password;
    @Value("${age}")
    private Integer age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User [username=" + username + ", password=" + password + ", age=" + age + "]";
    }

}

 

3、編寫SpringConfig,用於實例化Spring容器

package com.lynch.javaconfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

//通過@Configuration注解來表明該類是一個Spring的配置,相當於一個xml文件
@Configuration
@ComponentScan(basePackages = "com.lynch.javaconfig")
@PropertySource(value = { "classpath:config.properties"}, ignoreResourceNotFound = true)
public class UserSpringConfig {
  
    @Bean
    public User user(){
        return new User();
    }
    
}

 

4、編寫測試方法,用於啟動Spring容器

package com.lynch.javaconfig;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class UserApplication {

    public static void main(String[] args) {
        // 通過Java配置來實例化Spring容器
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(UserSpringConfig.class);
        
        System.out.println(context.getBean(User.class));
        
        // 銷毀該容器
        context.destroy();
    }

}

 

5、執行結果

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
User [username=Administrator, password=123456, age=20]
九月 16, 2018 9:04:45 下午 org.springframework.context.annotation.AnnotationConfigApplicationContext doClose

注意,從執行結果發現username輸出的是"Administrator",而不是"zhangsan",那是因為系統變量跟配置文件存在相同變量時,優先從系統變量獲取,故username輸出的是"Administrator"。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM