注:该系列文章为慕课网springboot2深度实践课程学习记录
什么是模块装配
Spring Framework 3.1 开始支持”@Enable 模块驱动“。所谓“模块”是指具备相同领域的功能组件集合, 组合所形成一个独立
的单元。比如 Web MVC 模块、AspectJ代理模块、Caching(缓存)模块、JMX(Java 管 理扩展)模块、Async(异步处
理)模块等。
做过微服务的童鞋应该对@EnableEurekaServer这个注解不会陌生哈,它表示开启或者说激活该工程作为一个EurekaServer的服务端。这些我们平常都能见到的注解,我们真的了解它吗?
@EnableEurekaServer是一个典型的@Enable 模块装配的实例。我们看下其源码
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import(EurekaServerMarkerConfiguration.class)
public @interface EnableEurekaServer {
}
通过源码我们很清楚的看到,原来这个注解的作用是让spring来导入EurekaServerMarkerConfiguration类。通过这个例子是不是对上面定义有了更深一步的理解了呢!
自己实现代码
工程目录我们看看每个类分别做了什么:
MyEnableEurekaServer:
package com.wangming.annotation;
import com.wangming.config.MyEurekaServerMarkerConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 模拟激活 eurekaserver模块
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
@Import(MyEurekaServerMarkerConfiguration.class)
public @interface MyEnableEurekaServer {
}
MyEurekaServerMarkerConfiguration:
package com.wangming.config;
import org.springframework.context.annotation.Bean;
/**
* 模拟EurekaServerMarkerConfiguration
*/
public class MyEurekaServerMarkerConfiguration {
@Bean
public String hello() { // 方法名即 Bean 名称
return "Hello,2018";
}
}
最后是一个启动类MyEurekaServerBootstrap:注意@MyEnableEurekaServer注解。
package com.wangming.bootstrap;
import com.wangming.annotation.MyEnableEurekaServer;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
@MyEnableEurekaServer
public class MyEurekaServerBootstrap {
public static void main(String[] args) {
ConfigurableApplicationContext context = new SpringApplicationBuilder(MyEurekaServerBootstrap.class)
.web(WebApplicationType.NONE)
.run(args);
String hello = context.getBean("hello", String.class);
System.out.println("hello Bean : " + hello);
// 关闭上下文
context.close();
}
}
我们运行可以发现hello这个bean已经被装配进来了。
我们还可以使用另一个更灵活的方式来进行装配。
我们修改一下MyEnableEurekaServer 这个类:
package com.wangming.annotation;
import com.wangming.config.MyEurekaServerMarkerConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
/**
* 模拟激活 eurekaserver模块
*
* @author 王明
* @since 2018/9/25
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
//@Import(MyEurekaServerMarkerConfiguration.class)
@Import(MyEurekaImportSelector.class)
public @interface MyEnableEurekaServer {
}
MyEurekaImportSelector类实现了ImportSelector这个接口:
package com.wangming.annotation;
import com.wangming.config.MyEurekaServerMarkerConfiguration;
import org.springframework.context.annotation.ImportSelector;
import org.springframework.core.type.AnnotationMetadata;
public class MyEurekaImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{
MyEurekaServerMarkerConfiguration.class.getName()};
}
}
重新启动发现也是同样的效果。这么写有个好处,更加灵活,可以装配自己想要装配的bean。