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 528aafb..501d121 100644 --- a/core/src/main/java/org/pqh/core/config/HibernateConfig.java +++ b/core/src/main/java/org/pqh/core/config/HibernateConfig.java @@ -3,67 +3,67 @@ package org.pqh.core.config; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.pqh.core.util.SpringUtil; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.DependsOn; import org.springframework.context.annotation.PropertySource; import org.springframework.orm.hibernate5.HibernateTemplate; import org.springframework.orm.hibernate5.HibernateTransactionManager; import org.springframework.orm.hibernate5.LocalSessionFactoryBean; import org.springframework.transaction.annotation.EnableTransactionManagement; -import javax.annotation.PostConstruct; -import java.util.Properties; - /** * Created by reborn on 2017/7/28. */ @Configuration -@PropertySource("classpath:config.properties") +@PropertySource("classpath:hibernate.properties") @EnableTransactionManagement -public class HibernateConfig extends LocalSessionFactoryBean{ - +public class HibernateConfig{ - private BasicDataSource dataSource; private Logger log= LogManager.getLogger(); - @Value("${dbcp2.url}") + @Value("${hibernate.connection.url}") private String url; - @Value("${dbcp2.username}") + @Value("${hibernate.connection.username}") private String username; - @Value("${dbcp2.password}") + @Value("${hibernate.connection.password}") private String password; - @PostConstruct - private void init(){ - log.info("数据库配置初始化\ndburl="+url+",dbcp2.username="+username+",dbcp2.password="+password); - dataSource = new BasicDataSource(); + + public BasicDataSource basicDataSource(){ + BasicDataSource dataSource = new BasicDataSource(); dataSource.setUsername(username); dataSource.setPassword(password); dataSource.setUrl(url); dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); - 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"); - this.setHibernateProperties(properties); - this.setPackagesToScan("org.pqh.model"); + return dataSource; + } + + @Bean("sessionFactoryBean") + public LocalSessionFactoryBean sessionFactoryBean(){ + LocalSessionFactoryBean sessionFactoryBean=new LocalSessionFactoryBean(); + log.info("数据库配置初始化\nurl="+sessionFactoryBean.getHibernateProperties()+"\nusername="+username+"\npassword="+password); + sessionFactoryBean.setDataSource(basicDataSource()); + sessionFactoryBean.setPackagesToScan("org.pqh.core.model"); + return sessionFactoryBean; } @Bean("transactionManager") + @DependsOn("sessionFactoryBean") public HibernateTransactionManager hibernateTransactionManager() { - HibernateTransactionManager hibernateTransactionManager = new HibernateTransactionManager(); - hibernateTransactionManager.setSessionFactory(this.getObject()); + HibernateTransactionManager hibernateTransactionManager=new HibernateTransactionManager(); + hibernateTransactionManager.setSessionFactory(SpringUtil.getBean(LocalSessionFactoryBean.class).getObject()); return hibernateTransactionManager; } - @Bean(name = "hibernateTemplate") - public HibernateTemplate hibernateTemplate() { - HibernateTemplate hibernateTemplate = new HibernateTemplate(); - hibernateTemplate.setSessionFactory(this.getObject()); + @Bean("hibernateTemplate") + @DependsOn("sessionFactoryBean") + public HibernateTemplate hibernateTemplate(){ + HibernateTemplate hibernateTemplate=new HibernateTemplate(); + hibernateTemplate.setSessionFactory(SpringUtil.getBean(LocalSessionFactoryBean.class).getObject()); return hibernateTemplate; } - } diff --git a/core/src/main/java/org/pqh/core/util/SpringUtil.java b/core/src/main/java/org/pqh/core/util/SpringUtil.java new file mode 100644 index 0000000..44de431 --- /dev/null +++ b/core/src/main/java/org/pqh/core/util/SpringUtil.java @@ -0,0 +1,55 @@ +package org.pqh.core.util; + +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; + +import java.util.Map; + +/** + * Created by reborn on 2017/9/19. + */ +public class SpringUtil extends WebMvcConfigurationSupport { + private static ApplicationContext applicationContext; + + public static T getBean(Class c){ + return applicationContext.getBean(c); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) { + super.setApplicationContext(applicationContext); + SpringUtil.applicationContext=applicationContext; + } + + /** + * + * @param clazz 注册class + * @param serviceName 注册别名 + * @param propertyMap 注入属性 + */ + private static void addBean(Class clazz,String serviceName,Map propertyMap){ + BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz); + if(propertyMap!=null){ + for(String key:propertyMap.keySet()){ + beanDefinitionBuilder.addPropertyValue(key, propertyMap.get(key)); + } + } + registerBean(serviceName, beanDefinitionBuilder.getRawBeanDefinition()); + } + + /** + * @desc 向spring容器注册bean + * @param beanName + * @param beanDefinition + */ + private static void registerBean(String beanName, BeanDefinition beanDefinition) { + ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext; + BeanDefinitionRegistry beanDefinitonRegistry = (BeanDefinitionRegistry) configurableApplicationContext + .getBeanFactory(); + beanDefinitonRegistry.registerBeanDefinition(beanName, beanDefinition); + } +} 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 691b867..4f50db6 100644 --- a/web/src/main/java/org/pqh/achieve/config/SpringConfig.java +++ b/web/src/main/java/org/pqh/achieve/config/SpringConfig.java @@ -1,10 +1,8 @@ package org.pqh.achieve.config; import org.pqh.core.config.HibernateConfig; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.*; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import org.springframework.web.servlet.view.InternalResourceViewResolver; /** @@ -15,19 +13,7 @@ import org.springframework.web.servlet.view.InternalResourceViewResolver; @EnableAspectJAutoProxy @ComponentScan("org.pqh.achieve") @Import({HibernateConfig.class}) -public class SpringConfig extends WebMvcConfigurationSupport{ - - private static ApplicationContext applicationContext; - - public static T getBean(Class c){ - return applicationContext.getBean(c); - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) { - super.setApplicationContext(applicationContext); - SpringConfig.applicationContext=applicationContext; - } +public class SpringConfig{ @Bean("viewReslover") public InternalResourceViewResolver internalResourceViewResolver(){ diff --git a/web/src/main/resources/config.properties b/web/src/main/resources/config.properties deleted file mode 100644 index 1f3aea4..0000000 --- a/web/src/main/resources/config.properties +++ /dev/null @@ -1,6 +0,0 @@ -#dbcp2.url=jdbc:mysql://localhost:3306/bilibili?serverTimezone=UTC&&useSSL=true -#dbcp2.username=root -#dbcp2.password=123456 -dbcp2.url=jdbc:mysql://localhost:3306/bilibili?serverTimezone=UTC&&useSSL=true -dbcp2.username=root -dbcp2.password=123456 \ No newline at end of file diff --git a/web/src/main/resources/hibernate.properties b/web/src/main/resources/hibernate.properties new file mode 100644 index 0000000..87b1254 --- /dev/null +++ b/web/src/main/resources/hibernate.properties @@ -0,0 +1,6 @@ +hibernate.connection.pool_size=5 +hibernate.jdbc.fetch_size=50 +hibernate.dialect=org.hibernate.dialect.MySQL57Dialect +hibernate.connection.username=root +hibernate.connection.password=123456 +hibernate.connection.url=jdbc:mysql://localhost:3306/bilibili?serverTimezone=UTC&&useSSL=true \ No newline at end of file