Starting a web project in servlet 3.0 can no longer require a web.xml configuration file, so the configuration in 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 JdbcTemplate used in the persistence layer. PS: Recommend a very useful framework for JdbcTemplate encapsulation: https://github.com/selfly/dexcoder-assistant. The following is the specific configuration:
Configure spring mvc DispatcherServlet
DispatcherServlet is the core of spring mvc. Spring provides a class AbstractAnnotationConfigDispatcherServletInitializer that quickly configures DispatcherServlet. The specific code is as follows:
where onStartup() is a method in the WebApplicationInitializer interface, and the user configures other filters and listeners.
getRootConfigClasses() gets the configuration class, what 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 comments are required in this category
package com.liu.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 : liu * Date : 2016-10-7 15:12 */public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class<?>[]{WebConfig.class}; } /** * Configure the path to match the DispatcherServlet* @return */ @Override protected String[] getServletMappings() { return new String[]{"/"}; } /** * Configure other servlets and filters * * @param servletContext * @throws ServletException */ @Override public void onStartup(ServletContext servletContext) 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.liu.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 : liu * Date : 2016-10-7 15:36 */@Configuration@PropertySource("classpath:config.properties") // Import property file @EnableAspectJAutoProxy // Equivalent to <aop:aspectj-autoproxy/>@EnableTransactionManagement in xml // Enable annotation transaction @ComponentScan(basePackages = {"com.liulu.lit", "com.liulu.bank"}, excludeFilters = @ComponentScan.Filter(classes = Controller.class ))public class RootConfig { // The properties in the attribute file imported above will be injected into Environment @Resource private Environment env; /** * Configure database connection pool c3p0, * @return * @throws PropertyVetoException */ @Bean public DataSource dataSource() throws PropertyVetoException { 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 the Things Manager* @param dataSource * @return */ @Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); } @Bean public JdbcTemplate jdbcTemplate (DataSource dataSource) { return new JdbcTemplate(dataSource); }}The config.properties file is in the resources directory
#Database configuration db.url=jdbc:mysql://192.168.182.135:3306/bankdb.driver=com.mysql.jdbc.Driverdb.user=rootdb.password=123456#Database connection pool configuration#Minimum number of connections reserved in connection pool pool pool.minPoolSize=5#Maximum number of connections reserved in connection pool pool pool.maxPoolSize=30#Get connection timeout pool.checkoutTimeout=1000
Configure servlet.xml, implemented by WebConfig class
The Thymeleaf template configuration is also below
package com.liu.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.templatesolver.SpringResourceTemplateResolver;import org.thymeleaf.spring4.view.ThymeleafViewResolver;import org.thymeleaf.templatemode.TemplateMode;import java.nio.charset.StandardCharsets;/** * User : liu * Date : 2016-10-7 15:16 */@Configuration@EnableWebMvc // Enable SpringMVC, which is equivalent to <mvc:annotation-driven/>@ComponentScan(basePackages = {"com.liulu.bank.controller", "com.liulu.lit"}, includeFilters = @ComponentScan.Filter(classes = Controller.class), useDefaultFilters = false)public class WebConfig extends WebMvcConfigurerAdapter { /** * Setting the static resources handled by the web container, which is equivalent to <mvc:default-servlet-handler/> in xml */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configure) { 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 ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine); viewResolver.setCharacterEncoding(String.valueOf(StandardCharsets.UTF_8)); return viewResolver; }}The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.