自定义配置文件
my-config.properties
1 bfxy.title=bfxy 2 bfxy.name=hello spring boot! 3 bfxy.attr=12345
配置文件类
appconfig.java
1 import org.springframework.beans.factory.annotation.Autowired; 2 import org.springframework.beans.factory.annotation.Value; 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.context.annotation.PropertySource; 6 import org.springframework.core.env.Environment; 7 8 @Configuration 9 @ComponentScan({"com.bfxy.springboot.*"}) //你要进行扫描的包路径 10 @PropertySource("classpath:my-config.properties") //加载指定的配置 11 public class appConfig { 12 13 @Value("${custom.group}") 14 private String customGroup; 15 16 @Autowired 17 private Environment environment; 18 19 @Value("${bfxy.name}") 20 private String name; 21 22 @Value("${bfxy.title}") 23 private String title; 24 25 @Value("${bfxy.attr}") 26 private String attr; 27 28 29 public void output(){ 30 System.err.println("通过@Value注解读取配置: " + this.customGroup); 31 System.err.println("通过Environment对象读取配置: " + environment.getProperty("custom.team")); 32 33 System.err.println("加载自己的配置文件内容"+this.name+","+this.title+","+this.attr); 34 } 35 36 37 }
原文地址:https://www.cnblogs.com/yangyang521/p/10287079.html
时间: 2024-11-12 03:11:14