Implementing custom factory pattern in Spring

2019-08-28 20:00发布

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 are implementations of the interface MyService.

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

I've looked through some examples and I know how to make Spring container create my objects depending on a 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?

1条回答
地球回转人心会变
2楼-- · 2019-08-28 20:37

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;
         }
         */
     }
}

EDIT :

Answers to your questions in comment :

Isn't it the same with getting by String?

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

I mean how should my spring.xml look like?

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

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

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

查看更多
登录 后发表回答