Java 工厂方法回位弹簧服务
我想要一个工厂类,它返回一个我可以用来做一些验证的服务。我实现了这个类Java 工厂方法回位弹簧服务,java,spring,spring-boot,factory-pattern,factory-method,Java,Spring,Spring Boot,Factory Pattern,Factory Method,我想要一个工厂类,它返回一个我可以用来做一些验证的服务。我实现了这个类 public class EventUpdateValidatorFactory { public EventUpdateValidatorStrategy getValidator(EEventStatus eventStatus) { if (SECOND_APPROVAL.equals(eventStatus)) { return new EventSecondApp
public class EventUpdateValidatorFactory {
public EventUpdateValidatorStrategy getValidator(EEventStatus eventStatus) {
if (SECOND_APPROVAL.equals(eventStatus)) {
return new EventSecondApprovalValidator();
} else if (APPROVED.equals(eventStatus)) {
return new EventApprovedValidator();
} else if (ACCOUNTING_HQ.equals(eventStatus)) {
return new EventAccountingHqValidator();
}
throw new IllegalArgumentException("Unknown status");
}
}
接口EventUpdateValidatorStrategy如下
结果是,当我在EventSecondApprovalValidator中输入时,所有的自动连线服务都是空的,很明显,我在第一次使用其中一个服务时收到一个NPE
如何正确使用factory返回基于EEventStatus所需的服务?手动创建对象不能让Spring执行自动布线。考虑通过Spring管理您的服务。
@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();
}
}
}
在EventUpdateValidatorFactory.GetValidateEventStatus方法中,需要从上下文返回EventSecondApprovalValidator bean,而不是使用new关键字创建新实例
类EventSecondApprovalValidator带有@Service注释,并且假设只有一个此类,则Spring将向ApplicationContext添加一个此类实例,并注入所有依赖项。所以,只需从上下文中获取并使用它
一种快速的方法如下:
public EventUpdateValidatorStrategy getValidator(ApplicationContext context,
EEventStatus eventStatus) {
if (SECOND_APPROVAL.equals(eventStatus)) {
return context.getBean(EventSecondApprovalValidator.class);
} else if (APPROVED.equals(eventStatus)) {
return context.getBean(EventApprovedValidator.class);
} else if (ACCOUNTING_HQ.equals(eventStatus)) {
return context.getBean(EventAccountingHqValidator.class);
}
throw new IllegalArgumentException("Unknown status");
}
您还可以在EventUpdateValidatorFactory中@Autowire所有验证程序,并返回@Autowired实例。这将保持getValidator方法的签名相同,但您必须使EventUpdateValidatorFactory成为@Component类
@Component
public class EventUpdateValidatorFactory {
@Autowired
EventSecondApprovalValidator a;
@Autowired
EventApprovedValidator b;
@Autowired
EventAccountingHqValidator c;
public EventUpdateValidatorStrategy getValidator(EEventStatus eventStatus) {
if (SECOND_APPROVAL.equals(eventStatus)) {
return a;
} else if (APPROVED.equals(eventStatus)) {
return b;
} else if (ACCOUNTING_HQ.equals(eventStatus)) {
return c;
}
throw new IllegalArgumentException("Unknown status");
}
对不起,我不明白你的例子。我喜欢你的第二个解决方案。非常感谢你!是的,两种解决方案都有效!但我认为你的第二个解决方案更优雅。你怎么看?大多数考虑应用程序。GETBEN是一个反模式/滥用/滥用。所以,是的,使用第二个是有意义的。这两种方法都可以很好地工作,您不必手动创建工厂,因为spring已经有了一个SpringBean工厂来实现这一点。简单的规则是,如果您正在自动连接@Autowired Myclass obj,那么您应该在其中公开@Component Myclass{},或者在@Configuration class中创建一个@Bean方法,该方法提供Myclass类型的obj。
@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();
}
}
}
public EventUpdateValidatorStrategy getValidator(ApplicationContext context,
EEventStatus eventStatus) {
if (SECOND_APPROVAL.equals(eventStatus)) {
return context.getBean(EventSecondApprovalValidator.class);
} else if (APPROVED.equals(eventStatus)) {
return context.getBean(EventApprovedValidator.class);
} else if (ACCOUNTING_HQ.equals(eventStatus)) {
return context.getBean(EventAccountingHqValidator.class);
}
throw new IllegalArgumentException("Unknown status");
}
@Component
public class EventUpdateValidatorFactory {
@Autowired
EventSecondApprovalValidator a;
@Autowired
EventApprovedValidator b;
@Autowired
EventAccountingHqValidator c;
public EventUpdateValidatorStrategy getValidator(EEventStatus eventStatus) {
if (SECOND_APPROVAL.equals(eventStatus)) {
return a;
} else if (APPROVED.equals(eventStatus)) {
return b;
} else if (ACCOUNTING_HQ.equals(eventStatus)) {
return c;
}
throw new IllegalArgumentException("Unknown status");
}