定义start子项目
项目结构
配置自动注册扫描
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.start_template.autoconfig.HelloAutoConfiguration
配置类转换:
@ConfigurationProperties(prefix = CustomProperties.CUSTOM_PROFILE_PREFIX)
public class CustomProperties {
public static final String CUSTOM_PROFILE_PREFIX = "custom.profile";
private Map<String, Object> info;
public Map<String, Object> getInfo() {
return info;
}
public void setInfo(Map<String, Object> info) {
System.out.println("CustomProperties-setInfo" + info.toString());
this.info = info;
}
}
选择注入实现:
@Configuration
public class FormatAutoConfiguration {
//没有指定的包则进行注入
@ConditionalOnMissingClass("com.alibaba.fastjson.JSON")
@Bean
@Primary
public FormatProcessor stringFormat(){
return new StringFormatProcessor();
}
//存在指定的包则进行注入
@ConditionalOnClass(name = "com.alibaba.fastjson.JSON")
@Bean
public FormatProcessor jsonFormat(){
return new JsonFormatProcessor();
}
}
待扫描类:
@Import(FormatAutoConfiguration.class)
@EnableConfigurationProperties(CustomProperties.class)
@Configuration
public class HelloAutoConfiguration {
@Bean
public HelloFormatTemplate helloFormatTemplate(CustomProperties customProperties, FormatProcessor formatProcessor) {
return new HelloFormatTemplate(customProperties,formatProcessor);
}
}
另一项目引入依赖,完成调用:
#自定义配置文件
custom.profile.info.key1=value1
custom.profile.info.key2=value1
custom.profile.info.key3=value1
custom.profile.info.key4=value1
@RestController
public class TestController {
@Autowired
private HelloFormatTemplate helloFormatTemplate;
@RequestMapping("/get/info")
public String getMessage(){
return helloFormatTemplate.getCustomPropertiesString();
}
}