多平台统一管理软件接口,如何实现多平台统一管理软件接口
347
2022-10-29
Spring通过Java配置集成Tomcat的方法
添加Tomcat依赖
首先需要一个tomcat启动类
public class TomcatRun {
private static final int PORT = 8080;
private static final String CONTEXT_PATH =onGHXV "/com/yu";
public static void main(String[] args) {
Tomcat tomcat = new Tomcat();
// 设置监听端口
tomcat.setPort(PORT);
tomcat.getHost().setAppBase(".");
// 这里 Connector 不存在,自动创建一个 Connector,并将 tomcat 的端口赋值给 Connector
tomcat.getConnector();
tomcat.addWebapp(CONTEXT_PATH, new File("src/main/webapp").getAbsolutePath());
try {
tomcat.start();
} catch (LifecycleException e) {
e.printStackTrace();
}
tomcat.getServer().await();
}
}
也可以自己创建一个Connector指定端口
// 手动创建 connector
// Connector connector = new Connector();
// connector.setPort(PORT);
// tomcat.getService().addConnector(connector);
创建Spring配置
public class MyWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class>[] getRootConfigClasses() {
return new Class[]{RootConfig.class};
}
@Override
protected Class>[] getServletConfigClasses() {
return new Class[]{AppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
@Override
protected Filter[] getServletFilters() {
return new Filter[]{
//
new CharacterEncodingFilter(StandardCharsets.UTF_8.name())
};
}
}
这里就结束了。。。
问题来了Tomcat是怎么启动Sping的?
实现了Servlet3.0的容器(例如Tomcat)会扫描classpath*下面的META-INF/services/javax.servlet.ServletContainerInitializer文件,里面指定ServletContainerInitializer的实现,另外还有一个注解HandlesTypes表达对某个类感兴趣,在调用onStartup方法时会将HandlesTypes指定接口的实现类传递进来。
例如:Spring中的ServletContainerInitializer实现类SpringServletContainerInitializer会调用WebApplicationInitializer的onStartup方法,也就是上面定义的MyWebApplicationInitializer父类的onStartup,这里就会相继完成AnnotationConfigWebApplicationContext和DispatcherServlet的初始化
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
@Override
public void onStartup(@Nullable Set
throws ServletException {
List
if (webAppInitializerClasses != null) {
for (Class> waiClass : webAppInitializerClasses) {
// Be defensive: Some servlet containers provide us with invalid classes,
// no matter what @HandlesTypes says...
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) &&
WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
initializers.add((WebApplicationInitializer)
ReflectionUtils.accessibleConstructor(waiClass).newInstance());
}
catch (Throwable ex) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", ex);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
return;
}
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
for (WebApplicationInitializer initializer : initializers) {
// 调用WebApplicationInitializer实现类的onStartup方法
initializer.onStartup(servletContext);
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~