In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
In this article Xiaobian introduces in detail "how to integrate Spring configuration into Tomcat server in Java", with detailed content, clear steps and proper handling of details. I hope that this article "how to integrate Spring configuration into Tomcat server in Java" can help you solve your doubts.
Add Tomcat dependency
Org.apache apache-tomcat-9.0.36-src 1.0-SNAPSHOT
First you need a tomcat startup class
Public class TomcatRun {private static final int PORT = 8080; private static final String CONTEXT_PATH = "/ com/yu"; public static void main (String [] args) {Tomcat tomcat = new Tomcat (); / / set listening port tomcat.setPort (PORT); tomcat.getHost () .setAppBase (".") / / Connector does not exist here. Automatically create a Connector and assign the port of tomcat to Connector tomcat.getConnector (); tomcat.addWebapp (CONTEXT_PATH, new File ("src/main/webapp"). GetAbsolutePath ()); try {tomcat.start () } catch (LifecycleException e) {e.printStackTrace ();} tomcat.getServer () .await ();}}
You can also create an Connector designated port yourself.
/ / manually create connector// Connector connector = new Connector (); / / connector.setPort (PORT); / / tomcat.getService () .addConnector (connector)
Create a Spring configuration
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 ())};}}
This is the end of it.
The question is, how does Tomcat start Sping?
The container that implements Servlet3.0 (such as Tomcat) scans the META-INF/services/javax.servlet.ServletContainerInitializer file under the classpath*, which specifies the implementation of ServletContainerInitializer. In addition, there is an annotation HandlesTypes that expresses interest in a class and passes in the implementation class of the interface specified by HandlesTypes when calling the onStartup method.
For example, the ServletContainerInitializer implementation class SpringServletContainerInitializer in Spring will call the onStartup method of WebApplicationInitializer, which is the onStartup of the MyWebApplicationInitializer parent class defined above. Here, initialization of AnnotationConfigWebApplicationContext and DispatcherServlet will be completed one after another.
@ HandlesTypes (WebApplicationInitializer.class) public class SpringServletContainerInitializer implements ServletContainerInitializer {@ Override public void onStartup (@ Nullable Set 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) {/ / call the onStartup method initializer.onStartup (servletContext) of the WebApplicationInitializer implementation class } read here, this article "how to integrate Spring configuration into Tomcat server in Java" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.