1. 程式人生 > 實用技巧 >Springboot獲取配置屬性的幾種方式

Springboot獲取配置屬性的幾種方式

  需求:獲取配置檔案中的屬性

  application.properties

server.port=8089
server.servlet.context-path=/HelloWorld
user.username=\u5F20\u4E09
user.password=123 spring.profiles.active=dev

  application-dev.properties

mysql.jdbcName=com.mysql.jdbc.Driver
mysql.dbUrl=jdbc:mysql://localhost:3306/db_boot
mysql.userName=root
mysql.password=123456

1、controller或service元件中,直接使用@Value獲取

/**
 * 測試從application.properties中獲取引數
 * 
 * @author oy
 * @version 1.0
 * @date 2018年12月23日
 * @time 下午9:17:54
 */
@Controller
//@RestController 使用該註解後,@ResponseBody註解可以省略
public class HelloWorldController {
    
    /**
     * 注入自定義屬性
     */
    @Value("${user.username}")
    
private String username; @Value("${user.password}") private String password; @RequestMapping("/showUsernamePassword") @ResponseBody public String showUsernamePassword() { return "使用者名稱:" + username + "<br/>" + "密碼:" + password; } }

2、@Component和@Value將屬性封裝成實體類,在使用的類中注入

@Controller
public class HelloWorldController {
    
    @Autowired
    // User類通過@Component掃描成bean,並使用@Value進行普通屬性注入
    private User user;/**
     * 測試:User類通過@Component掃描成bean,並使用@Value進行普通屬性注入
     * 
     * @return
     */
    @RequestMapping("/showUser")
    @ResponseBody
    public String showUser() {
        return "使用者名稱:" + user.getUsername() + "<br/>"
                + "密碼:" + user.getPassword();
    }
    
}

  User實體類

package com.oy.entity;

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

/**
 * @author oy
 * @version 1.0
 * @date 2018年12月23日
 * @time 下午10:17:27
 */
@Component
public class User {
    @Value("${user.username}")
    private String username;
    
    @Value("${user.password}")
    private String password;

    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;
    }

}

3、@Component掃描成bean,並使用@ConfigurationProperties進行普通屬性注入

  使用@ConfigurationProperties進行普通屬性注入。需要新增依賴:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-configuration-processor</artifactId>
    <optional>true</optional>
</dependency>

@Controller
public class HelloWorldController {

    @Autowired
    // MysqlProperties類通過@Component掃描成bean,並使用@ConfigurationProperties進行普通屬性注入
    private MysqlProperties mysqlProperties;
   /**
     * 測試: MysqlProperties類通過@Component掃描成bean,並使用@ConfigurationProperties進行普通屬性注入
     * @return
     */
    @RequestMapping("/showJdbc")
    @ResponseBody
    public String showJdbc() {
        return "驅動類:" + mysqlProperties.getJdbcName() + "<br/>"
                + "URL:" + mysqlProperties.getDbUrl() + "<br/>"
                + "使用者名稱:" + mysqlProperties.getUserName() + "<br/>"
                + "密碼:" + mysqlProperties.getPassword() + "<br/>";
    }
    
}

  MysqlProperties實體類

package com.oy.entity;

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

/**
 * 註解@Component表明本類會被掃描,生成bean
 * 
 * 掃描該元件時,發現有@ConfigurationProperties(prefix = "mysql"),
 * 會結合字首"mysql" + 屬性名去application.properties找對應的值進行注入。
 * 
 * @author oy
 * @version 1.0
 * @date 2018年12月23日
 * @time 下午10:13:56
 */
@Component
@ConfigurationProperties(prefix = "mysql")
public class MysqlProperties {
    private String jdbcName;
    private String dbUrl;
    private String userName;
    private String password;

    public String getJdbcName() {
        return jdbcName;
    }

    public void setJdbcName(String jdbcName) {
        this.jdbcName = jdbcName;
    }

    public String getDbUrl() {
        return dbUrl;
    }

    public void setDbUrl(String dbUrl) {
        this.dbUrl = dbUrl;
    }

    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;
    }
}

4、通過 Environment 獲取

  另外參考:視訊《一起學Beetl》記錄3.1

@Controller
public class HelloWorldController {
  /**
* 注入環境變數
*/
@Value("${USERDOMAIN}")
private String userdomain;

  /** * 瀏覽器輸入:http://localhost:8089/HelloWorld/helloworld * @return */ @RequestMapping("/helloworld") @ResponseBody public String sayHello() { return "hello world!!!" + userdomain; // hello world!!!oy-PC } @Autowired private Environment env; @RequestMapping("/ent") @ResponseBody public String ent() { return env.getProperty("user.username"); } }

---