Announcement Announcement Module
Collapse
No announcement yet.
Target bean is being autowired instead of the Proxy Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Target bean is being autowired instead of the Proxy

    Hi,

    I have a Service bean with one private field which I am trying to autowire to a Proxy . My code is as follow:

    @Service
    class MyServiceBean implements MyService {

    @Autowired
    private MyDataAccess dataAccess;
    }

    @Repository
    class MyDataAccessBean implements MyDataAccess {

    }

    Config.xml

    <context:annotation-config />

    <context:component-scan base-package="com.x">
    <context:include-filter type="regex" expression=".*\..*DataAccessBean"/>
    <context:include-filter type="regex" expression=".*\..*ServiceBean"/>
    </context:component-scan>

    <bean class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <value>*ServiceBean</value>
    </property>
    </bean>

    <bean class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <value>*DataAccessBean</value>
    </property>
    </bean>

    Spring is autowiring a real MyDataAccessBean instance instead of a Proxy. To get it to auto-wire to a Proxy, I have to use a @Qualifier with the @Autowired annotation.

    Is this an expected behavior? Do you know if I I can get it to work properly without the use of a qualifier?

  • #2
    Please use [ code][/code ] tags when posting that way it remains readable...

    For starters I strongly suggest NOT to use BeanNameAutoProxyCreator as there are better ways and it should be considered deprecated. Use the new aop namespace support for replacement. If you really must for your current scenario instead of 2 use 1.

    Also your includes in your component-scan are useless as they are already being picked up. The same goes for your context:annotation-config that is already implied by component-scan.

    Comment


    • #3
      Thank you Martin.

      The reason why I am using 2 BeanNameAutoProxyCreator is because I am attaching different interceptors for each type of classes(Did not show in my sample code).There might be better ways to do that. Do you have an example of how to use the new aop namespace support for replacement ?

      Comment

      Working...
      X