我如何将applicationContext.xml中的对象转换为java注释

时间:2016-11-14 10:58:13

标签: java spring spring-mvc spring-data-jpa entitymanager

我正在开发一个spring mvc项目,我想转换我在我的applicationContext.xml中的jpa配置,这是我在spring mvc 3上工作时写的,现在我想转移到spring Mvc 4并写我使用Java注释的所有Jpa配置都可以帮助我

这是我的applicationContext文件:

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

2 个答案:

答案 0 :(得分:4)

XML配置中的类属性必须作为具体bean在您的应用程序上下文中。 Java config synax如下(在@Configuration类中):

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

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

说明:您在XML配置中创建的bean使用setter-injection进行配置(<property> - 元素用于setter注入。因此,您必须在Java配置中创建一个bean并设置其他bean使用equivalant setter-methods并在之后返回它.Spring扫描你的@Configuration类,看到你的上下文中应该有bean,创建它们并将它们放在你的应用程序上下文中。

答案 1 :(得分:2)

您必须按如下方式创建spring bean组件(或bean):

@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

}