由于需要抽取公共方法,提取第三方路径,由于配置抽取进依赖包中不便每个项目进行配置,所以需要将环境配置一起打入依赖包中,做到开箱即用。
@RestController
@SpringBootApplication
public class Application {
@Autowired
private ApplicationContext context;
public static void main(String[] args) {
SpringApplication.run(DbAdminApplication.class, args);
}
@GetMapping("hello")
public String hello() {
StandardServletEnvironment environment = (StandardServletEnvironment) context.getEnvironment();
MutablePropertySources propertySources = environment.getPropertySources();
Map<String, Object> map = new HashMap<>();
map.put("testKey","testValue");
propertySources.addFirst(new MapPropertySource("myProperties", map));
String testKey= environment.getProperty("testKey");
System.out.println(testKey);
return testKey;
}