Pregunton
Pregunton

Reputation: 43

Why my service is autowired to a Proxy?

Hi I've got this class to do test:

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

    @Autowired private CommonService commonService;

while debuging, I get on CommonService an Object with an attribute h which is a SdkDynamicAopProxy.

How can I get on my attribute commonService one CommonServiceImp Object?

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 />

Upvotes: 2

Views: 2520

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280174

Your CommonServiceImp class is annotated with @Transactional and you have an application context which does transaction management with <tx:annotation-driven /> and the transaction manager bean. Spring uses proxies to achieve this behavior and intercept all your method calls and wrap them with the transactional behavior. That is why you see a SdkDynamicAopProxy and not the Type of your class.

See the official documentation.

Upvotes: 7

Related Questions