简体   繁体   English

抽象工厂模式与2工厂方法

[英]Abstract Factory pattern with 2 factory methods

In a spring hibernate java project with(controller - services - dao - entity layers), i am having abstract factory pattern (the abstract class has 2 abstract methods). 在一个带有(controller - services - dao - entity layers)的spring hibernate java项目中,我有抽象工厂模式(抽象类有2个抽象方法)。 In the implemented classes of abstract factory pattern i am having dao methods(to run the named queries). 在抽象工厂模式的实现类中,我有dao方法(运行命名查询)。 Now when the request comes to the service impl the dao from another Service class it gives null pointer excecption I have autowired the dao classes in the services impl ` 现在当请求来自另一个服务类的服务impl时,它给出了空指针,我已经在服务impl中自动连接了dao类。

AbstractFruitService - [addToBasket() and removeFromBasket()] 
|
|- SeasonalFruitsServicsImpl - SeasonalFruitsDAO
| 
|- BerryFruitsServiceImpl - BerryFruitsDAO

i cant add @Service("abstractFruitService") to both SeasonalFruitsServicsImpl and BerryFruitsServiceImpl it gives error - conflicts with existing, non-compatible bean definition of same name and class 我不能将@Service(“abstractFruitService”)添加到SeasonalFruitsServicsImpl和BerryFruitsServiceImpl,它会给出错误 - 与同名和类的现有的,不兼容的bean定义冲突

Not sure if the spring static/instance factory method is useful in this case, i tries this also but didnt work 不确定spring static / instance factory方法在这种情况下是否有用,我也尝试了这个但是没有用

Is there any approach 有什么办法吗?

What you are doing rather looks like a Service Locator pattern, Service Locator is an alternative to Dependency Injection. 你正在做的事情看起来像Service Locator模式,Service Locator是Dependency Injection的替代品 So there's no reason to implement one if you are already using IoC Spring Container. 因此,如果您已经在使用IoC Spring Container,则没有理由实现它。

Can declare your SeasonalFruitsServiceImpl and BerryFruitsServiceImpl as separate beans, and request their instances from the container either using their respective interfaces SeasonalFruitsDAO and BerryFruitsDAO , or by providing them with the separate names(if you need to replace the implementations then you just register another implementations in the container). 可以将SeasonalFruitsServiceImplBerryFruitsServiceImpl声明为单独的bean,并使用各自的接口SeasonalFruitsDAOBerryFruitsDAO从容器中请求它们的实例,或者通过为它们提供单独的名称(如果您需要替换实现,那么您只需在其中注册另一个实现)容器)。

See Martin J. Fowler article 参见Martin J. Fowler的文章

If the service locator was provided by some 3rd party then you should create an instance of Service Locator 如果服务定位器是由某些第三方提供的,那么您应该创建一个服务定位器实例

<bean id="someServiceLocator" class="com.some3rdparty.ServiceLocator"/>
<bean id="seasonalFruitsDao" factory-bean="someServiceLocator" factory-method="createSeasonalFruitDao"/>
<bean id="berryFruitsDao" factory-bean="someServiceLocator" factory-method="createBerryFruitsDao"/>

or with java code configuration 或者使用java代码配置

@Configuration
public class someServiceLocatorConfiguration{
  @Bean
  public ServiceLocator locator(){
    return new com.some3rdparty.ServiceLocator();
  }
  @Bean()
  public SeasonalFruitsDao  seasonalFruitsDao(){
    return locator().createSeasonalFruitDao();
  }
  @Bean()
  public SeasonalFruitsDao  seasonalFruitsDao(){
    return locator().createSeasonalFruitDao();
  }
}

but of course you should never ever do it to instantiate objects developed by you. 但是当然你永远不应该这样做来实例化你开发的对象。

使用注释@Transactional(propagation = Propagation.REQUIRES_NEW)非常适合我的代码,在服务方法上我添加了这个功能,它运行良好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM