@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;
}
}