在Spring中实现自定义工厂模式 [英] Implementing custom factory pattern in Spring

查看:104
本文介绍了在Spring中实现自定义工厂模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,如果我想实现工厂模式,我会这样做.

Normally if I want to implement a factory pattern I will do it like this.

public class CustomFactory(){

     // pay attention: parameter is not a string
     public MyService getMyService(Object obj){
     /* depending on different combinations of fields in an obj the return
        type will be MyServiceOne, MyServiceTwo, MyServiceThree
     */
     }
}

MyServiceOne,MyServiceTwo,MyServiceThree是MyService接口的实现.

MyServiceOne, MyServiceTwo, MyServiceThree are implementations of the interface MyService.

那将很好地工作. 但是问题是我想让Spring容器实例化我的对象.

That will work perfectly fine. But the issue is that I would like to have my objects instanciated by Spring container.

我已经浏览了一些示例,并且知道如何使Spring容器根据字符串创建对象.

I've looked through some examples and I know how to make Spring container create my objects depending on a string.

问题是:我可以在此示例中包含Spring Container的对象实现,还是应该在其他位置使用Object obj进行所有操作,并在CumtomFactory中编写public MyService getMyService(String string)方法?

The queston is: can I include implemenations of objects by Spring Container in this example or should I make all my manipulations with Object obj in some other place and write a method public MyService getMyService(String string) in my CumtomFactory?

推荐答案

那么您如何看待以下方式? :

Well what do you think about following way? :

public class CustomFactory {
    // Autowire all MyService implementation classes, i.e. MyServiceOne, MyServiceTwo, MyServiceThree
    @Autowired
    @Qualifier("myServiceBeanOne")
    private MyService myServiceOne; // with getter, setter
    @Autowired
    @Qualifier("myServiceBeanTwo")
    private MyService myServiceTwo; // with getter, setter
    @Autowired
    @Qualifier("myServiceBeanThree")
    private MyService myServiceThree; // with getter, setter

     public MyService getMyService(){
         // return appropriate MyService implementation bean
         /*
         if(condition_for_myServiceBeanOne) {
             return myServiceOne;
         }
         else if(condition_for_myServiceBeanTwo) {
             return myServiceTwo;
         } else {
             return myServiceThree;
         }
         */
     }
}

在评论中回答您的问题:

Answers to your questions in comment :

通过String获取不一样吗?

Isn't it the same with getting by String?

->肯定是的,您是从Spring获得的那些豆.

--> Yes definitely, you are getting those beans from Spring.

我的意思是我的spring.xml应该是什么样子?

I mean how should my spring.xml look like?

->参见下面的xml:

--> See below xml :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

  <!-- services -->

  <bean id="myServiceBeanOne"
        class="com.comp.pkg.MyServiceOne">
    <!-- additional collaborators and configuration for this bean go here -->
  </bean>

  <bean id="myServiceBeanTwo"
        class="com.comp.pkg.MyServiceTwo">
    <!-- additional collaborators and configuration for this bean go here -->
  </bean>

  <bean id="myServiceBeanThree"
        class="com.comp.pkg.MyServiceThree">
    <!-- additional collaborators and configuration for this bean go here -->
  </bean>    
  <!-- more bean definitions for services go here -->

</beans>

我应该在getMyService方法中做什么?只是返回新的MyServiceOne()等等?

And what should i do inside getMyService method? just return new MyServiceOne() and so on or what?

->请参见上面代码中的getMyService()方法,该方法已更新.

--> See getMyService() method in above code, it is updated.

这篇关于在Spring中实现自定义工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆