【spring Boot】spring boot獲取資源文件的三種方式【兩種情況下】


首先聲明一點,springboot獲取資源文件,需要看是

  1》從spring boot默認的application.properties資源文件中獲取

  2》還是從自定義的資源文件中獲取

 

帶着這個想法去看下面幾種方式

===============================================================================================

1》從spring boot默認的application.properties資源文件中獲取

先給出來application.properties的內容

#方式1
com.sxd.type1 = type1
com.sxd.title1 = 使用@ConfigurationProperties獲取配置文件

#方式2
com.sxd.type2 = type2
com.sxd.title2 = 使用@Value獲取配置文件

#方式3
com.sxd.type3 = type3
com.sxd.title3 = 使用Environment獲取資源文件

#map
com.sxd.login[username] = sxd
com.sxd.login[password] = admin123
com.sxd.login[callback] = http://www.cnblogs.com/sxdcgaq8080/

#list
com.sxd.comList[0] = com1
com.sxd.comList[1] = com2
com.sxd.comList[2] = com3
View Code

 

①===第一種方式:使用@ConfigurationProperties獲取配置文件

先搞一個綁定資源文件的bean

注意屬性名和資源文件中的屬性名相一致。

package com.sxd.beans;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@ConfigurationProperties(prefix = "com.sxd")
//@PropertySource("classpath:/application.properties")
//不用這個注解,默認就是加載application.properties資源文件
public class User {

    private String type1;
    private String title1;

    private Map<String,String> login = new HashMap<>();
    private List<String> comList = new ArrayList<>();

    public String getType1() {
        return type1;
    }

    public void setType1(String type1) {
        this.type1 = type1;
    }

    public String getTitle1() {
        return title1;
    }

    public void setTitle1(String title1) {
        this.title1 = title1;
    }

    public Map<String, String> getLogin() {
        return login;
    }

    public void setLogin(Map<String, String> login) {
        this.login = login;
    }

    public List<String> getComList() {
        return comList;
    }

    public void setComList(List<String> comList) {
        this.comList = comList;
    }
}
View Code

然后在啟動類中使用

package com.sxd.secondemo;

import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication {


    @Autowired
    User user;


    @RequestMapping("/")
    public String hello(){
        user.getLogin().forEach((k,v)->{
            System.out.println("map的鍵:"+k+">>map的值:"+v);
        });

        user.getComList().forEach(i->{
            System.out.println("list的值:"+i);
        });

        return user.getType1()+user.getTitle1();
    }

    public static void main(String[] args) {
        SpringApplication.run(SecondemoApplication.class, args);
    }
}
View Code

 

結果如下:

控制台打印:

訪問地址:

 

②===第二種方式:使用@Value獲取配置文件

這里不用搞一個綁定資源文件的bean了。

只需要在你想用的地方使用@Value調用你想要的屬性名對應的值即可。

package com.sxd.secondemo;

import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class SecondemoApplication {

    @Value("${com.sxd.type2}")
    private String type;

    @Value("${com.sxd.title2}")
    private String title;


    @RequestMapping("/")
    public String hello(){
        return type+title;
    }

    public static void main(String[] args) {
        SpringApplication.run(SecondemoApplication.class, args);
    }
}
View Code

 

訪問結果:

 

③===第三種方式:使用Environment獲取資源文件

也不用提前做什么使用,Environment就是一個全局的資源池,application.properties中的屬性值都可以從這里獲取到。

package com.sxd.secondemo;

import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class SecondemoApplication {

    @Autowired
    Environment environment;

    @RequestMapping("/")
    public String hello(){
        return environment.getProperty("com.sxd.type3")+environment.getProperty("com.sxd.title3");
    }

    public static void main(String[] args) {
        SpringApplication.run(SecondemoApplication.class, args);
    }
}
View Code

 

運行結果:

================================================================================================

2》從自定義的資源文件中獲取屬性值

①===第一種方式:使用@ConfigurationProperties獲取配置文件

 自定義資源文件如下:

然后指定綁定自定義資源文件

package com.sxd.beans;


import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;


@Component
@ConfigurationProperties(prefix = "com.sxd")
@PropertySource("classpath:/test.properties")
//需要用這個注解,默認就是加載application.properties資源文件,替換@ConfigurationProperties取消location屬性的效果
public class User {

    private String type1;
    private String title1;


    public String getType1() {
        return type1;
    }

    public void setType1(String type1) {
        this.type1 = type1;
    }

    public String getTitle1() {
        return title1;
    }

    public void setTitle1(String title1) {
        this.title1 = title1;
    }


}
View Code

 

最后在啟動類中使用一下

package com.sxd.secondemo;

import com.sxd.beans.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
@EnableConfigurationProperties(User.class)
public class SecondemoApplication {

    @Autowired
    User user;

    @RequestMapping("/")
    public String hello(){
        return user.getType1()+user.getTitle1();
    }

    public static void main(String[] args) {
        SpringApplication.run(SecondemoApplication.class, args);
    }
}
View Code

 

 

運行結果:

 

②===第二種方式:使用@Value獲取配置文件

先設定一個自定義資源文件

如下,自定義資源文件需要滿足application-{profile}.properties格式

 

然后在application.properties文件中指明加載這個資源文件

spring.profiles.active=test
#spring.profiles.include=test

這兩種哪種都可以加載上自定義的資源文件,后面的test就是上面{profile}的值

 

 最后在啟動類中使用@Value獲取自定義資源文件中的屬性,這個時候自定義的資源文件已經在application,properties文件中被指明要被加載了,因此是可以被獲取到的

package com.sxd.secondemo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class SecondemoApplication {

    @Value("${com.sxd.type2}")
    private String type;
    @Value("${com.sxd.title2}")
    private String title;

    @RequestMapping("/")
    public String hello(){
        return type+title;
    }

    public static void main(String[] args) {
        SpringApplication.run(SecondemoApplication.class, args);
    }
}
View Code

 

運行結果:

③===第三種方式:使用Environment獲取資源文件

 

 首先還是寫一個自定義的資源文件,文件命名同上面第二種方式一樣

接着,在application.properties中聲明加載這個自定義的資源文件

最后在啟動類中,也就是哪里使用就在那里自動注入Environment.

package com.sxd.secondemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@SpringBootApplication
public class SecondemoApplication {

    @Autowired
    Environment environment;

    @RequestMapping("/")
    public String hello(){
        return environment.getProperty("com.sxd.type3")+environment.getProperty("com.sxd.title3");
    }

    public static void main(String[] args) {
        SpringApplication.run(SecondemoApplication.class, args);
    }
}
View Code

 

 

運行結果:

 

==================================================================================================================

===================================================完成============================================================

 


免責聲明!

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



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