Spring - Injecting a Bean as a JDK interface based Proxy Object

[Updated: Mar 6, 2016, Created: Mar 6, 2016]

As alternative to our last example we can specify proxyMode as ScopedProxyMode.INTERFACES for @Scope annotation.

This mode needs to be specified if we are autowiring interface rather than a concrete class.

Spring injects JDK interface based proxy rather than CGLIB class based proxy.

This example is a modification of our previous example. We are going to create an interface for our prototype bean so that it can be injected into wider scoped singleton bean using JDK interface based proxy mode.

Configuring the Scoped Proxy for Prototype bean

public class AppConfig {

    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE,
                        proxyMode = ScopedProxyMode.INTERFACES)
    public MyPrototypeBean prototypeBean () {
        return new MyPrototypeBean();

    public MySingletonBean singletonBean () {
        return new MySingletonBean();

IPrototype Interface

public interface IPrototype {

    String getDateTime();

Prototype Bean implementation

public class MyPrototypeBean implements IPrototype {

    private String dateTimeString = LocalDateTime.now().toString();

    public String getDateTime() {
        return dateTimeString;

Autowiring the prototype bean into Singleton bean

public class MySingletonBean {

    private IPrototype prototypeBean;

    public void showMessage(){
        System.out.println("Hi, the time is "+prototypeBean.getDateTime());

Client code

  AnnotationConfigApplicationContext context =
                            new AnnotationConfigApplicationContext(AppConfig.class);
    MySingletonBean bean = context.getBean(MySingletonBean.class);

    bean = context.getBean(MySingletonBean.class);


Hi, the time is 2016-03-05T14:33:37.399
Hi, the time is 2016-03-05T14:33:38.454

Example Project

Dependencies and Technologies Used :

  • Spring Context 4.2.4.RELEASE: Spring Context.
  • JDK 1.8
  • Maven 3.0.4

Scoped Interface Based Proxy Example Select All Download
  • scoped-proxy-interface-example
    • src
      • main
        • java
          • com
            • logicbig
              • example

See Also