Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How I can convert my objects in applicationContext.xml to java annotations

I'm working on a spring mvc project and I want to convert my jpa configuration that I have in my applicationContext.xml that I wrote when I was working on spring mvc 3 now I want to move to spring Mvc 4 and write all my Jpa configuration just using Java annotations can someone help me

This is my applicationContext file :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">


 <bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
  <property name="url" value="jdbc:mysql://localhost:3306/db_adpub"></property>
  <property name="username" value="root"></property>
  <property name="password" value=""></property>
  </bean>

<bean id="persistenceUnitManager" class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="persistenceXmlLocations">
 <list>
    <value>classpath*:META-INF/persistence.xml</value>
  </list>
</property>
<property name="defaultDataSource" ref="datasource"></property>
</bean>

<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
   <property name="persistenceUnitManager" ref="persistenceUnitManager"></property>
   <property name="persistenceUnitName" value="adpub"></property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory"></property>
</bean>

<tx:annotation-driven transaction-manager="transactionManager" />
<context:annotation-config></context:annotation-config>
<mvc:annotation-driven />
</beans>

Merci d'avance

like image 426
e2rabi Avatar asked Nov 14 '16 10:11

e2rabi


2 Answers

The class attributes from your XML config must be in your application context as concrete beans. The Java config synax is as follows (in your @Configuration class):

@Bean
public JpaTransactionManager transactionManager() {
    JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory());
    return transactionManager;
}

@Bean LocalContainerEntityManagerFactoryBean entityManagerFactory() {
....
}

Explanation: The beans you are creating in the XML config are configurated with setter-injection (the <property>-element is for the setter injection. Therefore you have to create a bean in your Java config and set other beans with the equivalant setter-methods and return it afterwards. Spring scans your @Configuration class, sees that there should be beans in your context, creates them and puts them in your application context.

like image 80
smsnheck Avatar answered Oct 11 '22 22:10

smsnheck


You will have to create the spring bean components (or beans) as follows:

@Compnonent
MyTransactionManager
{
@Autowired
EntityManagerFactory entityManagerFactory;

...Constructor to set properties and entityManagerFactory
...Getters and setters

}

@Component
entityManagerFactory{
persistenceUnitName

@Autowired
PersistenceUnitManager persistenceUnitManager

...Constructor to set properties and persistenceUnitManager
...Getters and setters
}

@Component
persistenceUnitManager{
Values as described in properties file, for example like

public @Value("${version}") String version;

...Constructor to set properties 
...Getters and setters

}
like image 44
KayV Avatar answered Oct 11 '22 22:10

KayV