我如何将 applicationContext.xml 中的对象转换为 java 注释
How I can convert my objects in applicationContext.xml to java annotations
我正在开发一个 spring mvc 项目,我想转换我在开发 spring mvc 时编写的 applicationContext.xml 中的 jpa 配置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>
谢谢你
XML 配置中的 class 属性必须作为具体 bean 在应用程序上下文中。 Java 配置语法如下(在你的 @Configuration
class 中):
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory());
return transactionManager;
}
@Bean LocalContainerEntityManagerFactoryBean entityManagerFactory() {
....
}
说明:您在 XML 配置中创建的 beans 配置有 setter-注入(<property>
-元素用于 setter 注入。因此您必须在 Java 配置中创建一个 bean,然后使用等效的 setter 方法设置其他 bean,然后 return 它。Spring 扫描您的 @Configuration
class,看到你的上下文中应该有 beans,创建它们并将它们放在你的应用程序上下文中。
您必须按如下方式创建 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
}
我正在开发一个 spring mvc 项目,我想转换我在开发 spring mvc 时编写的 applicationContext.xml 中的 jpa 配置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>
谢谢你
XML 配置中的 class 属性必须作为具体 bean 在应用程序上下文中。 Java 配置语法如下(在你的 @Configuration
class 中):
@Bean
public JpaTransactionManager transactionManager() {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory());
return transactionManager;
}
@Bean LocalContainerEntityManagerFactoryBean entityManagerFactory() {
....
}
说明:您在 XML 配置中创建的 beans 配置有 setter-注入(<property>
-元素用于 setter 注入。因此您必须在 Java 配置中创建一个 bean,然后使用等效的 setter 方法设置其他 bean,然后 return 它。Spring 扫描您的 @Configuration
class,看到你的上下文中应该有 beans,创建它们并将它们放在你的应用程序上下文中。
您必须按如下方式创建 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
}