Announcement Announcement Module
Collapse
No announcement yet.
Annotation equivalent of <aop:scoped-proxy/> ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotation equivalent of <aop:scoped-proxy/> ?

    Hi,

    I'm kind of new to AOP, and have been trying to figure out the annotation equivalent of the xml bean option <aop:scoped-proxy />.

    I've looked around, and the closest I've been able to find is the following:
    @Scope( value= "request", proxyMode=ScopedProxyMode.INTERFACES )

    Whereas that seems very similar, I'm not suer if it is exactly the same or. In the xml option, not specifying the proxy-target-class, by default it does not proxy the target class directly. If true, then it a JDK proxy is created for interfaces and CGLIB proxy for any other class.

    According to the docs of ScopedProxyMode, INTERFACES creates a JDK proxy for all interfaces, and TARGET_CLASS forces CGLIB proxy. Does that mean that INTERFACES is the equiavlent of proxy-target-class=false, and TARGET_CLASS is the same as proxy-target-class=true?


    The other question, is in XML, if I specify <aop:scoped-proxy />, I can control the proxy'ing for all my scoped-proxy'ed beans with:
    <bean class="org.springframework.aop.framework.autoproxy .DefaultAdvisorAutoProxyCreator" scope="singleton">
    <property name="proxyTargetClass" value="false"/>
    <property name="usePrefix" value="true" />
    <property name="beanName" value="advisor" />
    </bean>

    Does the same still hold true when using annotations? If I specify TARGET_CLASS or INTERFACES, does the above Advisor override that proxy definition?

    Thanks,

    Eric

  • #2
    From the reference guide, page 113:

    Spring offers a convenient way of working with scoped dependencies through scoped proxies. The easiest
    way to create such a proxy when using the XML configuration is the <aop:scoped-proxy/>
    element. Configuring your beans in Java with a @Scope annotation offers equivalent support with the
    proxyMode attribute. The default is no proxy (ScopedProxyMode.NO), but you can specify
    ScopedProxyMode.TARGET_CLASS or ScopedProxyMode.INTERFACES.
    So, to answer your questions:

    Does that mean that INTERFACES is the equiavlent of proxy-target-class=false, and TARGET_CLASS is the same as proxy-target-class=true?
    Yes it is. For Java config and Annotation config obviously there is no "default" behavior (i.e. use interface proxies when there is an interface, use classproxies otherwise) because with autowiring it doesn't make any sense (but then nobody's gonna miss it since it was but a source of problems in most cases).

    Does the same still hold true when using annotations? If I specify TARGET_CLASS or INTERFACES, does the above Advisor override that proxy definition?
    If you go the JavaConfig way or the annotated way, you shouldn't declare and use DefaultAdvisorAutoProxyCreator because that would be a mixing of strategies = chaos. For the JavaConfig, as stated in the sentence cited from the reference guide, the default for each bean defined with @Bean is no proxy, so you have to specify individually which bean has to be proxied (there is no way to specify that you want to proxy all the beans). For annotated config, you can specify it with the scoped-proxy attribute in the context:component-scan element:

    Code:
    <context:component-scan base-package="org.example" scoped-proxy="interfaces" />

    Comment

    Working...
    X