方式一:Spring SPI
META-INF\spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.example.boot.serve.starter.AppConfig
方式二:
利用 @Import
注解,导入一个实现了 ImportSelector
接口的类,然后由这个类,完成 Configuration 的注册
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Import(SwitchLoggerImport.class)
public @interface EnableSwitchLogger {
}
public class SwitchLoggerImport implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
return new String[]{SwitchLoggerConfig.class.getName()};
}
}
自动配置类
@Configuration
@Import({Heartbeat.class})
@ConditionalOnBean({ServeMarker.class})
@EnableConfigurationProperties({ServeProperties.class})
public class AppConfig {
@Bean
public ServeController serveController() {
return new ServeController();
}
@Bean
public ServletRegistrationBean<Servlet> servletRegistrationBean() {
ServletRegistrationBean<Servlet> registrationBean = new ServletRegistrationBean();
registrationBean.setServlet(new DispatcherRegisterServlet());
registrationBean.addUrlMappings(new String[]{"*.do"});
registrationBean.setLoadOnStartup(1);
return registrationBean;
}
}