1. sessionFactory 的配置
@Configuration
//启用注解事务管理,使用CGLib代理
@EnableTransactionManagement(proxyTargetClass = true)
@Import({DataSourceConfig.class})
public class DaoConfig {
private static final Logger logger = Logger.getLogger(DaoConfig.class);
@Resource(name = "dataSource")
public DataSource dataSource;
@Value("${hibernate.dialect}")
String hibernate_dialect;
@Value("${hibernate.show_sql}")
String hibernate_show_sql;
/**
* 描述 : <负责解析资源文件>. <br>
* <p>
* <这个类必须有,而且必须声明为static,否则不能正常解析>
* </p>
*
* @return
*/
@Bean
public static PropertySourcesPlaceholderConfigurer placehodlerConfigurer() {
logger.info("PropertySourcesPlaceholderConfigurer");
return new PropertySourcesPlaceholderConfigurer();
}
@Bean(name = "hibernateDAO")
public CP_Hibernate4DAOImpl hibernate4Dao() {
logger.info("hibernateDAO");
CP_Hibernate4DAOImpl dao = new CP_Hibernate4DAOImpl();
dao.setSessionFactory(localSessionFactoryBean().getObject());
return dao;
}
@Bean(name = "sessionFactory")
public LocalSessionFactoryBean localSessionFactoryBean() {
logger.info("sessionFactory");
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource);
String[] packagesToScan = new String[]{"com.mvc.entity"};
sessionFactory.setPackagesToScan(packagesToScan);
Properties hibernateProperties = new Properties();
hibernateProperties.setProperty("hibernate.dialect", hibernate_dialect);
hibernateProperties.setProperty("hibernate.show_sql",
hibernate_show_sql);
hibernateProperties.setProperty(
"hibernate.current_session_context_class",
"org.springframework.orm.hibernate5.SpringSessionContext");
// 二级缓存
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", "true");
hibernateProperties.setProperty("hibernate.cache.use_query_cache", "true");
// hibernateProperties.setProperty("hibernate.cache.provider_class",
// "org.hibernate.cache.EhCacheProvider");
// hibernateProperties.setProperty("hibernate.cache.region.factory_class",
// "org.hibernate.cache.ehcache.EhCacheRegionFactory");
hibernateProperties.setProperty("hibernate.cache.region.factory_class",
"org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory");
sessionFactory.setHibernateProperties(hibernateProperties);
return sessionFactory;
}
2. datasource配置
@Configuration
//db 配置文件
@PropertySource({"classpath:config/properties/db.properties"})
public class DataSourceConfig {
private static final Logger logger = Logger.getLogger(DataSourceConfig.class);
/*
*db 配置
*/
@Value("${jdbc.driver}")
String driverClass;
@Value("${jdbc.url}")
String url;
@Value("${jdbc.username}")
String userName;
@Value("${jdbc.password}")
String passWord;
@Bean(name = "dataSource")
public DataSource dataSource() {
logger.info("DataSource");
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(driverClass);
dataSource.setUrl(url);
dataSource.setUsername(userName);
dataSource.setPassword(passWord);
return dataSource;
}
}
// ehcache 配置配置
@Configuration
@EnableCaching//<!-- 启用缓存注解 --> <cache:annotation-driven cache-manager="cacheManager" />
public class CachingConfig {
private static final Logger logger = Logger.getLogger(CachingConfig.class);
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() {
EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource(
"config/ehcache.xml"));
ehCacheManagerFactoryBean.setShared(true);
return ehCacheManagerFactoryBean;
}
@Bean
public CacheManager cacheManager() {
logger.info("EhCacheCacheManager");
EhCacheCacheManager cacheManager = new EhCacheCacheManager();
cacheManager.setCacheManager(ehCacheManagerFactoryBean().getObject());
return cacheManager;
}
}
实体配置:
@Entity
@Table(name="UserInfo")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class UserInfo {
}
整合时发生以下错误:
1 . Unable to create requested service [org.hibernate.cache.spi.RegionFactory]
原因: hibernate.cache.region.factory_class 属性没有配置, 这个属于hibernate版本4以上的配置, 3版本配置 hibernate.cache.provider_class
2. Another unnamed CacheManager already exists in the same VM.
原因 ehcache在2.5以后,CacheManager使用了Singleton,这样在创建多个CacheManager时就会产生上述错误。ehCacheManagerFactoryBean.setShared(true);
于是我尝试找到ehcache2.5 以前的jar包进行尝试,对于gradle 自动下载compile("org.hibernate:hibernate-ehcache:5.1.7.Final") 自动下载hibernate依赖会连
同ehcache也自动关联一个版本, 所以使用本地jar加载,创建src 同级目录,在build.gradle 中添加 compile fileTree(dir:'libs',include:['*.jar']), 去掉ehCacheManagerFactoryBean.setShare(true),
同时还要加载
compile("org.apache.logging.log4j:log4j-core:2.8.2")
compile("log4j:log4j:1.2.17")
这两个日志依赖,才能正常运行