I hard to force students a piece, will soon graduate, faced with looking for work, really do not want to leave school. Under the guidance of teachers to learn spring MVC, the configuration file is really cumbersome, so on-line Baidu Learning Spring MVC full annotated way to complete the assembly work of spring;
Not much nonsense to say on the dry, in fact, I do not understand how to simply run a spring MVC full annotated project, can not be said to be full annotation, because the preservation of Web. XML and Spring-serlvet.xml files, (may have children's shoes will say, This configuration may not be convenient for future modifications, and it is not possible to switch to certain environments by modifying only the configuration file. In fact, the 0 configuration file simply modifies the configuration of the class definition and does not modify the flexibility of the previous configuration file. I think no one will change the configuration of a servlet in the previous web. Xml. Moreover, these so-called configuration file flexibility, only for a certain value, we can write in our properties file, and spring for such configuration files have very good support, and the use of very convenient, interested children shoes can go to search. So please dispel the idea that the configuration is not flexible).
First, let's look at the project structure.
The project uses MAVEN management, the beginners should all know the advantages of MAVEN, you can learn a bit.
Here's a look at the core configuration Java class
1 PackageCn.edu.lhs.config;2 ImportOrg.springframework.context.annotation.Bean;3 ImportOrg.springframework.context.annotation.ComponentScan;4 Importorg.springframework.context.annotation.Configuration;5 ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;6 ImportOrg.springframework.web.servlet.view.JstlView;7 ImportOrg.springframework.web.servlet.view.UrlBasedViewResolver;8 9 @ConfigurationTen@ComponentScan ("CN.EDU.LHS") One @EnableWebMvc A Public classWebconfig { - /** - * JSP View parser the */ - @Bean - Publicurlbasedviewresolver Setupviewresolver () { -Urlbasedviewresolver resilver=Newurlbasedviewresolver (); +Resilver.setprefix ("web-inf/views/"); -Resilver.setsuffix (". JSP"); +Resilver.setviewclass (Jstlview.class); A returnResilver; at - - } -}
The above code is for the JSP view under Configuration Spring-mvc.xml.
Let's go to Web.
package Cn.edu.lhs.config;import Javax.servlet.servletcontext;import Javax.servlet.servletexception;import Javax.servlet.servletregistration.dynamic;import Org.springframework.context.annotation.componentscan;import Org.springframework.web.WebApplicationInitializer; Import Org.springframework.web.context.support.annotationconfigwebapplicationcontext;import Org.springframework.web.servlet.DispatcherServlet, @ComponentScan ("CN.EDU.LHS") public class Webinitalizer Implements Webapplicationinitializer {public void Onstartup (ServletContext servletcontext) throws servletexception{ Annotationconfigwebapplicationcontext ctx=new Annotationconfigwebapplicationcontext (); Ctx.register ( Webconfig.class) Ctx.setservletcontext (servletcontext);D ynamic servlet=servletcontext.addservlet ("Dispatcher", New Dispatcherservlet (CTX)); Servlet.addmapping ("/"); Servlet.setloadonstartup (1);}}
Here with the Webapplicationinitializer interface, using the Annotationconfigwebapplicationcontext class
Register
Public void Register ( Class <? > ... annotatedclasses)
registers one or more annotation classes to be processed.
Please note that AbstractApplicationContext.refresh()
in order for the context to fully process the new class , you must call them.
- Designated by:
-
register
In the interface
AnnotationConfigRegistry
- Parameters:
-
annotatedClasses
-One or more annotation classes, such as
@Configuration
class
- You can also look at:
-
scan(String...)
loadBeanDefinitions(DefaultListableBeanFactory)
AbstractRefreshableConfigApplicationContext.setConfigLocation(String)
,, ,
AbstractApplicationContext.refresh()
Registering a configuration Class
Now it's time to delete what's in Web. Xml.
Project code: HTTPS://GITHUB.COM/TSXYLHS/SPRING-MVC
Java Spring MVC full annotation