Spring Boot-properties使用(二)


 

自定義屬性

@value注入

在application.properties新增配置

student.name=小明
student.age=12
student.info=${student.name}${student.age}
package com.liqiang.contorller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloWordContorller {
    @Value("${student.name}")
    private String name;
    @Value("${student.age}")
    private int age;

    @Value("${student.info}")
    private String info;
    @RequestMapping("/helloword")
    public String helloWord(){
        return "姓名:"+name+",年齡:"+age+" 全部信息:"+info;
    }
}

如果輸出亂碼

在properties加上

#返回頁面、數據中文亂碼問題
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding = UTF-8
#解決程序讀配置文件亂碼問題
spring.message.encodiang = UTF-8  
student.name=小明
student.age=12
student.info=${student.name}${student.age}

如果加上之后還是亂碼 修改idea文件編碼格式

 

 

java bean形式注入

需要引入依賴

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

 

如果自定義屬性很多 10幾個字段 需要一個一個注入太麻煩了,可以使用java bean的形式

@Component
@ConfigurationProperties(prefix = "student")//會找配置文件student前綴  后綴為屬性名
public class StudentConfig {

    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
@RestController
public class HelloWordContorller {
    @Autowired
   private StudentConfig studentConfig;


    @RequestMapping("/helloword")
    public String helloWord(){
        return "姓名:"+studentConfig.getName()+",年齡:"+studentConfig.getAge();
    }
}

所有配置都配置在appliction.properties 顯得太亂了。

我們可以新建studentConfig.properties

然后指定加載的文件

@Component
@ConfigurationProperties(prefix = "student")
@PropertySource("classpath:studentconfig.properties") public class StudentConfig

 

內置Random配置

${random.value}
${random.int}
${random.long}
${random.uuid}
${random.int(10)}
${random.int[1024,65536]}

多環境配置

properties形式

比如正式環境   測試環境  開發環境  不同環境的 數據庫不一樣 redis  mq等不一樣  當我們開發時使用開發環境 測試使用測試環境

application-dev.properties 開發(配置了 開發數據庫 mq redis等信息)
application-online.properties  線上(配置了 線上數據庫 mq redis等信息)
application-test-propertie 測試環境(配置了測試數據庫 mq redis等信息)

在appliction.properties使用spring.profiles.active切換對應的環境

如需要使用開發環境

spring.profiles.active=dev

發布到線上

spring.profiles.active=online

也可用通過

spring.profiles.include=datasource,prodmq  來進行不同配置的疊加

javaconfig形式

比如模擬正式與開發的數據源切換

1.創建一個數據源接口

public interface DataSource {
   public String  getConnection();
}

2.oracle數據源實現類

public class OracleDataSource implements DataSource {
    @Override
    public String getConnection() {
        return "oracle數據源";
    }
}

3.創建mysql數據源

public class MysqlDataSource implements DataSource {

    @Override
    public String getConnection() {
     return "mysql數據源";
    }
}

4.創建DataSourceConfig

@Configuration
public class DataSourceConfig {
    @Bean
    @Profile("online")
    public DataSource createMysqlDataSource(){
        return  new MysqlDataSource();
    }
    @Bean
    @Profile("dev")
    public DataSource createOracleDataSource(){
        return  new OracleDataSource();
    }
}

5.contorller

@RestController
public class HelloWordContorller {
    @Autowired
    private DataSource dataSource;


    @RequestMapping("/helloword")
    public String helloWord(){
        return dataSource.getConnection();
    }
}

當我們配置

spring.profiles.active=dev

頁面輸出

改成

spring.profiles.active=online
頁面輸出

我們這里只是模擬並不是真正的創建數據源。

注意:不要認為這個功能是springboot提供的。而是spring提供的

spring提供幾種裝配模式

一種是xml形式   就是我們以前傳統用的 配置數據源

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="${url}" />
        <property name="username" value="${username}" />
        <property name="password" value="${password}" />
        <property name="initialSize" value="5" />
    </bean>

通過配置spring掃描到xml會根據配置內容初始化com.alibaba.druid.pool.DruidDataSource類的對象 裝載到容器里面

還有就是javaconfig模式(也是springboot推薦的)

@Configuration
public class DataSourceConfig {
    @Beanpublic DataSource createMysqlDataSource(){
        DruidDataSource druidDataSource=new DruidDataSource();
        druidDataSource.setUrl("");
        druidDataSource.setUsername("");
        druidDataSource.setPassword("");
        return druidDataSource;
    }
}

spring 會掃描包下面打了Configuration的類 調用打上了@Bean的方法 將返回值注入容器

 @Profile("dev")注解也是spring提供。根據不同的環境初始化對應的@Bean的方法 實現不同環境配置的切換

 

 

 
        


免責聲明!

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



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