1、WebInitializer package com.amiu.spring.config;import javax.servlet.ServletContext;import javax.servlet.ServletException;import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;import org.sprin
package com.amiu.spring.config; import javax.servlet.ServletContext; import javax.servlet.ServletException; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import org.springframework.web.util.Log4jConfigListener; public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer{ @Override protected Class [] getRootConfigClasses() { // TODO Auto-generated method stub return new Class[]{MvcConfig.class,MybatisConfig.class}; } @Override protected Class [] getServletConfigClasses() { // TODO Auto-generated method stub return null; } @Override protected String[] getServletMappings() { // TODO Auto-generated method stub return new String[]{"/"}; } /** * 这个方法tomcat启动就会调用,可以代替web.xml注册filter,listener,servlet等 */ @Override public void onStartup(ServletContext container) throws ServletException { //config log4j container.setInitParameter("log4jConfigLocation", "classpath:log4j.properties"); container.addListener(Log4jConfigListener.class); super.onStartup(container); } }2、MybatisConfig
package com.amiu.spring.config; import java.io.FileNotFoundException; import java.io.IOException; import java.util.Properties; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.core.io.support.PropertiesLoaderUtils; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.core.env.Environment; import com.alibaba.druid.pool.DruidDataSource; @Configuration //启用注解事务管理,使用CGLib代理 @EnableTransactionManagement(proxyTargetClass = true) //加载资源文件 @PropertySource({"classpath:dbConfig.properties"}) @MapperScan(basePackages="com.amiu.autoMybatis.mapper") public class MybatisConfig { @Autowired Environment env;//还可以使用Environment获取properties信息 //绑定资源属性 @Value("${db.driver}") String driver; @Value("${db.url}") String url; @Value("${db.username}") String username; @Value("${db.password}") String password; //这里我们使用的DataSource是阿里的,也可以换为其他的,比如Mybatis自带的PooledDataSource @Bean public DruidDataSource druidDataSource() throws IOException{ //不用@Value我们可以使用Environment获取properties信息 //driver = env.getProperty("db.username"); //url = env.getProperty("db.url"); //username = env.getProperty("db.username"); //password = env.getProperty("db.password"); DruidDataSource druidDataSource = new DruidDataSource(); druidDataSource.setDriverClassName(driver); druidDataSource.setUrl(url); druidDataSource.setUsername(username); druidDataSource.setPassword(password); return druidDataSource; } //配置事务管理器 @Bean public DataSourceTransactionManager transactionManager() throws IOException{ //这里的数据源要和配置SqlSessionFactoryBean中配置的数据源相同,事务才会生效 DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(druidDataSource()); return transactionManager; } //配置SqlSessionFactoryBean @Bean public SqlSessionFactoryBean sqlSessionFactory() throws IOException{ SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setDataSource(druidDataSource()); // sqlSessionFactoryBean.setTypeAliasesPackage("com.amiu"); // sqlSessionFactoryBean.setMapperLocations( // new PathMatchingResourcePatternResolver() // .getResources("classpath:mapper/*.xml")); return sqlSessionFactoryBean; } //添加解析类才能让@PropertySource正确解析出${}中的值 @Bean public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() { return new PropertySourcesPlaceholderConfigurer(); } }3、MvcConfig
package com.amiu.spring.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframework.context.support.PropertySourcesPlaceholderConfigurer; import org.springframework.http.MediaType; import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.json.MappingJackson2JsonView; @Configuration @EnableWebMvc @ComponentScan("com.amiu") public class MvcConfig extends WebMvcConfigurerAdapter { @Override public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer) { configurer.enable(); } }