|
@ -2,10 +2,8 @@ package com.yihu.hos.config;
|
|
|
|
|
|
import com.yihu.hos.interceptor.AuditInterceptor;
|
|
|
import org.apache.commons.dbcp2.BasicDataSource;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
|
|
import org.springframework.context.ApplicationContextException;
|
|
|
import org.springframework.context.EnvironmentAware;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
@ -25,7 +23,6 @@ import org.springframework.web.servlet.i18n.CookieLocaleResolver;
|
|
|
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
import java.util.Properties;
|
|
@ -41,15 +38,15 @@ import java.util.Properties;
|
|
|
//@ImportResource({"classpath:spring/applicationContext.xml"}) //applicationContext相关bean创建
|
|
|
public class BeanConfig implements EnvironmentAware {
|
|
|
|
|
|
// @Autowired
|
|
|
// BasicDataSource dataSource;
|
|
|
@Autowired
|
|
|
BasicDataSource dataSource;
|
|
|
|
|
|
@Value("${spring.jpa.hibernate.dialect}")
|
|
|
private String dialect;
|
|
|
@Value("${spring.jpa.format-sql}")
|
|
|
private String formatSql;
|
|
|
@Value("${spring.jpa.show-sql}")
|
|
|
private String showSql;
|
|
|
// @Value("${spring.jpa.hibernate.dialect}")
|
|
|
// private String dialect;
|
|
|
// @Value("${spring.jpa.format-sql}")
|
|
|
// private String formatSql;
|
|
|
// @Value("${spring.jpa.show-sql}")
|
|
|
// private String showSql;
|
|
|
|
|
|
private Environment environment;
|
|
|
private Map<String , Object> hibernatePropertyResolver;
|
|
@ -85,11 +82,11 @@ public class BeanConfig implements EnvironmentAware {
|
|
|
@Bean
|
|
|
public LocalSessionFactoryBean sessionFactory() {
|
|
|
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
|
|
|
localSessionFactoryBean.setDataSource(dataSource());
|
|
|
localSessionFactoryBean.setDataSource(this.dataSource);
|
|
|
Properties properties1 = new Properties();
|
|
|
properties1.setProperty("hibernate.dialect",dialect);
|
|
|
properties1.setProperty("hibernate.show_sql", showSql);
|
|
|
properties1.setProperty("hibernate.format_sql",formatSql);
|
|
|
properties1.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
|
|
|
properties1.setProperty("hibernate.show_sql", "false");
|
|
|
properties1.setProperty("hibernate.format_sql","true");
|
|
|
localSessionFactoryBean.setHibernateProperties(properties1);
|
|
|
localSessionFactoryBean.setPackagesToScan("com.yihu.hos.*.model");
|
|
|
ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
@ -100,28 +97,28 @@ public class BeanConfig implements EnvironmentAware {
|
|
|
return localSessionFactoryBean;
|
|
|
}
|
|
|
|
|
|
@Bean( destroyMethod = "close")
|
|
|
public BasicDataSource dataSource() {
|
|
|
if (StringUtils.isEmpty(datasourcePropertyResolver.get("url").toString())) {
|
|
|
System.out.println("Your database connection pool configuration is incorrect!" +
|
|
|
" Please check your Spring profile, current profiles are:"+
|
|
|
Arrays.toString(environment.getActiveProfiles()));
|
|
|
throw new ApplicationContextException(
|
|
|
"Database connection pool is not configured correctly");
|
|
|
}
|
|
|
BasicDataSource dataSource = new BasicDataSource();
|
|
|
dataSource.setUrl(datasourcePropertyResolver.get("url").toString());
|
|
|
dataSource.setUsername(datasourcePropertyResolver.get("username").toString());
|
|
|
dataSource.setPassword(datasourcePropertyResolver.get("password").toString());
|
|
|
dataSource.setInitialSize((Integer)datasourcePropertyResolver.get("initial-size"));
|
|
|
dataSource.setMaxTotal((Integer)datasourcePropertyResolver.get("max-total"));
|
|
|
dataSource.setMinIdle((Integer)datasourcePropertyResolver.get("min-idle"));
|
|
|
dataSource.setMaxIdle((Integer)datasourcePropertyResolver.get("max-idle"));
|
|
|
dataSource.setValidationQuery(datasourcePropertyResolver.get("validation-query").toString());
|
|
|
dataSource.setRemoveAbandonedTimeout(55);
|
|
|
dataSource.setTestOnBorrow((boolean)datasourcePropertyResolver.get("test-on-borrow"));
|
|
|
return dataSource;
|
|
|
}
|
|
|
// @Bean( destroyMethod = "close")
|
|
|
// public BasicDataSource dataSource() {
|
|
|
// if (StringUtils.isEmpty(datasourcePropertyResolver.get("url").toString())) {
|
|
|
// System.out.println("Your database connection pool configuration is incorrect!" +
|
|
|
// " Please check your Spring profile, current profiles are:"+
|
|
|
// Arrays.toString(environment.getActiveProfiles()));
|
|
|
// throw new ApplicationContextException(
|
|
|
// "Database connection pool is not configured correctly");
|
|
|
// }
|
|
|
// BasicDataSource dataSource = new BasicDataSource();
|
|
|
// dataSource.setUrl(datasourcePropertyResolver.get("url").toString());
|
|
|
// dataSource.setUsername(datasourcePropertyResolver.get("username").toString());
|
|
|
// dataSource.setPassword(datasourcePropertyResolver.get("password").toString());
|
|
|
// dataSource.setInitialSize((Integer)datasourcePropertyResolver.get("initial-size"));
|
|
|
// dataSource.setMaxTotal((Integer)datasourcePropertyResolver.get("max-total"));
|
|
|
// dataSource.setMinIdle((Integer)datasourcePropertyResolver.get("min-idle"));
|
|
|
// dataSource.setMaxIdle((Integer)datasourcePropertyResolver.get("max-idle"));
|
|
|
// dataSource.setValidationQuery(datasourcePropertyResolver.get("validation-query").toString());
|
|
|
// dataSource.setRemoveAbandonedTimeout(55);
|
|
|
// dataSource.setTestOnBorrow((boolean)datasourcePropertyResolver.get("test-on-borrow"));
|
|
|
// return dataSource;
|
|
|
// }
|
|
|
|
|
|
//txManager事务开启
|
|
|
@Bean
|
|
@ -167,7 +164,7 @@ public class BeanConfig implements EnvironmentAware {
|
|
|
public JdbcTemplate jdbcTemplate(){
|
|
|
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
|
|
try {
|
|
|
jdbcTemplate.setDataSource(this.dataSource());
|
|
|
jdbcTemplate.setDataSource(this.dataSource);
|
|
|
jdbcTemplate.setLazyInit(false);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|