首先确定springboot在spring基础上主要做了哪些改动:
springboot: 源码实现逻辑
user : 业务系统
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.60</version>
</dependency>
</dependencies>
下面一一实现:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Configuration
@ComponentScan
@Import(KcImportSelect.class)
public @interface KcSpringBootApplication {
}
public class KcSpringApplication {
public static AnnotationConfigWebApplicationContext run(Class cls){
/**
* spring启动步骤:
* 1、构建上下文对象
* 2、注册配置类
* 3、刷新容器
*/
AnnotationConfigWebApplicationContext context=new AnnotationConfigWebApplicationContext();
context.register(cls);
context.refresh();
/**
* 内嵌tomcat\jetty 启动
*/
WebServer webServer=getWebServer(context);
webServer.start();
return context;
}
/**
* 获取服务,有可能tomcat\jetty或者其他服务器
* @param context
* @return
*/
public static WebServer getWebServer(AnnotationConfigWebApplicationContext context){
Map<String, WebServer> beansOfType = context.getBeansOfType(WebServer.class);
if(beansOfType.isEmpty()||beansOfType.size()>1){
throw new NullPointerException();
}
return beansOfType.values().stream().findFirst().get();
}
}
项目根据pom配置动态实现tomcat\jetty内嵌要求如下:
首先定义服务接口WebServer
public interface WebServer {
void start() ;
}
tomcat服务实现:
public class TomcatWebServer implements WebServer {
private Tomcat tomcat;
public TomcatWebServer(WebApplicationContext webApplicationContext) {
tomcat = new Tomcat();
Server server = tomcat.getServer();
Service service = server.findService("Tomcat");
Connector connector = new Connector();
connector.setPort(8081);
Engine engine = new StandardEngine();
engine.setDefaultHost("localhost");
Host host = new StandardHost();
host.setName("localhost");
String contextPath = "";
Context context = new StandardContext();
context.setPath(contextPath);
context.addLifecycleListener(new Tomcat.FixContextListener());
host.addChild(context);
engine.addChild(host);
service.setContainer(engine);
service.addConnector(connector);
tomcat.addServlet(contextPath, "dispatcher", new DispatcherServlet(webApplicationContext));
context.addServletMappingDecoded("/*", "dispatcher");
}
@Override
public void start() {
try {
System.out.println("tomcat start......");
tomcat.start();
}catch (Exception e){
e.printStackTrace();
}
}
}
jetty服务实现(具体实现逻辑没写,具体实现逻辑类似tomcat实现)
public class JettyWebServer implements WebServer{
@Override
public void start() {
System.out.println("jetty start......");
}
}
思考:jetty\tomcat都已实现,总不能用if/else这样决定用哪个服务扩展性太差,基于此,就联想到spring的Condition条件注解和参考springboot中的OnClassCondition这个注解。
具体实现如下:
public class KcOnClassCondition implements Condition {
@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(KcConditionalOnClass.class.getName());
String value = (String) annotationAttributes.get("value");
try {
context.getClassLoader().loadClass(value);
} catch (ClassNotFoundException e) {
return false;
}
return true;
}
}
@Configuration
public class WebServerConfiguration{
@Bean
@KcConditionalOnClass("org.apache.catalina.startup.Tomcat")
public TomcatWebServer tomcatWebServer( WebApplicationContext webApplicationContext){
return new TomcatWebServer(webApplicationContext);
}
@Bean
@KcConditionalOnClass("org.eclipse.jetty.server.Server")
public JettyWebServer jettyWebServer(){
return new JettyWebServer();
}
}
public interface AutoConfiguration {
}
public class KcImportSelect implements DeferredImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
ServiceLoader<AutoConfiguration> load = ServiceLoader.load(AutoConfiguration.class);
List<String> list = new ArrayList<>();
for (AutoConfiguration autoConfiguration : load) {
list.add(autoConfiguration.getClass().getName());
}
return list.toArray(new String[0]);
}
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Configuration
@ComponentScan
@Import(KcImportSelect.class)
public @interface KcSpringBootApplication {
}
@Configuration
public class WebServerConfiguration implements AutoConfiguration{
@Bean
@KcConditionalOnClass("org.apache.catalina.startup.Tomcat")
public TomcatWebServer tomcatWebServer( WebApplicationContext webApplicationContext){
return new TomcatWebServer(webApplicationContext);
}
@Bean
@KcConditionalOnClass("org.eclipse.jetty.server.Server")
public JettyWebServer jettyWebServer(){
return new JettyWebServer();
}
}
至此,springboot简易版已全部实现。
<dependencies>
<dependency>
<groupId>com.kc</groupId>
<artifactId>springboot</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
@ComponentScan(basePackages = {
"kc.*"})
@KcSpringBootApplication
public class UserApplication {
public static void main(String[] args) {
AnnotationConfigWebApplicationContext run = KcSpringApplication.run(UserApplication.class);
System.out.println(run.getBeanFactory());
}
}
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("test")
public String test() {
return userService.test();
}
}
@Service
public class UserService {
public String test() {
return "hello springboot";
}
}
git地址