Thought and implementation of the configuration of spring container and Web container 0 based on pure Java Code (1)-Data source and transaction management and the thinking and implementation of the spring container and Web container 0 configuration based on pure Java Code (2)-a description of the two blog posts in static resources, views, and messages We have configured the basic configuration required for spring. In this blog post, we'll show you how to use these configurations in the actual project and replace the Web. xml file with a Java class.
We use Java code to configure spring so that our configurations can be reused, and for the reuse of these configurations, we implement them with inheritance and introduction. We also provide two classes to configure the spring container in a project. First provide a AppConfig class to configure the spring container, the source code is as follows:
package com.kiiwow;import org.springframework.context.annotation.componentscan;import org.springframework.context.annotation.configuration;import org.springframework.context.annotation.enableaspectjautoproxy;import org.springframework.context.annotation.import;import org.springframework.context.annotation.propertysource;import org.springframework.transaction.annotation.enabletransactionmanagement;import com.kiiwow.framework.config.context.spring.rest.restspringcontextconfig;import com.kiiwow.framework.config.context.spring.rest.dbconfig.c3p0datasourcedbconfig;@ Configuration@enableaspectjautoproxy@enabletransactionmanagement@componentscan (basePackages = {" Com.kiiwow.* "}) @Import ({springcontextconfig.class, c3p0datasourcedbconfig.class}) @PropertySource ({" Classpath:kiiwow.properties "}) public class appconfig {}
The above code uses a lot of annotations, and we explain each of them individually:
-
@Configuration, used to indicate that this class is a configuration class for configuring spring related information
-
@EnableAspectJAutoProxy, enable slice Auto proxy for AOP
-
Enabletransactionmanagement, enable annotation transactions, that is, you can use @transactional annotations to control transactions
-
Componentscan, component scanning, scanning of components registered by @controller, @Service, @Component and other annotations under basepackages specified directory
-
@Import, introducing the specified configuration class, we introduced the Spring container configuration class and the data source transaction configuration class
-
@PropertySource, loads the specified configuration file, the profile content is loaded into the environment and waits for the call
So that we can complete the configuration required by the spring container, we will provide a Webconfig class to configure the content of the Web sub-container, the source code is as follows:
package com.kiiwow;import org.springframework.context.annotation.componentscan;import org.springframework.context.annotation.configuration;import org.springframework.context.annotation.propertysource;import org.springframework.web.servlet.config.annotation.enablewebmvc;import org.springframework.web.servlet.config.annotation.interceptorregistry;import Com.kiiwow.framework.config.context.spring.WebContextConfig, @Configuration @enablewebmvc@componentscan ( basepackages = {"com.kiiwow.*"}) @PropertySource ({"Classpath:kiiwow.properties"}) Public class WebConfig extends WebContextConfig { @Override public void addinterceptors (interceptorregistry registry) { //registering a custom listener }}
@EnableWebMvc annotations are used to enable SPRINGMVC, we have webconfig inherit from the Web sub-container configuration class Webcontextconfig that we have designed so that Webconfig already has the basic configuration needed. Because Webcontextconfig itself inherits the Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter, so we can rewrite void directly Addinterceptors (Interceptorregistry Registry) method to register the custom listener.
At this point, we're all done with the basic configuration of spring based on Java code. But we know that SPRINGMVC will need to configure the relevant servlet in Web. XML, but since we have a full Java configuration for spring, we might as well use Java code instead of the Web. XML configuration. We provide a Kiiwowinitializer class to replace the Web. XML, which implements the Org.springframework.web.WebApplicationInitializer interface with a method void in the interface Onstartup (ServletContext container) throws Servletexception, the method body of this method is the action that should be done when the Web container starts, that is, the information we previously configured in XML. The source code is as follows:
package com.kiiwow;import java.util.enumset;import javax.servlet.dispatchertype;import javax.servlet.servletcontext;import javax.servlet.servletexception;import Javax.servlet.servletregistration.dynamic;import org.springframework.web.webapplicationinitializer;import org.springframework.web.context.ContextLoaderListener;import org.springframework.web.context.support.annotationconfigwebapplicationcontext;import org.springframework.web.filter.characterencodingfilter;import org.springframework.web.servlet.dispatcherservlet;import com.kiiwow.framework.platform.sqlmapping.sqlmappinginitlistener;/** * A Web container configuration class that is used in place of XML * Here Configure filters, listeners, servlet * * @author leon.gan * */public class kiiwowinitializer implements webapplicationinitializer { @Override public void onstartup (Servletcontext container) throws servletexception { //configuring spring-provided character encoding filters javax.servlet.FilterRegistration.Dynamic filter = Container.addfilter ("Encoding", new characterencodingfilter ()); //the filter path of the configuration filter filter.addmappingforurlpatterns ( Enumset.of (dispatchertype.request), true, "/"); //Spring container context based on annotation configuration AnnotationConfigWebApplicationContext rootContext = new Annotationconfigwebapplicationcontext (); //register Spring container configuration class rootcontext.register (Appconfig.class); container.addlistener (New contexTloaderlistener (rootcontext)) //sql configuration file Listener container.addlistener (New sqlmappinginitlistener ()); //Web container context based on annotation configuration AnnotationConfigWebApplicationContext context = new Annotationconfigwebapplicationcontext (); //registering the Web container configuration class context.register (Webconfig.class); dynamic servlet = container.addservlet ("Dispatcher", new Dispatcherservlet (context)); //Configuration Mapping Path servlet.addmapping ("/"); //start sequence seRvlet.setloadonstartup (1); }}
We first register the character encoding filter provided by spring into the container and then create a spring container context based on the annotation configuration, which registers the configuration class configured for the spring container, that is, the AppConfig we configured earlier, This context is then added to the container's listener, making its life cycle consistent with the Web container's life cycle, and then adding a series of custom listeners. We then create a Web container context based on the annotation configuration, which is used to register the configuration class configured for the Springweb container, which is the webconfig we just defined, The Org.springframework.web.servlet.DispatcherServlet.DispatcherServlet is then created based on this context to intercept and forward requests.
At this point, all of our configuration has been completed, this time in our project for the configuration of this piece will no longer exist in the XML file. In addition, if we are using MAVEN to manage the project, this time MAVEN cannot find the Web. xml file in our project, it will error in the Pom file, and will not be able to package the operation. At this point we need to add a war package plugin in the Pom file to suppress this error, the source code is as follows:
<plugin><groupid>org.apache.maven.plugins</groupid><artifactid>maven-war-plugin</ artifactid><version>2.2</version><configuration><failonmissingwebxml>false</ Failonmissingwebxml></configuration></plugin>
After three blog posts, we introduced the method of spring container and Web container 0 configuration based on pure Java code, hoping to be helpful to the use of spring. Thank you for your reading, trouble a bit of praise.
Thinking and implementation of the spring container and Web container 0 configuration based on pure Java Code (3)-Using configuration