嗨,我有这个课要测试:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"/service.xml"})
public class Test {

    @Autowired private CommonService commonService;


调试时,我在CommonService上获得一个对象,该对象的属性为SdkDynamicAopProxy。

如何在我的属性commonService上获得一个CommonServiceImp对象?

commonService

public interface CommonService {...}


CommonServiceImp

@Service("commonService")
@Transactional("transactionManager")
public class CommonServiceImp implements CommonService {
    @Autowired private CommonDaoJdbcImp commonDao; ...}


service.xml

    <?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:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
    http://www.springframework.org/schema/task
    http://www.springframework.org/schema/task/spring-task-3.0.xsd">

    <import resource="/bbb-dao.xml"/>

    <context:component-scan base-package="aaa.bbb.service"/>
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- <bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager" /> -->
    <tx:annotation-driven />
<task:annotation-driven />

最佳答案

您的CommonServiceImp类用@Transactional注释,并且您有一个应用程序上下文,该上下文使用<tx:annotation-driven />和事务管理器bean进行事务管理。 Spring使用代理来实现此行为,并拦截所有方法调用,并将其包装为事务性行为。这就是为什么您看到SdkDynamicAopProxy而不是类的类型的原因。

See the official documentation.

07-24 20:17