Damn “smart” quotes in Word Jeremy Boynes and Dain Sundstrom on Apache Geronimo on Video
Jul 27

Spring: Encapsulate the fact that you have a proxy and a target bean via “inner beans”

Tech Add comments

I have always been a little irked when I saw two bean definitions:

  • The target: This is the real service that will do things
  • The proxy: This holds interceptors and such, and then finally delegates to the target

A usual Spring convention is to have fooTarget and foo. You know not to lookup *Target beans.

However, I want to stop people :) I don’t want it as an option.

Well, while talking about this with Rod Johnson, he told me that he had implemented this while sitting at an AOP talk at a BeJUG meeting :)

So now we can use inner bean classes:

<bean id=”person”
class=”org.springframework.aop.framework.ProxyFactoryBean”>
<property name=”proxyInterfaces”><value>com.mycompany.Person</value></property>

<!– Use inner bean, not local reference to target –>
<property name=”target”>
<bean class=”com.mycompany.PersonImpl”>
<property name=”name”><value>Tony</value></property>
<property name=”age”><value>51</value></property>
</bean>
</property>

<property name=”interceptorNames”>
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>

Compare that to the old way:

<bean id=”personTarget” class=”com.mycompany.PersonImpl”>
<property name=”name”><value>Tony</value></property>
<property name=”age”><value>51</value></property>
</bean>

<bean id=”person”
class=”org.springframework.aop.framework.ProxyFactoryBean”>
<property name=”proxyInterfaces”><value>com.mycompany.Person</value></property>

<property name=”target”><ref local=”personTarget”/></property>
<property name=”interceptorNames”>
<list>
<value>myAdvisor</value>
<value>debugInterceptor</value>
</list>
</property>
</bean>

Obviously, I omitted the interceptor declarations

Thanks Rod, Juergen, and co!

4 Responses to “Spring: Encapsulate the fact that you have a proxy and a target bean via “inner beans””

  1. Dion Says:

    Thanks Seth, I will look into the BeanNameAutoProxyCreator!

    Cheers,

    Dion

  2. Rod Johnson Says:

    This isn’t actually a new feature: it’s a combination of existing features (inner beans an ProxyFactoryBean). I just realised it was possible at the AOP seminar. So it should work in any 1.0.x release.

    One major benefit of getting rid of the two definitions is that it allows the use of autowiring.

    Rgds
    Rod

  3. nat Says:

    every man has a fool in his sleeve

  4. bab Says:

    beware of a quite dog and still water

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: What are the first four letters in the word British?