由spring项目转为springboot项目的问题

由spring项目转为springboot项目的问题

  • 1、xml配置转为bean
  • 2、使用hibernate和durid

1、xml转bean

http://www.oschina.net/translate/consider-replacing-spring-xml-configuration-with-javaconfig?cmp

这篇文章很好的解决了

2、使用hibernate和durid

下面是我配置的 但是 由问题,需要将
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(sessionFactory);

return txManager;

}
这段删除才行

package luculent.fupin.durid;

import java.util.Properties;

import javax.sql.DataSource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.orm.hibernate4.HibernateTemplate;
import org.springframework.orm.hibernate4.HibernateTransactionManager;
import org.springframework.orm.hibernate4.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableTransactionManagement
public class DruidDataSourceConfiguration implements EnvironmentAware{

private RelaxedPropertyResolver propertyResolver;  

@Override  
public void setEnvironment(Environment env) {  
    this.propertyResolver = new RelaxedPropertyResolver(env, "spring.hibernate.");  
}  

@Bean
@ConfigurationProperties(prefix = "spring.datasource")
public DataSource druidDataSource(){
    DruidDataSource druidDataSource = new DruidDataSource ();
    return druidDataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory(){
    LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
    sessionFactory.setDataSource (this.druidDataSource ());
    sessionFactory.setPackagesToScan (new String[] {"luculent"});
    sessionFactory.setHibernateProperties(hibernateProperties()); 
    return sessionFactory;
}

@Bean
public HibernateTemplate hibernateTemplate(){
    HibernateTemplate temp = new HibernateTemplate();
    temp.setSessionFactory (this.sessionFactory ().getObject ());
    return temp;
}

@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory sessionFactory) {
    HibernateTransactionManager txManager = new HibernateTransactionManager();
    txManager.setSessionFactory(sessionFactory);

    return txManager;
 }


@Bean
public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
   return new PersistenceExceptionTranslationPostProcessor();
}

Properties hibernateProperties() {
    return new Properties() {
       /**
         * 
         */
        private static final long serialVersionUID = 1285306698726701874L;

    {   
          setProperty ("hibernate.show_sql", propertyResolver.getProperty ("show_sql")); 
       }
    };
 }

}

你可能感兴趣的:(springboot-聊天室)