0

我的配置出现 NoSuchBeanDefinitionException - “没有定义名为 'transactionManager' 的 bean”。在正常 webapp 启动的情况下,我的配置正在运行。

JUnit 类

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class })
@ContextConfiguration(locations = {"classpath:**/applicationContext.xml", "classpath:**/datasource-config.xml"}) //, 
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class TestTripService {

TripService tripService;

@Test
public void addTrip() { 
  .
  .
  .

应用程序上下文.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans .......
spring-context-3.0.xsd">
<import resource="datasource-config.xml" />
<import resource="webflow-config.xml" />
<import resource="security-config.xml" />


<context:annotation-config/>
<context:component-scan base-package="de.wiegand.mytransport" />

<!-- DAO declarations -->
<bean id="userDao" class="de.wiegand.mytransport.dao.UserJpaDao" />
<bean id="shippingAgencyDao" class="de.wiegand.mytransport.dao.ShippingAgencyJplDao" />

<!-- Services declarations -->
<bean id="userService" class="de.wiegand.mytransport.services.impl.UserServiceImpl">
    <property name="userDao" ref="userDao" />
    <property name="shippingAgencyDao" ref="shippingAgencyDao" />
</bean>

<bean id="userAuthenticationProviderService"
    class="de.wiegand.mytransport.services.impl.UserAuthenticationProviderServiceImpl">
    <property name="authenticationManager" ref="authenticationManager" />
</bean>

<bean id="tripService" class="de.wiegand.mytransport.services.impl.TripServiceImpl" />

<bean id="schedulerTask" class="org.springframework.scheduling.timer.MethodInvokingTimerTaskFactoryBean">
    <property name="targetObject" ref="postCodeManager" />
    <property name="targetMethod" value="init" />
</bean>

<bean id="postCodeManager" class="de.wiegand.mytransport.postcodeservice.PostCodeManager" />

<bean id="timerTask" class="org.springframework.scheduling.timer.ScheduledTimerTask">
    <property name="timerTask" ref="schedulerTask" />
    <property name="delay" value="1000" />
</bean>

<bean class="org.springframework.scheduling.timer.TimerFactoryBean">
    <property name="scheduledTimerTasks">
        <list>
            <ref local="timerTask" />
        </list>
    </property>
</bean>

数据源-config.xml

<?xml version="1.0" encoding="UTF-8"?>

<context:property-placeholder location="classpath:datasource.properties" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">

    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://localhost:3306/test2" />
    <property name="username" value="root" />
    <property name="password" value="" />
</bean>

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
        </bean>
    </property>

    <property name="dataSource" ref="dataSource" />
</bean>

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

<tx:annotation-driven transaction-manager="transactionManager" />

<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

谢谢你的时间。

4

1 回答 1

0

将以下注释添加到您的测试类:

@TransactionConfiguration(transactionManager="txManager")

txManagerSpring 配置文件中定义的 TranscationManager 名称在哪里。

于 2014-11-27T08:27:44.553 回答