ServletContainerInitializer
:servlet 3.0
引入的接口,用于在web应用启动时动态添加servlet
,filter
,listener
基于spi
机制(service provider interface) ,META-INF/services/javax.servlet.ServletContainerInitializer
中存放实现该接口的实现类,这些类会被
只能在使用jar文件中,不能使用在web项目中
@HandlerType
作用:
serlvet容器启动时 ,会将SPI注册的Java EE 接口 ServletContainerInitializer 的所有实现类(例如:SpringServletContainerInitailizer
)挨个回调 onStartup 方法,,而 onStartup需要的参数,就是通过@HandlerType
传递的,
引用:https://www.jianshu.com/p/230878a836f9
https://blog.csdn.net/qq_41490274/article/details/120578654
spring使用java 配置类,不使用web.xml
- 实现
WebApllicationInitializer
,, 因为SpringServletContainerInitializer
实现了ServletContainerInitilizer
service provider interface 自动导入SpringServletContainerInitializer
这个SpringServletContainerInitailizer
中有个注解
会将WebApplicationInitializer
传入onStartup
方法的第一个参数
代码:
spring配置类:
@ComponentScan(basePackages = "com.cj",useDefaultFilters = true,excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class)})
@Configuration
public class SpringConfig {
}
springmvc 配置类: 将@Configuration
注册进去
@ComponentScan(basePackages = "com.cj",useDefaultFilters = false,includeFilters ={ @ComponentScan.Filter(type = FilterType.ANNOTATION,value = Controller.class),@ComponentScan.Filter(type = FilterType.ANNOTATION,value = Configuration.class)})
public class SpringMvcConfig {
}
实现WebApplicationInitializer
加载 servlet
public class WebInit implements WebApplicationInitializer {
@Override
public void onStartup(javax.servlet.ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
// 注册springmvc配置文件 这里只加载了 springmvc,也可以将 spring配置添加进去
context.register(SpringMvcConfig.class);
ServletRegistration.Dynamic springmvc = servletContext.addServlet("springmvc", new DispatcherServlet(context));
springmvc.addMapping("/");
springmvc.setLoadOnStartup(1);
}
}
controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.hehe();
}
@GetMapping("/hello1")
public String hello1(){
return "hello1";
}
}
SPI (service provider interface)
- 在jar包的
META-INF/services/
下面创建一个文件,,文件名是接口的全限定名,,,文件内容是接口的实现类的全限定名,, - 将spi所在的jar放在主程序的classpath中
- 服务调用方 使用
java.util.ServiceLoader
,去动态加载具体的实现类到JVM
代码
实现接口的类:
public class SimpleDriver implements Driver {
@Override
public Connection connect(String url, Properties info) throws SQLException {
System.out.println("ok");
return null;
}
@Override
public boolean acceptsURL(String url) throws SQLException {
return false;
}
@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
return new DriverPropertyInfo[0];
}
@Override
public int getMajorVersion() {
return 0;
}
@Override
public int getMinorVersion() {
return 0;
}
@Override
public boolean jdbcCompliant() {
return false;
}
@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}
}
public class Test {
public static void main(String[] args) throws SQLException {
Iterator<Driver> providers = Service.providers(Driver.class);
System.out.println(providers.hasNext());
while (providers.hasNext()){
Driver current = providers.next();
System.out.println("current = " + current);
current.connect("",null);
}
}
}
引用:https://www.w3cschool.cn/article/28821241.html