package db.config; import com.alibaba.druid.pool.DruidDataSource; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.hibernate.SessionFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.util.ObjectUtils; import org.springframework.util.ReflectionUtils; import javax.annotation.PostConstruct; import javax.sql.DataSource; import java.io.IOException; import java.lang.reflect.Method; import java.util.Properties; /** * Created by reborn on 2017/7/28. */ public class HibernateConfig{ private static Logger log=LogManager.getLogger(); @Value("${hibernate.dialect}") private String dialect; @Value("${hibernate.connection.driver_class}") private String driver; @Value("${hibernate.connection.url}") private String url; @Value("${hibernate.connection.username}") private String username; @Value("${hibernate.connection.password}") private String password; @Value("${hibernate.connection.url2}") private String url2; @Value("${hibernate.connection.username2}") private String username2; @Value("${hibernate.connection.password2}") private String password2; @Value("${sessionFactory.scan}") private String packagesToScan[]; private static final String tableNote="tableNote"; private LocalSessionFactoryBean aliyun = new LocalSessionFactoryBean(); private LocalSessionFactoryBean huaWeiCloud = new LocalSessionFactoryBean(); private DruidDataSource dataSource(String url,String username,String password){ DruidDataSource dataSource=new DruidDataSource(); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setDriverClassName(driver); log.info("数据源初始化\nurl="+url+"\nusername="+username+"\npassword="+password); return dataSource; } @PostConstruct private void init() { initSessionFactory(aliyun,dataSource(url,username,password)); initSessionFactory(huaWeiCloud,dataSource(url2,username2,password2)); } private void initSessionFactory(LocalSessionFactoryBean bean, DataSource dataSource){ Properties properties=new Properties(); properties.setProperty("hibernate.dialect",dialect); properties.setProperty("hibernate.format_sql","true"); bean.setHibernateProperties(properties); bean.setDataSource(dataSource); bean.setPackagesToScan(packagesToScan); try { bean.afterPropertiesSet(); checkClass(bean); } catch (IOException e) { e.printStackTrace(); } } @Bean("aliyun") public SessionFactory aliyun(){ return aliyun.getObject(); } @Bean("huaWeiCloud") public SessionFactory huaWeiCloud(){ return huaWeiCloud.getObject(); } @Bean("transactionManager1") public HibernateTransactionManager aliyunTransaction() { HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(aliyun()); return hibernateTransactionManager; } @Bean("transactionManager2") public HibernateTransactionManager huaweiTransaction() { HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager(); hibernateTransactionManager.setSessionFactory(huaWeiCloud()); return hibernateTransactionManager; } private void checkClass(LocalSessionFactoryBean bean){ for(Class c:bean.getMetadataSources().getAnnotatedClasses()){ try { Method method=ReflectionUtils.findMethod(c,tableNote); Object result=ReflectionUtils.invokeMethod(method,c.newInstance()); if(ObjectUtils.isEmpty(result)){ throw new RuntimeException(method+"没有正确重写"); } } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } } } }