简介
本文就 Spring Boot 的配置装配实现方式做了介绍,主要是常用的模式注解、@EnableXXX注解、条件注解和自动装配是如何实现的。
手机用户请
横屏
获取最佳阅读体验,REFERENCES
中是本文参考的链接,如需要链接和更多资源,可以关注其他博客发布地址。
平台 | 地址 |
---|---|
CSDN | https://blog.csdn.net/sinat_28690417 |
简书 | https://www.jianshu.com/u/3032cc862300 |
个人博客 | https://yiyuery.github.io/NoteBooks/ |
正文
参考实现
参考
EnableWebMvc
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(HelloWorldConfig.class)
public @interface EnableHello {
}
@Configuration
public class HelloWorldConfig {
@Bean
public World world(){
return new World();
}
}
@SpringBootApplication
@EnableHello
public class BootAutoConfigApplication {
public static void main(String[] args) {
SpringApplication.run(BootAutoConfigApplication.class, args);
}
}
@SpringBootTest
public class BootAutoConfigTest {
@Autowired
private World world;
@Test
public void say2World() {
System.out.println(world.say());
}
}
say2World 输出
.
实现
ImportSelector
接口,根据判断条件导入配置类,参考@EnableCaching注解
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({HelloConfigSelector.class})
public @interface EnableHelloSelector {
boolean isMac() default false;
}
//选择器实现
public class HelloConfigSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
MultiValueMap<String, Object> metaData = importingClassMetadata.getAllAnnotationAttributes(EnableHelloSelector.class.getName());
Boolean isMac = (Boolean) metaData.get("isMac").get(0);
if (isMac) {
return new String[]{HelloWorldConfig.class.getName()};
}else{
return new String[]{HelloWorldConfig.class.getName(),HelloFutureConfig.class.getName()};
}
}
}
@Configuration
public class HelloWorldConfig {
@Bean
public World world(){
return new World();
}
}
@Configuration
public class HelloFutureConfig {
@Bean
public Future future(){
return new Future();
}
}
@SpringBootApplication
@EnableHelloSelector
public class BootAutoConfigApplication {
public static void main(String[] args) {
SpringApplication.run(BootAutoConfigApplication.class, args);
}
}
@SpringBootTest
public class BootAutoConfigTest {
@Autowired
private World world;
@Test
public void say2World() {
System.out.println(world.say());
}
@Autowired
private Future future;
@Test
public void say2FutureAndWorld() {
System.out.println(world.say());
System.out.println(future.say());
}
}
say2FutureAndWorld 输出
.
@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Conditional(OnSystemCondition.class)
public @interface ConditionOnSystem {
String system();
}
public class OnSystemCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> metaDataMap = metadata.getAnnotationAttributes(ConditionOnSystem.class.getName());
return "windows".equals(metaDataMap.get("system"));
}
}
@Configuration
@Slf4j
public class HelloFutureConfig {
@Bean
public Future future(){
log.info("future bean init");
return new Future();
}
@ConditionOnSystem(system = "windows")
@Bean
public Future future2(){
log.info("future bean init for windows");
return new Future();
}
@ConditionOnSystem(system = "mac")
@Bean
public Future future3(){
log.info("future bean init for mac");
return new Future();
}
}
@SpringBootApplication
@EnableHelloSelector
public class BootAutoConfigApplication {
public static void main(String[] args) {
SpringApplication.run(BootAutoConfigApplication.class, args);
}
}
.
此处同时使用了@Enable自定义装配
和条件装配
@Configuration
@Import({HelloWorldConfig.class,HelloFutureConfig.class})
public class HelloAutoConfiguration {
}
META-INF/spring.factories
中填写自动装配类信息.
@SpringBootApplication
//@EnableHelloSelector
//@EnableHello
public class BootAutoConfigApplication {
public static void main(String[] args) {
SpringApplication.run(BootAutoConfigApplication.class, args);
}
}
.
比对可以看出,输出结果和@EnableHelloSelector
实现的效果是一致的。
Spring Boot 遵从约定大于配置的规则,提供了很多种方式来实现手动装配和自动装配
.
.
扫码关注
架构探险之道
,回复『源码』,获取本文相关源码和资源链