|
@ -1,7 +1,34 @@
|
|
|
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.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;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.context.annotation.ImportResource;
|
|
|
import org.springframework.context.support.ResourceBundleMessageSource;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.core.io.DefaultResourceLoader;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
import org.springframework.core.io.ResourceLoader;
|
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
|
|
import org.springframework.orm.hibernate4.HibernateTemplate;
|
|
|
import org.springframework.orm.hibernate4.HibernateTransactionManager;
|
|
|
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
|
|
|
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
|
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
|
|
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;
|
|
|
|
|
|
/**
|
|
|
* @author HZY
|
|
@ -9,149 +36,157 @@ import org.springframework.context.annotation.ImportResource;
|
|
|
* Created at 2016/8/5.
|
|
|
*/
|
|
|
@Configuration
|
|
|
//@EnableTransactionManagement
|
|
|
//@ComponentScan("com.yihu.hos")
|
|
|
@ImportResource({"classpath:spring/applicationContext.xml"}) //applicationContext相关bean创建
|
|
|
public class BeanConfig{
|
|
|
|
|
|
|
|
|
// private Environment environment;
|
|
|
// private RelaxedPropertyResolver datasourcePropertyResolver;
|
|
|
// private RelaxedPropertyResolver hibernatePropertyResolver;
|
|
|
//
|
|
|
//
|
|
|
// //从application.yml中读取资源
|
|
|
// @Override
|
|
|
// public void setEnvironment(Environment environment) {
|
|
|
// this.environment = environment;
|
|
|
// this.datasourcePropertyResolver = new RelaxedPropertyResolver(environment, "spring.datasource.");
|
|
|
// this.hibernatePropertyResolver = new RelaxedPropertyResolver(environment, "spring.jpa.");
|
|
|
//
|
|
|
// }
|
|
|
//
|
|
|
// //datasource 池方式
|
|
|
//// @Bean(initMethod = "init", destroyMethod = "close")
|
|
|
//// public DataSource dataSource() throws SQLException {
|
|
|
//// if (StringUtils.isEmpty(datasourcePropertyResolver.getProperty("url"))) {
|
|
|
//// 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");
|
|
|
//// }
|
|
|
//// DruidDataSource druidDataSource = new DruidDataSource();
|
|
|
//// druidDataSource.setUrl(datasourcePropertyResolver.getProperty("url"));
|
|
|
//// druidDataSource.setUsername(datasourcePropertyResolver
|
|
|
//// .getProperty("username"));
|
|
|
//// druidDataSource.setPassword(datasourcePropertyResolver
|
|
|
//// .getProperty("password"));
|
|
|
//// druidDataSource.setInitialSize(1);
|
|
|
//// druidDataSource.setMinIdle(1);
|
|
|
//// druidDataSource.setMaxActive(20);
|
|
|
//// druidDataSource.setMaxWait(60000);
|
|
|
//// druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
|
|
|
//// druidDataSource.setMinEvictableIdleTimeMillis(300000);
|
|
|
//// druidDataSource.setValidationQuery("SELECT 'x'");
|
|
|
//// druidDataSource.setTestWhileIdle(true);
|
|
|
//// druidDataSource.setTestOnBorrow(false);
|
|
|
//// druidDataSource.setTestOnReturn(false);
|
|
|
//// return druidDataSource;
|
|
|
//// }
|
|
|
//
|
|
|
// @Bean( destroyMethod = "close")
|
|
|
// public BasicDataSource dataSource() throws SQLException {
|
|
|
// if (StringUtils.isEmpty(datasourcePropertyResolver.getProperty("url"))) {
|
|
|
// 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.getProperty("url"));
|
|
|
// dataSource.setUsername(datasourcePropertyResolver.getProperty("username"));
|
|
|
// dataSource.setPassword(datasourcePropertyResolver.getProperty("password"));
|
|
|
// dataSource.setInitialSize(datasourcePropertyResolver.getProperty("initial-size", Integer.class));
|
|
|
// dataSource.setMaxTotal(datasourcePropertyResolver.getProperty("max-total", Integer.class));
|
|
|
// dataSource.setMinIdle(datasourcePropertyResolver.getProperty("min-idle",Integer.class));
|
|
|
// dataSource.setMaxIdle(datasourcePropertyResolver.getProperty("max-idle",Integer.class));
|
|
|
// dataSource.setValidationQuery(datasourcePropertyResolver.getProperty("validation-query"));
|
|
|
// dataSource.setRemoveAbandonedTimeout(55);
|
|
|
// dataSource.setTestOnBorrow(datasourcePropertyResolver.getProperty("test-on-borrow",Boolean.class));
|
|
|
// return dataSource;
|
|
|
// }
|
|
|
//
|
|
|
// //sessionFactory
|
|
|
@EnableTransactionManagement
|
|
|
@ComponentScan("com.yihu.hos")
|
|
|
//@ImportResource({"classpath:spring/applicationContext.xml"}) //applicationContext相关bean创建
|
|
|
public class BeanConfig implements EnvironmentAware {
|
|
|
|
|
|
// @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;
|
|
|
|
|
|
private Environment environment;
|
|
|
private Map<String , Object> hibernatePropertyResolver;
|
|
|
private Map<String , Object> datasourcePropertyResolver;
|
|
|
|
|
|
//从application.yml中读取资源
|
|
|
@Override
|
|
|
public void setEnvironment(Environment environment) {
|
|
|
this.environment = environment;
|
|
|
datasourcePropertyResolver = new RelaxedPropertyResolver(environment).getSubProperties("spring.datasource.");
|
|
|
this.hibernatePropertyResolver = new RelaxedPropertyResolver(environment).getSubProperties("spring.jpa.");
|
|
|
|
|
|
}
|
|
|
|
|
|
//sessionFactory
|
|
|
// @Bean
|
|
|
// public LocalSessionFactoryBean sessionFactory() throws SQLException {
|
|
|
// LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
|
|
|
// localSessionFactoryBean.setDataSource(this.dataSource());
|
|
|
// localSessionFactoryBean.setDataSource(this.dataSource);
|
|
|
// Properties properties1 = new Properties();
|
|
|
// properties1.setProperty("hibernate.dialect",hibernatePropertyResolver.getProperty("hibernate.dialect"));
|
|
|
// properties1.setProperty("hibernate.show_sql",hibernatePropertyResolver.getProperty("show-sql"));
|
|
|
// properties1.setProperty("hibernate.format_sql",hibernatePropertyResolver.getProperty("format-sql"));
|
|
|
// properties1.setProperty("hibernate.dialect",hibernatePropertyResolver.get("hibernate.dialect").toString());
|
|
|
// properties1.setProperty("hibernate.show_sql",hibernatePropertyResolver.get("show-sql").toString());
|
|
|
// properties1.setProperty("hibernate.format_sql",hibernatePropertyResolver.get("format-sql").toString());
|
|
|
// localSessionFactoryBean.setHibernateProperties(properties1);
|
|
|
// localSessionFactoryBean.setPackagesToScan("com.yihu.hos.standard.model");
|
|
|
// localSessionFactoryBean.setPackagesToScan("com.yihu.hos.*.model");
|
|
|
// ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
|
// Resource resource = resourceLoader.getResource("classpath:resource/");
|
|
|
// localSessionFactoryBean.setMappingDirectoryLocations(resource);
|
|
|
//// localSessionFactoryBean.setPackagesToScan("*");
|
|
|
// return localSessionFactoryBean;
|
|
|
// }
|
|
|
//
|
|
|
// //txManager事务开启
|
|
|
// @Bean
|
|
|
// public HibernateTransactionManager txManager() throws SQLException {
|
|
|
// HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
|
|
|
// hibernateTransactionManager.setSessionFactory(sessionFactory().getObject());
|
|
|
// return hibernateTransactionManager;
|
|
|
// }
|
|
|
//
|
|
|
// //文经上传
|
|
|
// @Bean
|
|
|
// public CommonsMultipartResolver multipartResolver(){
|
|
|
// return new CommonsMultipartResolver();
|
|
|
// }
|
|
|
//
|
|
|
// //国际化配置
|
|
|
// @Bean
|
|
|
// public ResourceBundleMessageSource messageSource(){
|
|
|
// ResourceBundleMessageSource messageSource =new ResourceBundleMessageSource();
|
|
|
// messageSource.setBasenames("text/message");
|
|
|
// messageSource.setDefaultEncoding("UTF-8");
|
|
|
// return messageSource;
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// public CookieLocaleResolver localeResolver(){
|
|
|
// CookieLocaleResolver localeResolver = new CookieLocaleResolver();
|
|
|
// localeResolver.setCookieName("Language");
|
|
|
// localeResolver.setCookieMaxAge(604800);
|
|
|
// localeResolver.setDefaultLocale(new Locale("zh_CN"));
|
|
|
// return localeResolver;
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// public LocaleChangeInterceptor localeChangeInterceptor(){
|
|
|
// return new LocaleChangeInterceptor();
|
|
|
// }
|
|
|
//
|
|
|
// @Bean
|
|
|
// public JdbcTemplate jdbcTemplate(){
|
|
|
// JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
|
|
// try {
|
|
|
// jdbcTemplate.setDataSource(this.dataSource());
|
|
|
// } catch (SQLException e) {
|
|
|
// e.printStackTrace();
|
|
|
// }
|
|
|
// return jdbcTemplate;
|
|
|
// }
|
|
|
//
|
|
|
// //Hibernate模版配置
|
|
|
// @Bean
|
|
|
// public HibernateTemplate hibernateTemplate() throws SQLException {
|
|
|
// HibernateTemplate hibernateTemplate = new HibernateTemplate();
|
|
|
// hibernateTemplate.setSessionFactory(this.sessionFactory().getObject());
|
|
|
// return hibernateTemplate;
|
|
|
// }
|
|
|
|
|
|
@Bean
|
|
|
public LocalSessionFactoryBean sessionFactory() {
|
|
|
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
|
|
|
localSessionFactoryBean.setDataSource(dataSource());
|
|
|
Properties properties1 = new Properties();
|
|
|
properties1.setProperty("hibernate.dialect",dialect);
|
|
|
properties1.setProperty("hibernate.show_sql", showSql);
|
|
|
properties1.setProperty("hibernate.format_sql",formatSql);
|
|
|
localSessionFactoryBean.setHibernateProperties(properties1);
|
|
|
localSessionFactoryBean.setPackagesToScan("com.yihu.hos.*.model");
|
|
|
ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
|
Resource resource = resourceLoader.getResource("resource/");
|
|
|
localSessionFactoryBean.setMappingDirectoryLocations(resource);
|
|
|
//设置拦截器
|
|
|
localSessionFactoryBean.setEntityInterceptor(this.auditInterceptor());
|
|
|
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;
|
|
|
}
|
|
|
|
|
|
//txManager事务开启
|
|
|
@Bean
|
|
|
public HibernateTransactionManager txManager() throws SQLException {
|
|
|
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
|
|
|
LocalSessionFactoryBean sessionFactoryBean = this.sessionFactory();
|
|
|
hibernateTransactionManager.setSessionFactory(sessionFactoryBean.getObject());
|
|
|
return hibernateTransactionManager;
|
|
|
}
|
|
|
|
|
|
//文经上传
|
|
|
@Bean
|
|
|
public CommonsMultipartResolver multipartResolver(){
|
|
|
return new CommonsMultipartResolver();
|
|
|
}
|
|
|
|
|
|
//国际化配置
|
|
|
@Bean
|
|
|
public ResourceBundleMessageSource messageSource(){
|
|
|
ResourceBundleMessageSource messageSource =new ResourceBundleMessageSource();
|
|
|
messageSource.setBasenames("text/message");
|
|
|
messageSource.setDefaultEncoding("UTF-8");
|
|
|
return messageSource;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
public CookieLocaleResolver localeResolver(){
|
|
|
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
|
|
|
localeResolver.setCookieName("Language");
|
|
|
localeResolver.setCookieMaxAge(604800);
|
|
|
localeResolver.setDefaultLocale(new Locale("zh_CN"));
|
|
|
return localeResolver;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public LocaleChangeInterceptor localeChangeInterceptor(){
|
|
|
return new LocaleChangeInterceptor();
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public JdbcTemplate jdbcTemplate(){
|
|
|
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
|
|
try {
|
|
|
jdbcTemplate.setDataSource(this.dataSource());
|
|
|
jdbcTemplate.setLazyInit(false);
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return jdbcTemplate;
|
|
|
}
|
|
|
|
|
|
//Hibernate模版配置
|
|
|
@Bean
|
|
|
public HibernateTemplate hibernateTemplate() throws SQLException {
|
|
|
HibernateTemplate hibernateTemplate = new HibernateTemplate();
|
|
|
LocalSessionFactoryBean sessionFactory = this.sessionFactory();
|
|
|
hibernateTemplate.setSessionFactory(sessionFactory.getObject());
|
|
|
return hibernateTemplate;
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public AuditInterceptor auditInterceptor(){
|
|
|
return new AuditInterceptor();
|
|
|
}
|
|
|
|
|
|
}
|