【金蝶天燕鲲鹏云最佳实践 三】springboot源码浅析(二)spring的模块装配
一、springboot的模块装配
什么是模块?
模块定义:Spring Framework 3.1 开始支持”@Enable 模块驱动“。所谓“模块”是指具备相同领域的功能组件集合, 组合所形成一个独立
的单元。比如 Web MVC 模块、AspectJ代理模块、Caching(缓存)模块、JMX(Java 管 理扩展)模块、Async(异步处
理)模块等。
Spring相关模装配举例 @EnableWebMvc WebMvc模块 @EnableTransationManagement 事物管理模块 @EnableCaching Caching模块 @EnableMBeanExport JMX模块 |
SpringBoot相关模装配举例 @EnableAutoConfiguration 自动装配模块 @EnableManagementContext Actuator 管理模块 @EnableConfigurationProperties 配置属性绑定模块 @EnableOAuth2 OAuth2单点登录模块 |
SpringCloud相关模装配举例 @EnableEurekaServer Eureka服务器模块 @EnableConfigServer 配置服务模块 @EnableFeignClients Feign客户端模块 @EnableZuulProxy 服务网管Zuul模块 @EnableCircuitBreaker 服务熔断模块 |
创建自己的模块注解:
我们通过自己实现一个自定义的模块注解,来加深对模块注解的理解。
实现模块注解有2中方式:
1、import具体实现类——ApusicConfiguration.class。
2、import ImportSelector接口的具体实现类——ApusicConfigImportSelector
第二种方式可以在ImportSelector接口的具体实现类中添加判断逻辑
第一种方式代码:EnableApusicConfig.java
package com.apusic.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Import; import com.apusic.config.ApusicConfiguration; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented //通过ImportSelector的实现类,进行自己的逻辑判断 //@Import(ApusicConfigImportSelector.class) //指向具体的实现类 @Import(ApusicConfiguration.class)// public @interface EnableApusicConfig { }
第一种方式代码:ApusicConfiguration.java
package com.apusic.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //EnableApusicConfig注解具体实现类 @Configuration public class ApusicConfiguration { @Bean public String helloApusicString() { return "hello,ApusicConfig"; } }
第一种方式代码:ConfigurationApusicTest.java
package com.apusic.test; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; import com.apusic.annotation.EnableApusicConfig; @EnableApusicConfig public class ConfigurationApusicTest { public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(ConfigurationApusicTest.class) .web(WebApplicationType.NONE) .run(args); String bean = context.getBean("helloApusicString",String.class); System.out.println(bean); context.close(); } }
第二种方式代码:EnableApusicConfig.java
package com.apusic.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import org.springframework.context.annotation.Import; import com.apusic.config.ApusicConfiguration; @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented //通过ImportSelector的实现类,进行自己的逻辑判断 //@Import(ApusicConfigImportSelector.class) //指向具体的实现类 @Import(ApusicConfiguration.class)// public @interface EnableApusicConfig { }
第二种方式代码:ApusicConfigImportSelector.java
package com.apusic.annotation; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; import com.apusic.config.ApusicConfiguration; import com.apusic.config.WbmConfiguration; public class ApusicConfigImportSelector implements ImportSelector{ @Override public String[] selectImports(AnnotationMetadata importingClassMetadata) { System.out.println("111111111111111111111"+importingClassMetadata.getClassName()); if(importingClassMetadata.getClassName().equals("com.apusic.test.ConfigurationApusicTest")) { return new String[] {ApusicConfiguration.class.getName()}; } else if(importingClassMetadata.getClassName().equals("com.apusic.test.ConfigurationWbmTest")){ return new String[] {WbmConfiguration.class.getName()}; } return null; } }
第二种方式代码:WbmConfiguration.java
package com.apusic.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //EnableApusicConfig注解具体实现类 @Configuration public class ApusicConfiguration { @Bean public String helloApusicString() { return "hello,ApusicConfig"; } }
第二种方式代码:WbmConfiguration.java
package com.apusic.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class WbmConfiguration { @Bean public String helloWBMApusicString() { return "hello,WbmConfig"; } }
第二种方式代码:ConfigurationApusicTest.java
package com.apusic.test; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; import com.apusic.annotation.EnableApusicConfig; @EnableApusicConfig public class ConfigurationApusicTest { public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(ConfigurationApusicTest.class) .web(WebApplicationType.NONE) .run(args); String bean = context.getBean("helloApusicString",String.class); System.out.println(bean); context.close(); } }
第二种方式代码:ConfigurationWbmTest.java
package com.apusic.test; import org.springframework.boot.WebApplicationType; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.ConfigurableApplicationContext; import com.apusic.annotation.EnableApusicConfig; @EnableApusicConfig public class ConfigurationWbmTest { public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(ConfigurationWbmTest.class) .web(WebApplicationType.NONE) .run(args); String bean = context.getBean("helloWBMApusicString",String.class); System.out.println(bean); context.close(); } }
总结:
1、import具体实现类——ApusicConfiguration.class。
我们通过Import指向具体的注解实现类,进行bean的装载
2、import ImportSelector接口的具体实现类——ApusicConfigImportSelector
我们通过Import指向一个ImportSelector接口的实现类,在里面进行判断处理,判断什么情况下装载哪些bean。
@EnableXXX注解能够简化配置,但是还是需要我们写@EnableXXX注解,并且需要通过import来指向具体的实现类。后面介绍的springboot自动装配,会告诉大家springboot是如何完成自动装配的。
- 点赞
- 收藏
- 关注作者
评论(0)