閱讀99 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Spring項目配置管理

配置相關

常見的幾種配置方式和優先級說明見下麵所示:

a. 命令行參數

b. 來自java:comp/env的JNDI屬性

b. Java係統屬性System.getProperties()

d. 操作係統環境變量

e. RandomValuePropertySource配置的random.*屬性值

f. jar外部的application-{profile}.properties或application.yml(帶spring.profile)配置文件

g. jar內部的application-{profile}.properties或application.yml(帶spring.profile)配置文件

h. jar外部的application.properties或application.yml(不帶spring.profile)配置文件

i. jar內部的application.properties或application.yml(不帶spring.profile)配置文件

j. @Configuration注解類上的@PropertySource

k. 通過SpringApplication.setDefaultProperties指定的默認屬性

命令行參數

@Component
public class UserProperties {  
    @Value("${myname}")  
    private  String myname;
}

啟動命令: java -jar *.jar --myname="KevinSun"
其他java 係統屬性,操作係統屬性都是類似處理方案如果不想通過命令行啟動,可以用下麵方法取消。

SpringApplication.setAddCommandLineProperties(false);

application-{profile}.properties參數加載

不同的環境可能需要不同配置,可以通過
application-{profile}.properties來解決這個問題.
首先:新建application-dev.properties 文件,增加需要參數啟動命令:

java -jar ***.jar --spring.profiles.active=dev
application-{profile}.properties 文件和默認的application.properties的加載方式本質是一致的,可以參照4中的內容.需要注意的是:application.properties 會首先被加載.

然後:從application-{profile}.properties中獲取替換,所以一些基本固定的值可以寫在application.properties, 然後個性化配置寫在application-{profile}.properties中。

application.properties 參數加載

SpringBoot中默認的從application.properties文件中加載參數,大量情況下默認是寫在這個文件中的.
a. application.properties 讀取的優先級
i. file:./config/ 當前jar目錄的config
ii. file:./ 當前jar目錄
iii. classpath:/config/ jar包中classpath的 config目錄下
iv. classpath:/ jar包中classpath 路徑下
排列的順序 就是 加載的優先級,application.properties隻會被加載一次

使用自定義文件

如果你覺得 application.properties不夠酷,你可以定義自己的文件名, 這裏也有兩種方式
i. 使用命令

java -jar ***.jar --spring.config.name=myproject
這時就會加載myproject.propertie並且 application.properties不會被加載的.
ii. 使用命令

java -jar ***.jar spring.config.location=classpath:/myproject.properties
這種情況下 application.properties也是會被加載,使用方式application-{profile}.properties相同.

使用${}進行屬性占位符替換

spring.data.mongodb.host=192.168.1.1
spring.data.mongodb.port=1234
spring.data1.mongodb.host=${spring.data.mongodb.host}_test
spring.data1.mongodb.port=${spring.data.mongodb.port}_test
spring.data1.mongodb.database=${random.value}_test
注意最後一個配置,這使用random 生產隨機值的方式, 在測試中可以用來造數據。

類型安全加載

使用@value 進行屬性的注入有的時候可能比較笨重, spring 提供一種強類型的bean 來替代這種方式

@Configuration
@EnableConfigurationProperties(UserProperties.class)
public class MyConfiguration{

}

@Component
@ConfigurationProperties(prefix = "spring.data.mongodb")
public class UserProperties {
private String host;
private int prot;

}
spring.data.mongodb.host=192.168.1.1
spring.data.mongodb.port=1234

使用application.yaml 參數加載

YAML文檔也可以比較好的被支持, YamlPropertiesFactoryBean會將yaml文件當初properties來解析的。

spring:
data.mongodb:
host:192.168.1.1
port:27017
但是YAML文件不能通過@PropertySource注解加載. 所以,在這種情況下,如果需要使用@PropertySource注解的方式加載值,那就要使用properties文件。

注解配置

  • @Value
  • @Configuration
  • @Bean
  • @AutoConfiguration
  • @Profile
  • @Conditional
  • @ConfigurationProperties

Value

使用Value可以為類的屬性注入初始值,包括注入普通字符、注入操作係統屬性、注入表達式結果、注入其他Bean的屬性、注入文件內容、注入網址、注入屬性文件

Bean

@Bean用於方法中,@Bean(initMethod="init", destroyMethod="destroy")

Profile

一般和@Bean一起使用,在不同的profile下使用不同的配置類,profile可以通過context.getEnvironment.setActiveProfiles("prod");來設置,通常的profile有dev、local、prod、online。

Conditional

在特定條件下創建特定的Bean。使用時實現Condition接口,創建條件類

public class WindowsCondition implements Condition {
    //實現matches方法
}

public class LinuxCondition implements Condition {
    //實現matches方法
}

使用時在不同的條件下配置不同的方法:

@Configuration
public class ConditionConfig {

    @Bean
    @Conditional(WindowsCondition.class)
    public OsConfig windowsConfig(){

    }

    @Bean
    @Conditional(WindowsCondition.class)
    public OsConfig linuxConfig(){

    }
}

ConfigurationProperties

一般和POJO對象一起使用,例如:

@Component
@ConfigurationProperties(prefix="author", locations={"classpath:config/author.properties"})
class Author {
    private String name;
    private int age;

    //getter and setter
}

其他配置

profile配置(多個配置)

application-prod.properties
application.properties
在application.properties中配置spring.profiles.active=prod

Bean相關

  • @Scope
  • @Service
  • @Component
  • @Repository
  • @Controller

Scope

用於描述Spring容器如何創建Bean實例的。

  • Singleton, 默認是單例
  • Prototype
  • Request
  • Session
  • GlobalSession

測試相關

Spring單元測試框架不依賴於任何其他的單元測試框架,可以是Junit也可以支持TestNG。

@RunWith(SpringJunitClassRunner.class)
@ContextConfiguration(Application.class)
@ActiveProfiles("prod")
public DemoClassTest {
    //@Autowired 

}

最後更新:2017-08-13 22:20:42

  上一篇:go  Spring項目HTTPS
  下一篇:go  最新阿裏雲八折推薦碼常見問題解答