三种方式读取:
(一)
通过Spring Boot带有的Environment 类读取
(二)
通过使用@Value读取
(三)
通过添加一个配置类,使用@ConfigurationProperties读取
使用步骤:
① 添加一个配置类,在类名上面使用@ConfigurationProperties;若读取的配置文件不是默认的application.properties,则需再添加@PropertySource(“xxx.properties”)注解
② 在引导类上面添加;@EnableConfigurationProperties(value=类名.class)
③ 使用;在需要的地方使用@Autowired注入该对象即可
代码:
引导类:
@SpringBootApplication
//方式三需要添加该注解
@EnableConfigurationProperties(MyProperties.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
测试类:
@RestController
public class DemoController {
@Autowired
private Environment environment;
@Value("${spring}")
private Integer password;
&