为什么我的服务自动连接到代理服务器?

时间:2013-03-25 21:11:02

标签: java spring proxy aop autowired

嗨,我有这堂课要做测试:

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

    @Autowired private CommonService commonService;

在调试时,我使用属性h为SdkDynamicAopProxy的CommonService一个对象。

如何在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 />

1 个答案:

答案 0 :(得分:5)

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

See the official documentation.

相关问题