Sabbane
Sabbane

Reputation: 3336

Spring: Migrating database configuration from XML file to annotation

I'm trying to update my project to the newest version of Spring, Hibernate etc. and the issue I'm facing now is the migration of the database configuration from my old XML configuration file to the annotation (I really like the annotated based configuration). Below is my old configuration:

<!-- Configure SessionFactory -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
    destroy-method="close">
    <property name="driverClass" value="org.postgresql.Driver" />
    <property name="jdbcUrl" value="jdbc:postgresql://localhost:5432/mydatabase" />
    <property name="user" value="user" />
    <property name="password" value="password" />
</bean>

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="dataSource">
        <ref local="dataSource" />
    </property>
    <property name="packagesToScan" value="com.myproject.core.domain" />
    <property name="configLocation">
        <value>/WEB-INF/hibernate.cfg.xml</value>
    </property>
    <property name="configurationClass">
        <value>org.hibernate.cfg.AnnotationConfiguration</value>
    </property>
</bean>

<!-- enable the configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="transactionManager" />

<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>

<bean
    class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

<!-- END HIBERNATE CONFIG --> 

Did any one know how to configure the sessionfactory and the transaction manager using annotations only ?

Upvotes: 1

Views: 2956

Answers (2)

Jama A.
Jama A.

Reputation: 16099

Here is what I have with Hibernate 4 and spring 3.2: You need to use @EnableTransactionManagement for your config class.

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:application.properties" })
@ComponentScan({ "org.mysample.model" })
public class PersistenceConfig {

    @Autowired
    private Environment env;

    @Bean
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(restDataSource());
        sessionFactory.setPackagesToScan(new String[] { "org.mysample.model" });
        sessionFactory.setHibernateProperties(hibernateProperties());

          return sessionFactory;
     }

      @Bean
      public DataSource restDataSource() {
         BasicDataSource dataSource = new BasicDataSource();
          dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
          dataSource.setUrl(env.getProperty("jdbc.url"));
          dataSource.setUsername(env.getProperty("jdbc.username"));
          dataSource.setPassword(env.getProperty("jdbc.password"));
          return dataSource;
       }

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

If you need more configuration you can add them by annotating with @Bean. These are several dependencies including hibernate-core:4.3.5.Final and javassist:3.18.1-GA

UPDATED: If you define some of your configurations in application.properties file, Spring automatically constructs your Datasource and other configurations: Common application properties

Upvotes: 2

Sabbane
Sabbane

Reputation: 3336

Based on Jama's answer I've updated my configuration and tried to minimize it as possible. The result is a configuration class and a property file:

@EnableAutoConfiguration
@ComponentScan
@EnableAspectJAutoProxy
@EnableTransactionManagement
@PropertySource(value = { "classpath:/appcontext/application.properties" })
public class Application {
    @Bean
    @Autowired
    public LocalSessionFactoryBean sessionFactory(DataSource datasource) {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(datasource);
        sessionFactory.setPackagesToScan(new String[] { "org.mysample.model" });

        return sessionFactory;
    }

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

Notes: the configuration also enables the aop supports.

the property file contains:

#DataSource configuration
spring.datasource.url=jdbc:postgresql://localhost:5432/mydatabase
spring.datasource.username=dbUsername
spring.datasource.password=dbPassword
spring.datasource.driverClassName=org.postgresql.Driver

Note: the properties file is located within the folder "/src/main/resources/appcontext/".

Upvotes: 0

Related Questions