123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- package com.yihu.hos.rest.common.configuration;
- import com.yihu.hos.rest.common.filter.HibernateFilter;
- import org.apache.tomcat.util.threads.ThreadPoolExecutor;
- import org.springframework.beans.factory.annotation.Autowired;
- 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.io.DefaultResourceLoader;
- import org.springframework.core.io.Resource;
- import org.springframework.core.io.ResourceLoader;
- import org.springframework.jdbc.core.JdbcTemplate;
- import org.springframework.orm.hibernate5.HibernateTemplate;
- import org.springframework.orm.hibernate5.HibernateTransactionManager;
- import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- 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 javax.sql.DataSource;
- import java.sql.SQLException;
- import java.util.Locale;
- import java.util.Properties;
- /**
- * 数据源自定义配置
- * @author HZY
- * @vsrsion 1.0
- * Created at 2016/8/5.
- */
- @Configuration
- @EnableTransactionManagement(proxyTargetClass = true)
- @ComponentScan("com.yihu.hos.rest")
- public class BeanConfiguration {
- @Autowired
- private DataSource dataSource;
- @Bean
- public HibernateFilter filterConfig() {
- return new HibernateFilter();
- }
- @Bean
- 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.show_sql", "false");
- properties1.setProperty("hibernate.format_sql", "true");
- localSessionFactoryBean.setHibernateProperties(properties1);
- localSessionFactoryBean.setPackagesToScan("com.yihu.hos.rest.models");
- return localSessionFactoryBean;
- }
- //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;
- }
- @Override
- protected void finalize() throws Throwable {
- super.finalize();
- }
- }
|