瞎几把改

master
luffy9412 7 years ago
parent 6ee0176bd7
commit d205830b18
  1. 2
      core/src/main/java/org/pqh/core/annotation/EnableWebCrawlerConfiguration.java
  2. 28
      core/src/main/java/org/pqh/core/config/HibernateConfig.java
  3. 4
      web/src/main/java/org/pqh/achieve/config/SpringConfig.java

@ -12,6 +12,6 @@ import java.lang.annotation.*;
@Target({ElementType.TYPE}) @Target({ElementType.TYPE})
@Documented @Documented
@Configuration @Configuration
@ComponentScan("org.pqh.core.*") @ComponentScan("org.pqh.core")
public @interface EnableWebCrawlerConfiguration { public @interface EnableWebCrawlerConfiguration {
} }

@ -12,7 +12,7 @@ import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
import java.io.IOException; import javax.annotation.PostConstruct;
import java.util.Properties; import java.util.Properties;
/** /**
@ -21,9 +21,9 @@ import java.util.Properties;
@Configuration @Configuration
@PropertySource("classpath:config.properties") @PropertySource("classpath:config.properties")
@EnableTransactionManagement @EnableTransactionManagement
public class HibernateConfig { public class HibernateConfig extends LocalSessionFactoryBean{
private LocalSessionFactoryBean sessionFactoryBean;
private BasicDataSource dataSource; private BasicDataSource dataSource;
private Logger log= LogManager.getLogger(); private Logger log= LogManager.getLogger();
@ -34,36 +34,34 @@ public class HibernateConfig {
@Value("${dbcp2.password}") @Value("${dbcp2.password}")
private String password; private String password;
public HibernateConfig() { @PostConstruct
private void init(){
log.info("数据库配置初始化\ndburl="+url+",dbcp2.username="+username+",dbcp2.password="+password); log.info("数据库配置初始化\ndburl="+url+",dbcp2.username="+username+",dbcp2.password="+password);
dataSource = new BasicDataSource(); dataSource = new BasicDataSource();
dataSource.setUsername(username);
dataSource.setPassword(password);
dataSource.setUrl(url);
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
sessionFactoryBean = new LocalSessionFactoryBean(); this.setDataSource(dataSource);
sessionFactoryBean.setDataSource(dataSource);
Properties properties = new Properties(); Properties properties = new Properties();
properties.setProperty("hibernate.connection.pool_size", "5"); properties.setProperty("hibernate.connection.pool_size", "5");
properties.setProperty("hibernate.jdbc.fetch_size", "50"); properties.setProperty("hibernate.jdbc.fetch_size", "50");
properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect"); properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL57Dialect");
sessionFactoryBean.setHibernateProperties(properties); this.setHibernateProperties(properties);
sessionFactoryBean.setPackagesToScan("org.pqh.model"); this.setPackagesToScan("org.pqh.model");
try {
sessionFactoryBean.afterPropertiesSet();
} catch (IOException e) {
e.printStackTrace();
}
} }
@Bean("transactionManager") @Bean("transactionManager")
public HibernateTransactionManager hibernateTransactionManager() { public HibernateTransactionManager hibernateTransactionManager() {
HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager();
hibernateTransactionManager.setSessionFactory(this.sessionFactoryBean.getObject()); hibernateTransactionManager.setSessionFactory(this.getObject());
return hibernateTransactionManager; return hibernateTransactionManager;
} }
@Bean(name = "hibernateTemplate") @Bean(name = "hibernateTemplate")
public HibernateTemplate hibernateTemplate() { public HibernateTemplate hibernateTemplate() {
HibernateTemplate hibernateTemplate = new HibernateTemplate(); HibernateTemplate hibernateTemplate = new HibernateTemplate();
hibernateTemplate.setSessionFactory(sessionFactoryBean.getObject()); hibernateTemplate.setSessionFactory(this.getObject());
return hibernateTemplate; return hibernateTemplate;
} }

@ -1,6 +1,5 @@
package org.pqh.achieve.config; package org.pqh.achieve.config;
import org.pqh.core.annotation.EnableWebCrawlerConfiguration;
import org.pqh.core.config.HibernateConfig; import org.pqh.core.config.HibernateConfig;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.*; import org.springframework.context.annotation.*;
@ -14,7 +13,8 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@EnableAspectJAutoProxy @EnableAspectJAutoProxy
@Import({HibernateConfig.class,WebCrawlerConfig.class}) @ComponentScan("org.pqh.achieve")
@Import({HibernateConfig.class})
public class SpringConfig extends WebMvcConfigurationSupport{ public class SpringConfig extends WebMvcConfigurationSupport{
private static ApplicationContext applicationContext; private static ApplicationContext applicationContext;

Loading…
Cancel
Save