簡體   English   中英

如何創建一個bean作為Interface類的實例

[英]How to create a bean as instance of Interface Class

我有三個類1)接口2)實現接口的類3)靜態工廠,該工廠將實例的第二類作為實例接口類返回

public Interface MyInterface {
}

public class Myclass implements MyInterface {
}

public Class MyStaticFactory {

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

我想創建MyClass的bean作為實例MyInterface。 Mycode的是

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

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

但是使用上面的代碼我得到了一個例外

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)

我正在嘗試使用以下代碼訪問bean

@Autowired
private MyInterface MyclassInstance

創建一個bean作為InterfaceClass的實例是否合法? 我們如何使用spring在MyInterface的類變量中加載MyClass的實例?

您不需要自動連接任何字段,因為您有自己的工廠可以為您實例化bean。 另外,您不需要在其中使用靜態方法實例化工廠類。 相反,您可以繼續使用:

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

現在,假設您有MyInterface的多個實現,可以通過傳遞如下參數來實現:

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

在工廠類中,可以使用switch(來自JDK 7),也可以使用梯子檢查方法getInstance方法的參數中所請求的內容,並實例化適當的bean。

然后,您可以執行以下操作:

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

我對此的看法取決於上下文,無論是使用基於XML的bean創建還是使用基於Java的“ config” bean創建,邏輯都差不多。

如果要使用Java配置來創建Bean,則需要有一個@Configuration注釋的類,其次,需要將Configuration類注冊為Bean創建者,即應用程序上下文必須能夠根據以下情況創建和初始化Bean:的類型(單例或原型)。

我的配置如下所示:

public Interface MyInterface {
    void executeSomeMethod();
}

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

Configuration類如下所示:

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

假設MyConfigurationClass已在應用程序上下文中注冊,則使用Bean的過程類似於:

@Component
public class SomeClassUsingTheBean {

    @Autowired
    MyInterface someBeanName;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM