日期:2014-05-20 浏览次数:20881 次
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- Spring集成jpa和集成tomcat数据连接池的配置文件 -->
<!-- 自动扫描带有com.itcast的类并纳入spring的管理 -->
<context:component-scan base-package="com.itcast" />
<!--找到数据库的配置文件-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 数据源配置 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxActive" value="${dataSource.maxActive}" />
<property name="maxIdle" value="${dataSource.maxIdle}" />
<property name="minIdle" value="${dataSource.minIdle}" />
<property name="maxWait" value="${dataSource.maxWait}" />
</bean>
<!--事物工厂-->
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceXmlLocation" value="/META-INF/persistence.xml"/>
<property name="loadTimeWeaver">
<bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
</property>
</bean>
<!--事物管理-->
<bean id="transactionManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--Transactional-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
@Service
public class ProductServiceBean implements ProductService {
@PersistenceContext EntityManager em;
@Transactional
public void save(ProductType product){
em.persist(product);
}
}
public interface ProductService {
public void save(ProductType type);
}
public class ProductTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void runtest() {
ApplicationContext test = new ClassPathXmlApplicationContext("applicationContext.xml");
ProductService productService = (ProductService)test.getBean("productServiceBean");
ProductType type = new ProductType();
type.setName("运动鞋");
type.setNote("品牌,没什么好说的");
type.setVisialbe(true);
productService.save(type);
}
}
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'productServiceBean': Injection of persistence fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2
at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessAfterInstantiation(PersistenceAnnotationBeanPostProcessor.java:311)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)