简体   繁体   English

在 Spring Boot 服务中使用多态性?

[英]Using Polymorphism in Spring Boot services?

In my Spring Boot app, I am thinking of using an approach as the following interface and service implementations:在我的 Spring Boot 应用程序中,我正在考虑使用一种方法作为以下接口和服务实现:

PDFService: PDF服务:

public interface PDFService {
    String createPdf(UUID uuid);
}

BrandPDFService:品牌PDF服务:

@Service
@RequiredArgsConstructor
public class BrandPDFService implements PDFService {
    
    private final BrandService brandService;

    @Override
    public String createPdf(UUID uuid) {
        Brand brand = brandService.findByUuid(uuid);

        // ... code omitted for brevity

        return generateHtml(brand);
    }
}

ProductPDFService:产品PDF服务:

@Service
@RequiredArgsConstructor
public class ProductPDFService implements PDFService {

    private final ProductService productService;

    @Override
    public String createPdf(UUID uuid) {
        Product product = productService.findByUuid(uuid);

        // ... code omitted for brevity

        return generateHtml(product);
    }
}

For using these services:使用这些服务:

// brand way
PDFService pdfService = new BrandService();
pdfService.createPdf(uuid);


// product way
PDFService pdfService = new ProductService();
pdfService.createPdf(uuid);

So, I think I need to use generic and pass it to PDFService and then their implementations, but I am not sure how to make it properly (using generic or passing via constructor).所以,我认为我需要使用泛型并将其传递给 PDFService 然后它们的实现,但我不确定如何正确地制作它(使用泛型或通过构造函数传递)。 So, in order to use createPdf efficiently without repeating code (I know I can also use Template Pattern method, but I just wanted to know polymorphism side) how should I apply polymorphism to these Spring Boot Services properly?那么,为了有效地使用createPdf而无需重复代码(我知道我也可以使用模板模式方法,但我只是想了解多态性方面)我应该如何将多态性正确应用于这些 Spring Boot 服务?

Since BrandPDFService and ProductPDFService are Spring beans (because you annotated them with the @Service annotation), you should not be instantiating them yourself by using new .由于BrandPDFServiceProductPDFService是 Spring bean(因为您使用@Service注释对它们进行了注释),因此您不应该使用new自己实例化它们。 Instead, you should let Spring autowire them into the class where you are using them.相反,您应该让 Spring 将它们自动装配到您使用它们的类中。

Because they are both implementations of interface PDFService , when you autowire them, you need to have something to let Spring distinguish them.因为它们都是接口PDFService的实现,所以当你自动装配它们时,你需要有一些东西让 Spring 区分它们。 Otherwise, if the field you are autowiring them in is of type PDFService , Spring won't know which implementation of the interface to autowire.否则,如果您在其中自动装配它们的字段是PDFService类型,Spring 将不知道要自动装配的接口的哪个实现。 You can give the beans names and use the @Qualifier annotation:您可以给 bean 命名并使用@Qualifier注释:

@Service("brandPDFService")
public class BrandPDFService implements PDFService { ... }

@Service("productPDFService")
public class ProductPDFService implements PDFService { ... }

// Example controller where you autowire them
@RestController
public class MyController {

    @Autowired
    @Qualifier("brandPDFService")
    private PDFService brandPDFService;

    @Autowired
    @Qualifier("productPDFService")
    private PDFService productPDFService;

    // ...
}

So, I think I need to use generic and pass it to PDFService and then their implementations所以,我认为我需要使用泛型并将其传递给 PDFService 然后它们的实现

I don't know why you think you need to use generics;我不知道你为什么认为你需要使用泛型; this doesn't have anything to do with generics.这与泛型没有任何关系。

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

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