Spring事物不起作用

身有光明 2015-08-28 05:48:10


@Configuration
@ComponentScan(basePackages = {"com.xyz.business", "com.xyz.base"},
excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION,
value = {Controller.class, ControllerAdvice.class})})
@EnableAspectJAutoProxy
@Import({ SpringSchedulingConfig.class,
SpringCachingConfig.class})
//@EnableAsync
@EnableTransactionManagement(proxyTargetClass = true,mode = AdviceMode.PROXY)
//@EnableSpringConfigured
//@EnableSpringConfigured Coordinate @Configurable(autowire=Autowire.BY_TYPE,
// dependencyCheck=false) Non-IOC management need to inject the object's
// class added
//@EnableLoadTimeWeaving
//@PropertySources(value = {@PropertySource(name="jdbc",
// value="classpath:/conf/jdbc/config.properties")})//加载资源文件
public class SpringConfig {

private static final Logger logger = LoggerFactory.getLogger
(SpringSqlSessionFactoryConfig.class);
/**
* 负责解析资源文件,这个类必须有,而且必须声明为静态的,否则无法正确解析
*
* @return PropertySourcesPlaceholderConfigurer
*
* @throws IOException IO Exception Information
*/
@Bean
public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer
() throws IOException {
PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer = new
PropertySourcesPlaceholderConfigurer();
propertySourcesPlaceholderConfigurer.setIgnoreResourceNotFound(false);
return propertySourcesPlaceholderConfigurer;
}
/**
* 数据源配置,采用阿里Druid
*
* @return DruidDataSource
*/
@Bean(name = BEAN_DATASOURCE, destroyMethod = "close", initMethod = "init")
public DruidDataSource dataSource() {
logger.info("Druid DataSource init");
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(DATA_SOURCE_CLASS_NAME);
dataSource.setUrl(DATA_SOURCE_URL);
dataSource.setUsername(DATA_SOURCE_USER_NAME);
dataSource.setPassword(DATA_SOURCE_PASSWORD);
dataSource.setDbType(DATA_SOURCE_DB_TYPE);
dataSource.setInitialSize(DATA_SOURCE_INITIAL_SIZE);
dataSource.setMinIdle(DATA_SOURCE_MIN_IDLE);
dataSource.setMaxActive(DATA_SOURCE_MAX_ACTIVE);
dataSource.setMaxWait(DATA_SOURCE_MAX_WAIT);
dataSource.setTimeBetweenEvictionRunsMillis
(DATA_SOURCE_TIME_BETWEEN_EVICTION_RUNS_MILLIS);
dataSource.setMinEvictableIdleTimeMillis
(DATA_SOURCE_MIN_EVICTABLE_IDLE_TIME_MILLIS);
dataSource.setValidationQuery(DATA_SOURCE_VALIDATION_QUERY);
dataSource.setTestWhileIdle(DATA_SOURCE_TEST_WHILE_IDLE);
dataSource.setTestOnBorrow(DATA_SOURCE_TEST_ON_BORROW);
dataSource.setTestOnReturn(DATA_SOURCE_TEST_ON_RETURN);
dataSource.setRemoveAbandoned(DATA_SOURCE_REMOVE_ABANDONED);
dataSource.setRemoveAbandonedTimeout
(DATA_SOURCE_REMOVE_ABANDONED_TIMEOUT);
dataSource.setLogAbandoned(DATA_SOURCE_LOG_ABANDONED);
dataSource.setPoolPreparedStatements
(DATA_SOURCE_POOL_PREPARED_STATEMENTS);
dataSource.setMaxPoolPreparedStatementPerConnectionSize
(DATA_SOURCE_MAX_POOL_PREPARED_STATEMENT_PER_CONNECTION_SIZE);
try {
dataSource.setFilters(DATA_SOURCE_FILTERS);
} catch (SQLException e) {
logger.error("init Druid DataSource set Filters Error:", e);
}
return dataSource;
}


/**
* 创建 SqlSessionFactory
*
* @return SqlSessionFactory
*/
@Bean(name = BEAN_SQL_SESSION_FACTORY)
protected SqlSessionFactory sqlSessionFactory() {
final SqlSessionFactoryBean sqlSessionFactoryBean = new
SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource());
sqlSessionFactoryBean.setMapperLocations(mapperLocations());
sqlSessionFactoryBean.setTypeAliasesPackage(TYPE_ALIAS_EASE_PACKAGE);
sqlSessionFactoryBean.setPlugins(new
Interceptor[]{initPageHelperPlugin(),
initMapperInterceptorPlugin()});
SqlSessionFactory sqlSessionFactory = null;
try {
sqlSessionFactory = sqlSessionFactoryBean.getObject();
} catch (Exception e) {
logger.error("Get SqlSessionFactory Error:", e);
}
return sqlSessionFactory;
}

/**
* 加载Mybatis Mapping文件集合
*
* @return Resource[]
*/
protected org.springframework.core.io.Resource[] mapperLocations() {
org.springframework.core.io.Resource[] resources = new org
.springframework.core.io.Resource[0];
try {
resources = new PathMatchingResourcePatternResolver()
.getResources(MAPPER_LOCATIONS);
} catch (IOException e) {
logger.error("Load Mybatis Mapping Resources Error:", e);
}
return resources;
}

/**
* Mybatis Mapper 扫描器 </br> <p> basePackage
* 属性是让你为映射器接口文件设置基本的包路径。你可以使用分号或逗号作为分隔符设置多于一个的包路径。每个映射器将会在指定的包路径中递归地被搜索到。
* </p>
*
* @return MapperScannerConfigurer
*/
@Bean
protected MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new
MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage(MAPPER_BASE_PACKAGE);
mapperScannerConfigurer.setSqlSessionFactoryBeanName
(BEAN_SQL_SESSION_FACTORY);
return mapperScannerConfigurer;
}

/**
* 事务管理
*
* @return DataSourceTransactionManager
*/
@Bean(name = BEAN_TRANSACTION_MANAGER)
protected DataSourceTransactionManager transactionManager() {
DataSourceTransactionManager dataSourceTransactionManager = new
DataSourceTransactionManager();
dataSourceTransactionManager.setDataSource(dataSource());
return dataSourceTransactionManager;
}



/*
* $[methodName] - replaced with the name of the method being
* invoked</br>
* $[targetClassName] - replaced with the name of the class that is
* the target of the invocation</br>
* $[targetClassShortName] - replaced with the short name of the
* class that is the target of the invocation</br>
* $[returnValue] - replaced with the value returned by the
* invocation</br>
* $[argumentTypes] - replaced with a comma-separated list of the
* short class names of the method arguments</br>
* $[arguments] - replaced with a comma-separated list of the String
* representation of the method arguments</br>
* $[exception] - replaced with the String representation of any
* Throwable raised during the invocation</br>
* $[invocationTime] - replaced with the time, in milliseconds, taken
* by the method invocation</br>
*/

/**
* TODO:
*
* @return CustomizableTraceInterceptor
*/
/* @Bean
public CustomizableTraceInterceptor customizableTraceInterceptor() {
CustomizableTraceInterceptor customizableTraceInterceptor = new
CustomizableTraceInterceptor();
logger.info("--------------CustomizableTraceInterceptor" +
"----------------------");
// customizableTraceInterceptor.setUseDynamicLogger(true);
customizableTraceInterceptor.setEnterMessage("Entering $[methodName]" +
"($[arguments])");
customizableTraceInterceptor.setExitMessage("Leaving $[methodName]()" +
", returned $[returnValue]");
customizableTraceInterceptor.setExceptionMessage
("kkkkkkkkk $[exception],Entering $[methodName]($[arguments])");
return customizableTraceInterceptor;
}*/

/**
* Cut point Advisor
*
* @return Advisor
*/
/* @Bean
public Advisor jpaRepositoryAdvisor() {

AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* com.xyz.business.service..*.*(..)" +
")");
return new DefaultPointcutAdvisor(pointcut,
customizableTraceInterceptor());
}*/

/**
* 初始化Mybatis 分页拦截器插件
*
* @return PageHelper
*/
protected PageHelper initPageHelperPlugin() {
PageHelper pageHelper = new PageHelper();
Properties properties = new Properties();
properties.put(PAGE_PLUGIN_DIALECT_NAME, PAGE_PLUGIN_DIALECT_VALUE);
properties.put(PAGE_PLUGIN_REASONABLE_NAME,
PAGE_PLUGIN_REASONABLE_VALUE);
pageHelper.setProperties(properties);
return pageHelper;
}

/**
* 初始化Mybatis Mapper 拦截器插件
*
* @return MapperInterceptor
*/
protected MapperInterceptor initMapperInterceptorPlugin() {
MapperInterceptor mapperInterceptor = new MapperInterceptor();
Properties properties = new Properties();
properties.put(MAPPER_PLUGIN_MAPPERS_NAME, MAPPER_PLUGIN_MAPPERS_VALUE);
properties.put(MAPPER_PLUGIN_IDENTITY_NAME,
MAPPER_PLUGIN_IDENTITY_VALUE);
properties.put(MAPPER_PLUGIN_NO_EMPTY_NAME,
MAPPER_PLUGIN_NO_EMPTY_VALUE);
mapperInterceptor.setProperties(properties);
return mapperInterceptor;
}

}
...全文
105 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
方法体上面加上@Transactional(propagation = Propagation.REQUIRED, rollbackFor = { Exception.class })这个注解
身有光明 2015-08-28
  • 打赏
  • 举报
回复
执行了一条插入一条更新,更新故意弄出错的,但插入竟然插进数据库了没有执行事物
第1章:对Spring框架进行宏观性的概述,力图使读者建立起对Spring整体性的认识。   第2章:通过一个简单的例子展现开发Spring Web应用的整体过程,通过这个实例,读者可以快速跨入Spring Web应用的世界。   第3章:讲解Spring IoC容器的知识,通过具体的实例详细地讲解IoC概念。同时,对Spring框架的三个最重要的框架级接口进行了剖析,并对Bean的生命周期进行讲解。   第4章:讲解如何在Spring配置文件中使用Spring 3.0的Schema格式配置Bean的内容,并对各个配置项的意义进行了深入的说明。   第5章:对Spring容器进行解构,从内部探究Spring容器的体系结构和运行流程。此外,我们还将对Spring容器一些高级主题进行深入的阐述。   第6章:我们从Spring AOP的底层实现技术入手,一步步深入到Spring AOP的内核中,分析它的底层结构和具体实现。   第7章:对如何使用基于AspectJ配置AOP的知识进行了深入的分析,这包括使用XML Schema配置文件、使用注解进行配置等内容。   第8章:介绍了Spring所提供的DAO封装层,这包括Spring DAO的异常体系、数据访问模板等内容。   第9章:介绍了Spring事务管理的工作机制,通过XML、注解等方式进行事务管理配置,同时还讲解了JTA事务配置知识。   第10章:对实际应用中Spring事务管理各种疑难问题进行透彻的剖析,让读者对Spring事务管理不再有云遮雾罩的感觉。   第11章:讲解了如何使用Spring JDBC进行数据访问操作,我们还重点讲述了LOB字段处理、主键产生和获取等难点知识。   第12章:讲解了如何在Spring中集成Hibernate、myBatis等数据访问框架,同时,读者还将学习到ORM框架的混用和DAO层设计的知识。   第13章:本章重点对在Spring中如何使用Quartz进行任务调度进行了讲解,同时还涉及了使用JDK Timer和JDK 5.0执行器的知识。   第14章:介绍Spring 3.0新增的OXM模块,同时对XML技术进行了整体的了解。   第15章:对Spring MVC框架进行详细介绍,对REST风格编程方式进行重点讲解,同时还对Spring 3.0的校验和格式化框架如果和Spring MVC整合进行讲解。   第16章:有别于一般书籍的单元测试内容,本书以当前最具实战的JUnit4+Unitils+ Mockito复合测试框架对如何测试数据库、Web的应用进行了深入的讲解。   第17章:以一个实际的项目为蓝本,带领读者从项目需求分析、项目设计、代码开发、单元测试直到应用部署经历整个实际项目的整体开发过程。

81,095

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧