views:

62

answers:

3

I have a bean definition in Spring and it's proxy counterpart which is meant to be used everywhere:

<bean name="my.Bean" class="org.springframework.aop.framework.ProxyFactoryBean" scope="prototype">
  <property name="proxyInterfaces" value="my.Interface"/>
  <property name="target" ref="my.BeanTarget"/>
  <property name="interceptorNames">
    <list>
      <value>someInterceptor</value>
    </list>
  </property>
</bean>

<bean name="my.BeanTarget" class="my.InterfaceImpl" scope="prototype">
  <property name="foo" ref="bar"/>
</bean>

This all works well; and in pre-Spring v3 world I was using it like

ApplicationContext ctx = ...;
my.Interface foo = (my.Interface) ctx.getBean("my.Bean"); // cast is necessary

In Spring 3 it became possible to do type safe lookups, e.g.:

my.Interface foo = ctx.getBean(my.Interface.class);

Again, this works well for ordinary beans whereas for proxied beans I am getting my.BeanTarget instead of my.Bean. I have tried to inline my.BeanTarget (as shown in Spring documentation) to make it hidden, but all I got was

org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [my.Interface] is defined: expected single bean but found 0: 

So is it possible to use type safe bean lookups with proxied beans and if yes - how?

A: 

Can't you make my.Interface foo = ctx.getBean(my.Bean.class); ?

Bozho
+2  A: 

It looks like the scope of proxies created by ProxyFactoryBean should be specified using singleton property instead of scope attribute:

<bean name="my.Bean" class="org.springframework.aop.framework.ProxyFactoryBean">  
    <property name="singleton" value="false"/>  
    ...
</bean>

This solved the problem when target bean is inner.

When you have several top-level beans of the same class, you can use a type-safe lookup by id:

my.Interface foo = ctx.getBean("my.Bean", my.Interface.class); 
axtavt
I accepted this answer too quickly. It turns out that singleton=false property is not the same thing as scope=prototype. I have managed to get singleton beans that are wrapped in target proxies even though singleton=false was specified. skaffman's answer is actually more closer to truth.
mindas
+2  A: 

The problem here is the scope="prototype" on your ProxyFactoryBean.

The context will only eagerly-initialize singleton bean definitions. Beans of non-singleton scope are only initialized when asked for. This means that when you ask the context for beans of a given type, the context cannot initialize those non-singleton beans in order to ask them for their type, it has to go purely on the information in the bean definition.

In the case of ProxyFactoryBean, the type of the generated proxy is determined by complex logic that requires the bean to be fully initialized. Without that initialization, ProxyFactoryBean can only report the target type as null.

I can't say a way around this, other than using a singleton bean definition, or explicitly asking for the bean by name, e.g.

<bean id="my.Interface"> class="ProxyFactoryBean"... >

and then:

ctx.getBean(MyInterface.class.getName());

Here, we use the convention of bean names being the interface they implement.

skaffman