不使用任何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); } }
以上
相关推荐
spring xml配置的12個技巧 spring xml配置的12個技巧 spring xml配置的12個技巧
服务Spring利用依赖注入机制来简化工作同时提高易测性Springbeans及依赖以及beans类所需服务都在配置 文件中进行了详细介绍说明这个配置文件是典型XML格式但是它既冗长又不实用对于需要定义大量 Springbeans大工程来...
spring bean配置 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring框架xml注解配置方式实例,包括Maven配置文件pom.xml、Springxml配置文件ApplicationContext-1.xml,以及类源码
Spring基于XML方式配置事务
logback-spring.xml文件配置,1、异步日志,2、滚动日志,存放固定时长的日志,超过时间的自动删除,3、单个文件超过指定大小,分成多个,防止单个文件过大,查看不方便
SpringAop xml方式配置通知
SSH三大框架整合 struts2(使用xml配置)+hibernate(使用xml配置)+spring(使用xml配置),实现分页查询,其他增删该查的接口已经写好。
模拟spring的xml配置文件注入对象,里面用saxbuilder来解析xml文件。
用于日志配置
spring mvc+spring security+自定义form表单+验证码+bootstrap
自己总结的spring xml配置的思维导图,包括了spring的基础配置
Spring使用XML配置声明式事务 ,具体效果和过程看博文 http://blog.csdn.net/evankaka/article/details/45478007
Spring-hibernate结合使用全xml配置方式
ssm配置文件spring.xml
Spring XML配置的12个技巧.doc
SSH三大框架整合 struts 2(xml配置)+hibernate(注解)+spring(xml配置),访问url,实现简单查询功能,(通过控制台输出数据)
主要是讲spring自定义xml配置的扩展相关的内容,东西不算新的,都是比较基础的,想看就看一下 我也是为了搞点积分才上传的
使用Idea工具建立的Maven工程,整合Spring和MyBatis,成功通过单元测试
Spring中XML配置文件的十二个最佳方法.doc Spring中XML配置文件的十二个最佳方法.doc