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:
PackageCom.kiiwow;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.EnableAspectJAutoProxy;ImportOrg.springframework.context.annotation.Import;ImportOrg.springframework.context.annotation.PropertySource;Importorg.springframework.transaction.annotation.EnableTransactionManagement;ImportCom.kiiwow.framework.config.context.spring.rest.RestSpringContextConfig;Importcom.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 classAppConfig {}
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:
PackageCom.kiiwow;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.context.annotation.PropertySource;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;ImportOrg.springframework.web.servlet.config.annotation.InterceptorRegistry;ImportCom.kiiwow.framework.config.context.spring.WebContextConfig, @Configuration @enablewebmvc@componentscan ( Basepackages= {"Com.kiiwow.*"}) @PropertySource ({"Classpath:kiiwow.properties"}) Public classWebconfigextendsWebcontextconfig {@Override Public voidaddinterceptors (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 directly override the void 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, there is a method in the interface void Onstartup (ServletContext container) throws Servletexception, The method body of this method is the action that needs to be done when the Web container starts, which is the information we previously configured in XML. The source code is as follows:
PackageCom.kiiwow;ImportJava.util.EnumSet;ImportJavax.servlet.DispatcherType;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRegistration.Dynamic;ImportOrg.springframework.web.WebApplicationInitializer;ImportOrg.springframework.web.context.ContextLoaderListener;ImportOrg.springframework.web.context.support.AnnotationConfigWebApplicationContext;ImportOrg.springframework.web.filter.CharacterEncodingFilter;ImportOrg.springframework.web.servlet.DispatcherServlet;ImportCom.kiiwow.framework.platform.sqlmapping.SQLMappingInitListener;/*** The Web container configuration class to replace the XML. * Configure filters, listeners, Servlets *@authorLeon.gan **/ Public classKiiwowinitializerImplementsWebapplicationinitializer {@Override Public voidOnstartup (ServletContext Container)throwsservletexception {//Configure spring-provided character encoding filtersJavax.servlet.FilterRegistration.Dynamic filter = Container.addfilter ("Encoding",NewCharacterencodingfilter ()); //Configure filter paths for filtersFilter.addmappingforurlpatterns (Enumset.of (dispatchertype.request),true, "/"); //Spring container context based on annotation configurationAnnotationconfigwebapplicationcontext Rootcontext =NewAnnotationconfigwebapplicationcontext (); //registering the Spring container configuration classRootcontext.register (AppConfig.class); Container.addlistener (NewContextloaderlistener (Rootcontext)); //SQL configuration file ListenerContainer.addlistener (NewSqlmappinginitlistener ()); //Web container context based on annotation configurationAnnotationconfigwebapplicationcontext context =NewAnnotationconfigwebapplicationcontext (); //registering the Web container configuration classContext.register (Webconfig.class); Dynamic servlet= Container.addservlet ("Dispatcher",NewDispatcherservlet (context)); //Configure mapping PathsServlet.addmapping ("/"); //Boot orderServlet.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 use the spring
http://my.oschina.net/devleon/blog/530953
Thinking and implementation of the spring container and Web container 0 configuration based on pure Java Code (3)-Using configuration