Take the 2-minute tour ×

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

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

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?

share|improve this question
Do you really need to interact with the context directly? Most of my applications only ever need to bootstrap it and then everything else is handled with dependency injection (which works for proxied beans). I'm done some framework stuff where I did need to access the context, but, in my experience, it was rare. –  SteveD Aug 26 '10 at 11:29
Our system is quite wide and some bits and classes are not born in Spring (nor it can be), so they have to use beanFactory/appCtx to get necessary dependencies. –  mindas Aug 26 '10 at 11:42

3 Answers 3

up vote 4 down vote accepted

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:


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

share|improve this answer

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

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); 
share|improve this answer
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 Aug 27 '10 at 14:03
What is the string argument about in this getBean(String, Class)? Can you please explain? –  Freakyuser Jan 25 '13 at 9:43
It's a name of the requested bean in application context. It corresponds to id or name attribute in XML configuration. –  axtavt Jan 25 '13 at 10:38

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

share|improve this answer

Your Answer


