不使用任何xml配置文件,也没有web.xml,只用注解来配置Spring,整合Hibernate,Mybatis,Spring MVC以及其他Spring的开源项目。
仅供参考,风格不同,个人喜好。
?
Class风格的web.xml
?
import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.ContextLoaderListener; import org.springframework.web.context.request.RequestContextListener; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; @Order(Ordered.LOWEST_PRECEDENCE) public class WebInit implements WebApplicationInitializer{ @Override public void onStartup(ServletContext container) throws ServletException { // Create the ‘root‘ Spring application context AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); rootContext.scan("com.demo.config.spring"); // Manage the lifecycle of the root application context container.addListener(new ContextLoaderListener(rootContext)); // Listener that exposes the request to the current thread container.addListener(new RequestContextListener()); // Create the dispatcher servlet‘s Spring application context AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext(); // Register and map the dispatcher servlet ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext)); dispatcher.setLoadOnStartup(1); dispatcher.addMapping("/"); } }
?
rootContext.scan("com.demo.config.spring")
?
这是用来扫描ApplicationContext的,也使用no xml实现,所以这里是扫package
?
Class风格的ApplicationContext
import java.util.Properties; import javax.sql.DataSource; import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.annotation.MapperScan; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.JstlView; import com.demo.config.constant.BaseConst; import com.demo.config.constant.MySQLConst; @Configuration @Profile(value = { "default" }) @EnableWebMvc @EnableScheduling @EnableTransactionManagement @MapperScan(basePackages = "com.demo.dao") @ComponentScan(basePackages = "com.demo") public class WebContext extends WebMvcConfigurerAdapter{ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } @Bean public ViewResolver viewResolver(){ InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/pages/"); viewResolver.setSuffix(".jsp"); return viewResolver; } @Bean(name = "dataSourceMySQL") public DataSource dataSourceMySQL() { DriverManagerDataSource source = new DriverManagerDataSource(); source.setUrl(MySQLConst.MYSQL_URL); source.setDriverClassName(MySQLConst.MYSQL_DRIVER); source.setUsername(MySQLConst.MYSQL_USER_NAME); source.setPassword(MySQLConst.MYSQL_PASSWORD); return source; } @Bean public JdbcTemplate jdbcTemplate(DataSource dataSourceMySQL){ return new JdbcTemplate(dataSourceMySQL); } @Bean(name = "sqlSessionFactory") public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSourceMySQL){ SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSourceMySQL); sqlSessionFactory.setTypeAliasesPackage("com.demo.entity"); return sqlSessionFactory; } @Bean public PlatformTransactionManager transactionManager(DataSource dataSourceMySQL) { DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(); transactionManager.setDataSource(dataSourceMySQL); return transactionManager; } @Bean(name = "hibernateSessionFactory") public LocalSessionFactoryBean localSessionFactoryBean(DataSource dataSourceMySQL) { LocalSessionFactoryBean sessionFactoryBean = new LocalSessionFactoryBean(); sessionFactoryBean.setDataSource(dataSourceMySQL); sessionFactoryBean.setPackagesToScan("com.demo.entity"); Properties properties = new Properties(); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); properties.setProperty("hibernate.show_sql", BaseConst.HIBERNATE_SHOW_SQL.toString()); properties.setProperty("hibernate.default_schema", "demo"); properties.setProperty("connection.driver_class", MySQLConst.MYSQL_DRIVER); properties.setProperty("connection.url", MySQLConst.MYSQL_URL); properties.setProperty("connection.username", MySQLConst.MYSQL_USER_NAME); properties.setProperty("connection.password", MySQLConst.MYSQL_PASSWORD); sessionFactoryBean.setHibernateProperties(properties); return sessionFactoryBean; } @Bean public HibernateTemplate hibernateTemplate(LocalSessionFactoryBean hibernateSessionFactory){ return new HibernateTemplate(hibernateSessionFactory.getObject()); } @Bean(name = "threadPoolTaskExecutor") public ThreadPoolTaskExecutor threadPoolTaskExecutor(){ ThreadPoolTaskExecutor poolTaskExecutor = new ThreadPoolTaskExecutor(); poolTaskExecutor.setCorePoolSize(5); //线程池维护线程的最少数量 poolTaskExecutor.setQueueCapacity(5); //线程池所使用的缓冲队列 poolTaskExecutor.setMaxPoolSize(10); //线程池维护线程的最大数量 poolTaskExecutor.setKeepAliveSeconds(30000); //线程池维护线程所允许的空闲时间 return poolTaskExecutor; }
?
整合MySQL,Hibernate,MyBatis,顺便配置了一下Spring事务管理和Spring线程池
?
单元测试的配置如下:
package com.demo.dao; import org.apache.ibatis.session.SqlSessionFactory; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.annotation.Commit; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.transaction.annotation.Transactional; import com.demo.config.spring.WebContext; import com.demo.entity.Application; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = WebContext.class) @WebAppConfiguration @Transactional public class ApplicationTest { @Autowired private SqlSessionFactory sqlSessionFactory; @Commit @Test public void testInsert() { Application app = new Application(); sqlSessionFactory.openSession().getMapper(ApplicationMapper.class).insert(app); } }
?
以上
原文:http://neverflyaway.iteye.com/blog/2295224