我得到一个NoSuchBeanDefinitionException - “我的配置没有定义名为'transactionManager'的bean”。 我的配置在正常的webapp启动时运行。
JUnit Class
@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() {
.
.
.
应用context.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" />
THX适合您的时间。
答案 0 :(得分:0)
在测试类中添加以下注释:
@TransactionConfiguration(transactionManager="txManager")
其中txManager
是Spring配置文件中定义的TranscationManager名称。