BeanConfiguration.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package com.yihu.hos.rest.common.configuration;
  2. import com.yihu.hos.rest.common.filter.HibernateFilter;
  3. import org.apache.tomcat.util.threads.ThreadPoolExecutor;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.beans.factory.annotation.Value;
  6. import org.springframework.context.annotation.Bean;
  7. import org.springframework.context.annotation.ComponentScan;
  8. import org.springframework.context.annotation.Configuration;
  9. import org.springframework.context.support.ResourceBundleMessageSource;
  10. import org.springframework.core.io.DefaultResourceLoader;
  11. import org.springframework.core.io.Resource;
  12. import org.springframework.core.io.ResourceLoader;
  13. import org.springframework.jdbc.core.JdbcTemplate;
  14. import org.springframework.orm.hibernate5.HibernateTemplate;
  15. import org.springframework.orm.hibernate5.HibernateTransactionManager;
  16. import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  17. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  18. import org.springframework.transaction.annotation.EnableTransactionManagement;
  19. import org.springframework.web.multipart.commons.CommonsMultipartResolver;
  20. import org.springframework.web.servlet.i18n.CookieLocaleResolver;
  21. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  22. import javax.sql.DataSource;
  23. import java.sql.SQLException;
  24. import java.util.Locale;
  25. import java.util.Properties;
  26. /**
  27. * 数据源自定义配置
  28. * @author HZY
  29. * @vsrsion 1.0
  30. * Created at 2016/8/5.
  31. */
  32. @Configuration
  33. @EnableTransactionManagement(proxyTargetClass = true)
  34. @ComponentScan("com.yihu.hos.rest")
  35. public class BeanConfiguration {
  36. @Autowired
  37. private DataSource dataSource;
  38. @Bean
  39. public HibernateFilter filterConfig() {
  40. return new HibernateFilter();
  41. }
  42. @Bean
  43. public LocalSessionFactoryBean sessionFactory() {
  44. LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
  45. localSessionFactoryBean.setDataSource(this.dataSource);
  46. Properties properties1 = new Properties();
  47. properties1.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  48. properties1.setProperty("hibernate.show_sql", "false");
  49. properties1.setProperty("hibernate.format_sql", "true");
  50. localSessionFactoryBean.setHibernateProperties(properties1);
  51. localSessionFactoryBean.setPackagesToScan("com.yihu.hos.rest.models");
  52. return localSessionFactoryBean;
  53. }
  54. //txManager事务开启
  55. @Bean
  56. public HibernateTransactionManager txManager() throws SQLException {
  57. HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
  58. LocalSessionFactoryBean sessionFactoryBean = this.sessionFactory();
  59. hibernateTransactionManager.setSessionFactory(sessionFactoryBean.getObject());
  60. return hibernateTransactionManager;
  61. }
  62. //文经上传
  63. @Bean
  64. public CommonsMultipartResolver multipartResolver() {
  65. return new CommonsMultipartResolver();
  66. }
  67. //国际化配置
  68. @Bean
  69. public ResourceBundleMessageSource messageSource() {
  70. ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
  71. messageSource.setBasenames("text/message");
  72. messageSource.setDefaultEncoding("UTF-8");
  73. return messageSource;
  74. }
  75. @Bean
  76. public CookieLocaleResolver localeResolver() {
  77. CookieLocaleResolver localeResolver = new CookieLocaleResolver();
  78. localeResolver.setCookieName("Language");
  79. localeResolver.setCookieMaxAge(604800);
  80. localeResolver.setDefaultLocale(new Locale("zh_cn"));
  81. return localeResolver;
  82. }
  83. @Bean
  84. public LocaleChangeInterceptor localeChangeInterceptor() {
  85. return new LocaleChangeInterceptor();
  86. }
  87. @Bean
  88. public JdbcTemplate jdbcTemplate() {
  89. JdbcTemplate jdbcTemplate = new JdbcTemplate();
  90. try {
  91. jdbcTemplate.setDataSource(this.dataSource);
  92. jdbcTemplate.setLazyInit(false);
  93. } catch (Exception e) {
  94. e.printStackTrace();
  95. }
  96. return jdbcTemplate;
  97. }
  98. //Hibernate模版配置
  99. @Bean
  100. public HibernateTemplate hibernateTemplate() throws SQLException {
  101. HibernateTemplate hibernateTemplate = new HibernateTemplate();
  102. LocalSessionFactoryBean sessionFactory = this.sessionFactory();
  103. hibernateTemplate.setSessionFactory(sessionFactory.getObject());
  104. return hibernateTemplate;
  105. }
  106. @Override
  107. protected void finalize() throws Throwable {
  108. super.finalize();
  109. }
  110. }