At the beginning of Servlet 3.0, Web projects can be completely web.xml configuration files, so the configuration of this article is only valid in web containers that support Servlet 3.0 and above
Using spring MVC (4.3.2.RELEASE) + thymeleaf (3.0.2.RELEASE), the spring jdbctemplate used by the persistence layer, PS: Recommend a very useful framework for the JdbcTemplate package: Https://github.com/selfly/dexcoder-assistant. The following starts with a specific configuration:
Configuring the Spring MVC Dispatcherservlet
Dispatcherservlet is at the heart of Spring MVC, and Spring provides a quick-configure Dispatcherservlet class Abstractannotationconfigdispatcherservletinitializer, the specific code is as follows:
where Onstartup () is the method in the Webapplicationinitializer interface, the user configures other filter and listener
Getrootconfigclasses () Gets the configuration class, which I understand is equivalent to the context created by Applicationcontext.xml
Getservletconfigclasses () Gets the configuration class, which is equivalent to the context created by Mvc-servlet.xml
No annotations are required on this class
Package com.liulu.bank.config;
Import Org.springframework.web.WebApplicationInitializer;
Import Org.springframework.web.filter.CharacterEncodingFilter;
Import Org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
Import javax.servlet.FilterRegistration;
Import Javax.servlet.ServletContext;
Import javax.servlet.ServletException;
Import Java.nio.charset.StandardCharsets; /** * User:liulu * date:2016-10-7 15:12 * * public class Webappinitializer extends Abstractannotationconfigdispatche Rservletinitializer implements Webapplicationinitializer {@Override protected class<?>[] getrootconfigclasses (
) {return new class<?>[]{rootconfig.class};
} @Override protected class<?>[] Getservletconfigclasses () {return new class<?>[]{webconfig.class}; /** * Configure Dispatcherservlet matching path * @return/@Override protected string[] Getservletmappings () {return new
string[]{"/"}; }/** * Configure additional servlet and Filter * * @param servletcontext * @throws servletexception/@Override public void Onstartup (ServletContext SERVL Etcontext) throws Servletexception {filterregistration.dynamic encodingfilter = Servletcontext.addfilter ("
Encodingfilter ", Characterencodingfilter.class);
Encodingfilter.setinitparameter ("Encoding", string.valueof (Standardcharsets.utf_8));
Encodingfilter.setinitparameter ("Forceencoding", "true");
Encodingfilter.addmappingforurlpatterns (null, FALSE, "/*");
}
}
Configure Applicationcontext.xml, implemented by RootConfig class
Package com.liulu.bank.config;
Import Com.mchange.v2.c3p0.ComboPooledDataSource;
Import org.springframework.context.annotation.*;
Import org.springframework.core.env.Environment;
Import Org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.jdbc.datasource.DataSourceTransactionManager;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.transaction.PlatformTransactionManager;
Import org.springframework.transaction.annotation.EnableTransactionManagement;
Import Javax.annotation.Resource;
Import Javax.sql.DataSource;
Import java.beans.PropertyVetoException;
/** * User:liulu * date:2016-10-7 15:36/@Configuration @PropertySource ("classpath:config.properties")//Import Property file @EnableAspectJAutoProxy//equivalent of <aop:aspectj-autoproxy/> @EnableTransactionManagement in XML//Open annotation Transaction @ Componentscan (basepackages = {"Com.liulu.lit", "Com.liulu.bank"}, Excludefilters = @ComponentScan. Filter (classes =
Controller.class)) public class RootConfig { The properties in the properties file imported above are injected into the environment @Resource private environment env; /** * Configuration database connection pool C3P0, * @return * @throws propertyvetoexception/@Bean public DataSource DataSource () throws Pr
opertyvetoexception {Combopooleddatasource DataSource = new Combopooleddatasource ();
Datasource.setjdbcurl (Env.getproperty ("Db.url"));
Datasource.setdriverclass (Env.getproperty ("Db.driver"));
Datasource.setuser (Env.getproperty ("Db.user"));
Datasource.setpassword (Env.getproperty ("Db.password"));
Datasource.setminpoolsize (integer.valueof) (Env.getproperty ("pool.minpoolsize"));
Datasource.setmaxpoolsize (integer.valueof) (Env.getproperty ("pool.maxpoolsize"));
Datasource.setautocommitonclose (FALSE);
Datasource.setcheckouttimeout (integer.valueof) (Env.getproperty ("pool.checkouttimeout"));
Datasource.setacquireretryattempts (2);
return dataSource; /** * Configure things Manager * @param dataSource * @return/@Bean public Platformtransactionmanager TransactionManager (Da Tasource daTasource) {return new Datasourcetransactionmanager (DataSource);
@Bean public jdbctemplate JdbcTemplate (DataSource DataSource) {return new JdbcTemplate (DataSource);
}
}
Config.properties files in the resources directory
#数据库配置
db.url=jdbc:mysql://192.168.182.135:3306/bank
db.driver=com.mysql.jdbc.driver
db.user=root
db.password=123456
#数据库连接池配置
#连接池中保留的最小连接数
pool.minpoolsize=5
#连接池中保留的最大连接数
pool.maxpoolsize=30
#获取连接超时时间
pool.checkouttimeout=1000
Configure Servlet.xml, implemented by the Webconfig class
The thymeleaf template configuration is also below
Package com.liulu.bank.config;
Import Org.springframework.context.annotation.Bean;
Import Org.springframework.context.annotation.ComponentScan;
Import org.springframework.context.annotation.Configuration;
Import Org.springframework.stereotype.Controller;
Import Org.springframework.web.servlet.ViewResolver;
Import Org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
Import ORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;
Import Org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
Import Org.thymeleaf.TemplateEngine;
Import Org.thymeleaf.spring4.SpringTemplateEngine;
Import Org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
Import Org.thymeleaf.spring4.view.ThymeleafViewResolver;
Import Org.thymeleaf.templatemode.TemplateMode;
Import Java.nio.charset.StandardCharsets; /** * User:liulu * date:2016-10-7 15:16/@Configuration @EnableWebMvc//enable SPRINGMVC, equivalent to <mvc:annotati in XML on-driven/> @ComponentScan (basepackages = {"Com.liulu.bank.controller", "Com.liulu.lit"}, Includefilters = @ Componentscan.filter (classes = controller.class), Usedefaultfilters = false) public class Webconfig extends, Webmvcconfig Ureradapter {/** * settings are handled by the Web container as static resources, equivalent to the <mvc:default-servlet-handler/> */@Override public void config in XML
Uredefaultservlethandling (Defaultservlethandlerconfigurer configurer) {configurer.enable ();
/** * The following three beans are configured thymeleaf template * @return * * * @Bean public springresourcetemplateresolver templateresolver () {
Springresourcetemplateresolver templateresolver = new Springresourcetemplateresolver ();
Templateresolver.setprefix ("/web-inf/templates/");
Templateresolver.setsuffix (". html");
Templateresolver.settemplatemode (templatemode.html);
Templateresolver.setcharacterencoding (string.valueof (standardcharsets.utf_8));
return templateresolver; @Bean public templateengine templateengine (Springresourcetemplateresolver templateresOlver) {Springtemplateengine templateengine = new Springtemplateengine ();
Templateengine.settemplateresolver (Templateresolver);
return templateengine; @Bean public viewresolver viewresolver (Templateengine templateengine) {thymeleafviewresolver viewresolver = new Th
Ymeleafviewresolver ();
Viewresolver.settemplateengine (Templateengine);
Viewresolver.setcharacterencoding (string.valueof (standardcharsets.utf_8));
return viewresolver;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.