From d205830b183b325ff1a37d6e63b0587a3e6b503c Mon Sep 17 00:00:00 2001 From: luffy9412 <1029559041@qq.com> Date: Tue, 19 Sep 2017 16:55:38 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9E=8E=E5=87=A0=E6=8A=8A=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EnableWebCrawlerConfiguration.java | 2 +- .../org/pqh/core/config/HibernateConfig.java | 28 +++++++++---------- .../org/pqh/achieve/config/SpringConfig.java | 4 +-- 3 files changed, 16 insertions(+), 18 deletions(-) diff --git a/core/src/main/java/org/pqh/core/annotation/EnableWebCrawlerConfiguration.java b/core/src/main/java/org/pqh/core/annotation/EnableWebCrawlerConfiguration.java index d4297a0..aa67e14 100644 --- a/core/src/main/java/org/pqh/core/annotation/EnableWebCrawlerConfiguration.java +++ b/core/src/main/java/org/pqh/core/annotation/EnableWebCrawlerConfiguration.java @@ -12,6 +12,6 @@ import java.lang.annotation.*; @Target({ElementType.TYPE}) @Documented @Configuration -@ComponentScan("org.pqh.core.*") +@ComponentScan("org.pqh.core") public @interface EnableWebCrawlerConfiguration { } diff --git a/core/src/main/java/org/pqh/core/config/HibernateConfig.java b/core/src/main/java/org/pqh/core/config/HibernateConfig.java index 690c554..528aafb 100644 --- a/core/src/main/java/org/pqh/core/config/HibernateConfig.java +++ b/core/src/main/java/org/pqh/core/config/HibernateConfig.java @@ -12,7 +12,7 @@ import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; -import java.io.IOException; +import javax.annotation.PostConstruct; import java.util.Properties; /** @@ -21,9 +21,9 @@ import java.util.Properties; @Configuration @PropertySource("classpath:config.properties") @EnableTransactionManagement -public class HibernateConfig { +public class HibernateConfig extends LocalSessionFactoryBean{ + - private LocalSessionFactoryBean sessionFactoryBean; private BasicDataSource dataSource; private Logger log= LogManager.getLogger(); @@ -34,36 +34,34 @@ public class HibernateConfig { @Value("${dbcp2.password}") private String password; - public HibernateConfig() { + @PostConstruct + private void init(){ log.info("数据库配置初始化\ndburl="+url+",dbcp2.username="+username+",dbcp2.password="+password); dataSource = new BasicDataSource(); + dataSource.setUsername(username); + dataSource.setPassword(password); + dataSource.setUrl(url); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); - sessionFactoryBean = new LocalSessionFactoryBean(); - sessionFactoryBean.setDataSource(dataSource); + this.setDataSource(dataSource); Properties properties = new Properties(); properties.setProperty("hibernate.connection.pool_size", "5"); properties.setProperty("hibernate.jdbc.fetch_size", "50"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect"); - sessionFactoryBean.setHibernateProperties(properties); - sessionFactoryBean.setPackagesToScan("org.pqh.model"); - try { - sessionFactoryBean.afterPropertiesSet(); - } catch (IOException e) { - e.printStackTrace(); - } + this.setHibernateProperties(properties); + this.setPackagesToScan("org.pqh.model"); } @Bean("transactionManager") public HibernateTransactionManager hibernateTransactionManager() { HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); - hibernateTransactionManager.setSessionFactory(this.sessionFactoryBean.getObject()); + hibernateTransactionManager.setSessionFactory(this.getObject()); return hibernateTransactionManager; } @Bean(name = "hibernateTemplate") public HibernateTemplate hibernateTemplate() { HibernateTemplate hibernateTemplate = new HibernateTemplate(); - hibernateTemplate.setSessionFactory(sessionFactoryBean.getObject()); + hibernateTemplate.setSessionFactory(this.getObject()); return hibernateTemplate; } diff --git a/web/src/main/java/org/pqh/achieve/config/SpringConfig.java b/web/src/main/java/org/pqh/achieve/config/SpringConfig.java index cce12fb..691b867 100644 --- a/web/src/main/java/org/pqh/achieve/config/SpringConfig.java +++ b/web/src/main/java/org/pqh/achieve/config/SpringConfig.java @@ -1,6 +1,5 @@ package org.pqh.achieve.config; -import org.pqh.core.annotation.EnableWebCrawlerConfiguration; import org.pqh.core.config.HibernateConfig; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.*; @@ -14,7 +13,8 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; @Configuration @EnableWebMvc @EnableAspectJAutoProxy -@Import({HibernateConfig.class,WebCrawlerConfig.class}) +@ComponentScan("org.pqh.achieve") +@Import({HibernateConfig.class}) public class SpringConfig extends WebMvcConfigurationSupport{ private static ApplicationContext applicationContext;