瞎几把改

master
luffy9412 7 years ago
parent d205830b18
commit 2213ddb298
  1. 56
      core/src/main/java/org/pqh/core/config/HibernateConfig.java
  2. 55
      core/src/main/java/org/pqh/core/util/SpringUtil.java
  3. 16
      web/src/main/java/org/pqh/achieve/config/SpringConfig.java
  4. 6
      web/src/main/resources/config.properties
  5. 6
      web/src/main/resources/hibernate.properties

@ -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;
}
}

@ -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>T getBean(Class<T> 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<String,Object> 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);
}
}

@ -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>T getBean(Class<T> 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(){

@ -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

@ -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
Loading…
Cancel
Save