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 {
}
1
2
3
4
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 {
}
1
2
3
实现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);
}
}
1
2
3
4
5
6
7
8
9
10
11
controller
@RestController
public class HelloController {
@Autowired
HelloService helloService;
@GetMapping("/hello")
public String hello(){
return helloService.hehe();
}
@GetMapping("/hello1")
public String hello1(){
return "hello1";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
引用:https://www.w3cschool.cn/article/28821241.html