@PropertySourece()
my.properties
UserService.java
添加@PropertySource(“my.properties”)指定配置文件 引用@Value(“${name}”)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package cn.kil.springboot_parameter.service;
import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service;
@Service @PropertySource("my.properties") public class UserService {
@Value("${name}") private String name;
public void test(){ System.out.println(name); } }
|
SpringbootParameterApplication.java #Spring启动类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package cn.kil.springboot_parameter;
import cn.kil.springboot_parameter.service.UserService; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.PropertySource;
@SpringBootApplication public class SpringbootParameterApplication {
public static void main(String[] args) { ConfigurableApplicationContext applicationContext = SpringApplication.run(SpringbootParameterApplication.class, args); UserService bean = applicationContext.getBean(UserService.class); bean.test(); } }
|
@ConfigurationProperties
@ConfigurationProperties 让开发者将整个配置文件,自动映射到对象中,比@Value 效率更高
需要添加@Component注册bean
属性:
MyProperties.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package cn.kil.springboot_parameter.properties;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
@Component @ConfigurationProperties(prefix = "kil") public class MyProperties {
private String name;
public String getName() { return name; }
public void setName(String name) { this.name = name; } }
|
UserService.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package cn.kil.springboot_parameter.service;
import cn.kil.springboot_parameter.properties.MyProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;
@Service public class UserService {
@Autowired private MyProperties myProperties;
public void test(){ System.out.println(myProperties.getName()); } }
|
application.properties
- 标准用法 配置类**@EnableConfigurationProperties(MyProperties.class)代替@Component**。方便添加条件,可设置多个配置文件
AppConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import cn.kil.springboot_parameter.properties.MyProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Configuration;
@Configuration @EnableConfigurationProperties(MyProperties.class) public class AppConfig {
}
|
- @ConfigurationPropertiesScan(“cn.kil.springboot_parameter.properties”) //扫描包下的配置文件
AppConfig.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package cn.kil.springboot_parameter;
import org.springframework.boot.context.properties.ConfigurationPropertiesScan; import org.springframework.context.annotation.Configuration;
@Configuration @ConfigurationPropertiesScan("cn.kil.springboot_parameter.properties") public class AppConfig {
}
|
文件目录信息