使用的是spring mvc (4.3.2.RELEASE) + thymeleaf(3.0.2.RELEASE), 持久层使用的 spring的 JdbcTemplate, PS:推荐一个很好用的对JdbcTemplate封装的框架:https://github.com/selfly/dexcoder-assistant 。 下面开始具体的配置:
配置spring mvc DispatcherServlet
DispatcherServlet 是spring mvc的核心, Spring 提供了一个快速配置DispatcherServlet的类 AbstractAnnotationConfigDispatcherServletInitializer,具体代码如下:
其中 onStartup() 是 WebApplicationInitializer 接口中的方法,用户配置其他的 filter 和 listener
getRootConfigClasses() 获取配置类,我理解的相当于 applicationContext.xml 创建的上下文
getServletConfigClasses()获取配置类,相当于 mvc-servlet.xml 创建的上下文
此类上不需要任何注解,
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 AbstractAnnotationConfigDispatcherServletInitializer implements WebApplicationInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class<?>[]{RootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class<?>[]{WebConfig.class};
}
/**
* 配置DispatcherServlet 匹配的路径
* @return
*/
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
/**
* 配置其他的 servlet 和 filter
*
* @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, "/*");
}
}
配置 applicationContext.xml,由RootConfig类实现
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") // 导入属性文件
@EnableAspectJAutoProxy // 相当于 xml 中的 <aop:aspectj-autoproxy/>
@EnableTransactionManagement // 开启注解事务
@ComponentScan(basePackages = {"com.liulu.lit", "com.liulu.bank"}, excludeFilters = @ComponentScan.Filter(classes = Controller.class ))
public class RootConfig {
// 上面导入的属性文件中的属性会 注入到 Environment 中
@Resource
private Environment env;
/**
* 配置数据库连接池 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;
}