实现与弹簧3注释简单工厂模式 [英] Implement a simple factory pattern with Spring 3 annotations
问题描述
我想知道我怎么能实现与Spring 3注解简单工厂模式。我说,你可以创建调用工厂类和经营的工厂方法豆的文档中看到的。我在想,如果这是可能的使用注释只。
I was wondering how I could implement the simple factory pattern with Spring 3 annotations. I saw in the documentation that you can create beans that call the factory class and run a factory method. I was wondering if this was possible using annotations only.
我有当前通话
MyService myService = myServiceFactory.getMyService(test);
result = myService.checkStatus();
为MyService是一个方法接口叫的checkStatus()。
MyService is an interface with one method called checkStatus().
我的工厂类看起来是这样的:
My factory class looks like this:
@Component
public class MyServiceFactory {
public static MyService getMyService(String service) {
MyService myService;
service = service.toLowerCase();
if (service.equals("one")) {
myService = new MyServiceOne();
} else if (service.equals("two")) {
myService = new MyServiceTwo();
} else if (service.equals("three")) {
myService = new MyServiceThree();
} else {
myService = new MyServiceDefault();
}
return myService;
}
}
MyServiceOne类看起来是这样的:
MyServiceOne class looks like this :
@Autowired
private LocationService locationService;
public boolean checkStatus() {
//do stuff
}
当我运行这个code中的locationService变量是alwasy空。我beleive这是因为我创建的对象自己在工厂里面,并自动连接没有发生。有没有办法添加注释正确地使这项工作?
When I run this code the locationService variable is alwasy null. I beleive this is because I am creating the objects myself inside the factory and autowiring is not taking place. Is there a way to add annotations to make this work correctly?
感谢
推荐答案
您是对的,通过创建对象手动你是不是让Spring来自动装配执行。考虑由Spring管理服务的,以及:
You are right, by creating object manually you are not letting Spring to perform autowiring. Consider managing your services by Spring as well:
@Component
public class MyServiceFactory {
@Autowired
private MyServiceOne myServiceOne;
@Autowired
private MyServiceTwo myServiceTwo;
@Autowired
private MyServiceThree myServiceThree;
@Autowired
private MyServiceDefault myServiceDefault;
public static MyService getMyService(String service) {
service = service.toLowerCase();
if (service.equals("one")) {
return myServiceOne;
} else if (service.equals("two")) {
return myServiceTwo;
} else if (service.equals("three")) {
return myServiceThree;
} else {
return myServiceDefault;
}
}
}
不过,我会考虑的整体设计是相当差。岂不更好有一个一般为MyService
执行情况,并通过有一个
/ 两个
/ 三
字符串作为额外的参数的checkStatus()
?你想要什么来实现?
But I would consider the overall design to be rather poor. Wouldn't it better to have one general MyService
implementation and pass one
/two
/three
string as extra parameter to checkStatus()
? What do you want to achieve?
@Component
public class MyServiceAdapter implements MyService {
@Autowired
private MyServiceOne myServiceOne;
@Autowired
private MyServiceTwo myServiceTwo;
@Autowired
private MyServiceThree myServiceThree;
@Autowired
private MyServiceDefault myServiceDefault;
public boolean checkStatus(String service) {
service = service.toLowerCase();
if (service.equals("one")) {
return myServiceOne.checkStatus();
} else if (service.equals("two")) {
return myServiceTwo.checkStatus();
} else if (service.equals("three")) {
return myServiceThree.checkStatus();
} else {
return myServiceDefault.checkStatus();
}
}
}
这是的仍然设计不当,因为添加新的为MyService
的实施需要 MyServiceAdapter
修改为以及(SRP违例)。但其实这是一个很好的起点。(提示:地图和策略模式)
This is still poorly designed because adding new MyService
implementation requires MyServiceAdapter
modification as well (SRP violation). But this is actually a good starting point (hint: map and Strategy pattern).
这篇关于实现与弹簧3注释简单工厂模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!