123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- package camel.config;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import org.apache.tomcat.util.threads.ThreadPoolExecutor;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- 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.LocalSessionFactoryBean;
- import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
- import org.springframework.web.multipart.MultipartResolver;
- 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.HashMap;
- import java.util.Locale;
- import java.util.Map;
- import java.util.Properties;
- /**
- * @author HZY
- * @vsrsion 1.0
- * Created at 2016/8/5.
- */
- @Configuration
- @EnableTransactionManagement
- @ComponentScan("camel")
- //@ImportResource({"classpath:spring/applicationContext.xml"}) //applicationContext相关bean创建
- public class BeanConfig {
- @Autowired
- private DataSource dataSource;
- @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("camel.*");
- ResourceLoader resourceLoader = new DefaultResourceLoader();
- //设置拦截器
- return localSessionFactoryBean;
- }
- @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 Session getSession() throws HibernateException {
- LocalSessionFactoryBean sessionFactory = this.sessionFactory();
- return sessionFactory.getObject().openSession();
- }
- @Bean
- public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
- ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
- threadPoolTaskExecutor.setCorePoolSize(5);
- threadPoolTaskExecutor.setMaxPoolSize(10);
- threadPoolTaskExecutor.setQueueCapacity(25);
- threadPoolTaskExecutor.setKeepAliveSeconds(300);
- threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
- return threadPoolTaskExecutor;
- }
- @Bean
- public ObjectMapper objectMapper() {
- return new ObjectMapper();
- }
- }
|