|
@ -3,13 +3,11 @@ package com.yihu.hos.config;
|
|
|
import com.yihu.hos.interceptor.AuditInterceptor;
|
|
|
import org.apache.commons.dbcp2.BasicDataSource;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
|
|
import org.springframework.context.EnvironmentAware;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.ComponentScan;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
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;
|
|
@ -21,10 +19,11 @@ 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 org.zbus.broker.ZbusBroker;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.sql.SQLException;
|
|
|
import java.util.Locale;
|
|
|
import java.util.Map;
|
|
|
import java.util.Properties;
|
|
|
|
|
|
/**
|
|
@ -36,57 +35,45 @@ import java.util.Properties;
|
|
|
@EnableTransactionManagement
|
|
|
@ComponentScan("com.yihu.hos")
|
|
|
//@ImportResource({"classpath:spring/applicationContext.xml"}) //applicationContext相关bean创建
|
|
|
public class BeanConfig implements EnvironmentAware {
|
|
|
public class BeanConfig {
|
|
|
|
|
|
@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("${hos.zbus.url}")
|
|
|
private String zbusUrl;
|
|
|
private ZbusBroker zbusBroker;
|
|
|
|
|
|
private Environment environment;
|
|
|
private Map<String , Object> hibernatePropertyResolver;
|
|
|
private Map<String , Object> datasourcePropertyResolver;
|
|
|
|
|
|
//从application.yml中读取资源
|
|
|
@Bean
|
|
|
public ZbusBroker getZbusBroker() {
|
|
|
try {
|
|
|
zbusBroker = new ZbusBroker(this.zbusUrl);
|
|
|
return zbusBroker;
|
|
|
} catch (IOException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public void setEnvironment(Environment environment) {
|
|
|
this.environment = environment;
|
|
|
datasourcePropertyResolver = new RelaxedPropertyResolver(environment).getSubProperties("spring.datasource.");
|
|
|
this.hibernatePropertyResolver = new RelaxedPropertyResolver(environment).getSubProperties("spring.jpa.");
|
|
|
protected void finalize() throws Throwable {
|
|
|
if (zbusBroker != null) {
|
|
|
zbusBroker.close();
|
|
|
}
|
|
|
|
|
|
super.finalize();
|
|
|
}
|
|
|
|
|
|
//sessionFactory
|
|
|
// @Bean
|
|
|
// public LocalSessionFactoryBean sessionFactory() throws SQLException {
|
|
|
// LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
|
|
|
// localSessionFactoryBean.setDataSource(this.dataSource);
|
|
|
// Properties properties1 = new Properties();
|
|
|
// 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.*.model");
|
|
|
// ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
|
// Resource resource = resourceLoader.getResource("classpath:resource/");
|
|
|
// localSessionFactoryBean.setMappingDirectoryLocations(resource);
|
|
|
//// localSessionFactoryBean.setPackagesToScan("*");
|
|
|
// return localSessionFactoryBean;
|
|
|
// }
|
|
|
|
|
|
@Bean
|
|
|
public LocalSessionFactoryBean sessionFactory() {
|
|
|
public LocalSessionFactoryBean sessionFactory() {
|
|
|
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
|
|
|
localSessionFactoryBean.setDataSource(this.dataSource);
|
|
|
Properties properties1 = new Properties();
|
|
|
properties1.setProperty("hibernate.dialect","org.hibernate.dialect.MySQL5Dialect");
|
|
|
properties1.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
|
|
|
properties1.setProperty("hibernate.show_sql", "false");
|
|
|
properties1.setProperty("hibernate.format_sql","true");
|
|
|
properties1.setProperty("hibernate.format_sql", "true");
|
|
|
localSessionFactoryBean.setHibernateProperties(properties1);
|
|
|
localSessionFactoryBean.setPackagesToScan("com.yihu.hos.*.model");
|
|
|
ResourceLoader resourceLoader = new DefaultResourceLoader();
|
|
@ -131,23 +118,22 @@ public class BeanConfig implements EnvironmentAware {
|
|
|
|
|
|
//文经上传
|
|
|
@Bean
|
|
|
public CommonsMultipartResolver multipartResolver(){
|
|
|
public CommonsMultipartResolver multipartResolver() {
|
|
|
return new CommonsMultipartResolver();
|
|
|
}
|
|
|
|
|
|
//国际化配置
|
|
|
@Bean
|
|
|
public ResourceBundleMessageSource messageSource(){
|
|
|
ResourceBundleMessageSource messageSource =new ResourceBundleMessageSource();
|
|
|
public ResourceBundleMessageSource messageSource() {
|
|
|
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
|
|
|
messageSource.setBasenames("text/message");
|
|
|
messageSource.setDefaultEncoding("UTF-8");
|
|
|
return messageSource;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Bean
|
|
|
public CookieLocaleResolver localeResolver(){
|
|
|
public CookieLocaleResolver localeResolver() {
|
|
|
CookieLocaleResolver localeResolver = new CookieLocaleResolver();
|
|
|
localeResolver.setCookieName("Language");
|
|
|
localeResolver.setCookieMaxAge(604800);
|
|
@ -156,12 +142,12 @@ public class BeanConfig implements EnvironmentAware {
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public LocaleChangeInterceptor localeChangeInterceptor(){
|
|
|
public LocaleChangeInterceptor localeChangeInterceptor() {
|
|
|
return new LocaleChangeInterceptor();
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public JdbcTemplate jdbcTemplate(){
|
|
|
public JdbcTemplate jdbcTemplate() {
|
|
|
JdbcTemplate jdbcTemplate = new JdbcTemplate();
|
|
|
try {
|
|
|
jdbcTemplate.setDataSource(this.dataSource);
|
|
@ -182,7 +168,7 @@ public class BeanConfig implements EnvironmentAware {
|
|
|
}
|
|
|
|
|
|
@Bean
|
|
|
public AuditInterceptor auditInterceptor(){
|
|
|
public AuditInterceptor auditInterceptor() {
|
|
|
return new AuditInterceptor();
|
|
|
}
|
|
|
|