Uday Kiran
Uday Kiran

Reputation: 33

How to create a bean as instance of Interface Class

I have three classes 1)an Interface 2) A class which implements the interface 3) a static factory which return an instance second class as an instance Interface Class

public Interface MyInterface {
}

public class Myclass implements MyInterface {
}

public Class MyStaticFactory {

    public static MyInterface getInstance() {
        MyInterface myClassInstance = new Myclass();
        return myClassInstance;
     }
}

I want to create a bean of MyClass as an instance MyInterface. Mycode for that is

<bean id="staticFactory" class="MyStaticFactory"> 
</bean>

<bean id="myclass" class="MyInterface" factory-bean="staticFactory" factory-method="getInstance"> 
</bean>

But with the above code I am getting an exception

org.springframework.beans.factory.BeanCreationException: Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private MyInterface ; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [MyInterface] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at com.amazon.coral.reflect.instantiate.SpringInstantiatorFactory$1.newInstance(SpringInstantiatorFactory.java:175)

I am trying to access bean using the following code

@Autowired
private MyInterface MyclassInstance

Is It a valid thing to create a bean as as instance of InterfaceClass ? How can we load an instance of MyClass in a class variable of MyInterface using spring ?

Upvotes: 1

Views: 25489

Answers (2)

Amos Kosgei
Amos Kosgei

Reputation: 947

My view on this is depending on the context, whether using XML based bean creation or using Java based "config"bean creation, the logic is pretty much the same.

You need to have a Class annotated with @Configuration if using Java configuration for you to create your beans, secondly, the Configuration class needs to be registered as a bean creator, i.e. the application context needs to be able to create and initialize the beans depending on their type (either Singleton or Prototype).

My configuration would look like this:

public Interface MyInterface {
    void executeSomeMethod();
}

public class Myclass implements MyInterface {
    @override
    void executeSomeMethod(){
        //some code executed here  
    }
}

The Configuration class would look something like this:

@Configuration
public MyConfigurationClass {
    @Bean(name="someBeanName")
    public MyInterface createBean() {
        MyInterface bean = new Myclass();
        return bean;
    }
}

Assuming the MyConfigurationClass is registered in the application context, then using the bean would look something like:

@Component
public class SomeClassUsingTheBean {

    @Autowired
    MyInterface someBeanName;

    public SomType method() {
        someBeanName.executeSomeMethod();
    }
}

Upvotes: 2

SMA
SMA

Reputation: 37023

You dont need to autowire any field as you have your own factory that will instantiate bean for you. Also you dont need to instantiate your factory class with static method in it. Instead you just can stay with:

<bean id="staticFactory" class="MyStaticFactory" factory-method="getInstance" />

Now let's say you have multiple implementation of MyInterface, you could do so by passing a parameter like:

<bean id="staticFactory" class="MyStaticFactory" factory-method="getInstance" scope="prototype">
    <constructor-arg value="MyClass" />
</bean>

And in your factory class you could either use switch (from JDK 7) or if else ladder to check what's requested in parameter to method getInstance method and instantiate proper bean.

You could then do the following:

public static void main(String args[]) {
    Application context = new ......
    MyInterface myIface = context.getBean("staticFactory", MyInterface.class);
    //myIface.mymethod();
}

Upvotes: 3

Related Questions