BeanConfig.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package camel.config;
  2. import com.fasterxml.jackson.databind.ObjectMapper;
  3. import org.apache.tomcat.util.threads.ThreadPoolExecutor;
  4. import org.hibernate.HibernateException;
  5. import org.hibernate.Session;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.beans.factory.annotation.Value;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.ComponentScan;
  10. import org.springframework.context.annotation.Configuration;
  11. import org.springframework.context.support.ResourceBundleMessageSource;
  12. import org.springframework.core.io.DefaultResourceLoader;
  13. import org.springframework.core.io.Resource;
  14. import org.springframework.core.io.ResourceLoader;
  15. import org.springframework.jdbc.core.JdbcTemplate;
  16. import org.springframework.orm.hibernate5.HibernateTemplate;
  17. import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  18. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  19. import org.springframework.transaction.annotation.EnableTransactionManagement;
  20. import org.springframework.web.multipart.MultipartResolver;
  21. import org.springframework.web.multipart.commons.CommonsMultipartResolver;
  22. import org.springframework.web.servlet.i18n.CookieLocaleResolver;
  23. import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
  24. import javax.sql.DataSource;
  25. import java.sql.SQLException;
  26. import java.util.HashMap;
  27. import java.util.Locale;
  28. import java.util.Map;
  29. import java.util.Properties;
  30. /**
  31. * @author HZY
  32. * @vsrsion 1.0
  33. * Created at 2016/8/5.
  34. */
  35. @Configuration
  36. @EnableTransactionManagement
  37. @ComponentScan("camel")
  38. //@ImportResource({"classpath:spring/applicationContext.xml"}) //applicationContext相关bean创建
  39. public class BeanConfig {
  40. @Autowired
  41. private DataSource dataSource;
  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("camel.*");
  52. ResourceLoader resourceLoader = new DefaultResourceLoader();
  53. //设置拦截器
  54. return localSessionFactoryBean;
  55. }
  56. @Bean
  57. public JdbcTemplate jdbcTemplate() {
  58. JdbcTemplate jdbcTemplate = new JdbcTemplate();
  59. try {
  60. jdbcTemplate.setDataSource(this.dataSource);
  61. jdbcTemplate.setLazyInit(false);
  62. } catch (Exception e) {
  63. e.printStackTrace();
  64. }
  65. return jdbcTemplate;
  66. }
  67. //Hibernate模版配置
  68. @Bean
  69. public HibernateTemplate hibernateTemplate() throws SQLException {
  70. HibernateTemplate hibernateTemplate = new HibernateTemplate();
  71. LocalSessionFactoryBean sessionFactory = this.sessionFactory();
  72. hibernateTemplate.setSessionFactory(sessionFactory.getObject());
  73. return hibernateTemplate;
  74. }
  75. @Bean
  76. public Session getSession() throws HibernateException {
  77. LocalSessionFactoryBean sessionFactory = this.sessionFactory();
  78. return sessionFactory.getObject().openSession();
  79. }
  80. @Bean
  81. public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
  82. ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
  83. threadPoolTaskExecutor.setCorePoolSize(5);
  84. threadPoolTaskExecutor.setMaxPoolSize(10);
  85. threadPoolTaskExecutor.setQueueCapacity(25);
  86. threadPoolTaskExecutor.setKeepAliveSeconds(300);
  87. threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  88. return threadPoolTaskExecutor;
  89. }
  90. @Bean
  91. public ObjectMapper objectMapper() {
  92. return new ObjectMapper();
  93. }
  94. }