简体   繁体   English

在 Spring 启动应用程序中实例化 @Service @Transactional class 的多个 bean

[英]Instantiating multiple beans of the @Service @Transactional class in Spring boot application

I am relatively new to Spring boot.我对 Spring 启动比较陌生。 I am working on a spring boot application, where I need to inject two different beans for the same POJO.我正在开发一个 spring 启动应用程序,我需要为同一个 POJO 注入两个不同的 bean。

As of now, I have a service class as follows:截至目前,我有一个服务 class 如下:

@Service
@Transactional
public class StudentServiceImpl implements StudentService {

  private final StudentHelper studentHelper;
  private final Validator validator;


  public StudentServiceImpl(
      StudentHelper studentHelper,
      Validator validator) {
    this.studentHelper = studentHelper;
    this.validator = validator;
  }

  @Override
  public List<Student> generateReport(String courseId) {
     ...
     if(validator != null) {
         validator.validate(courseId);
     }
     ...
  }

Now, I would like to instantiate two different beans for the same POJO: StudentServiceImpl , one with a proper validator and another with a validator as null.现在,我想为同一个 POJO 实例化两个不同的 bean: StudentServiceImpl ,一个具有适当的验证器,另一个具有 null 的验证器。 Actually StudentServiceImpl is being used from two flows: One, called from a resource where a validator is required, and in another, called from scheduler where it is not required.实际上, StudentServiceImpl正在从两个流程中使用:一个是从需要验证器的资源调用,另一个是从不需要验证器的调度程序调用。

In this regard, I have seen multiple examples, but I am not getting a clue how to do that for making two beans where one is to be used as Transactional Service class as mentioned above and another to be used as a simple component.在这方面,我已经看到了多个示例,但我不知道如何制作两个 bean,其中一个用作事务服务 class,如上所述,另一个用作简单组件。

Basically, I could figure out that, I have to write a config as follows:基本上,我可以弄清楚,我必须编写如下配置:

@Configuration
public class StudentServiceConfig {

    @Bean   //THIS BEAN IS TO BE USED AS TRANSACTIONAL SERVICE AS MENTIONED ABOVE
    public StudentServiceImpl studentServiceOne(StudentHelper helper, Validator validator) {
        return new StudentServiceImpl(helper, validator);
    }

    @Bean
    public StudentServiceImpl studentServiceTwo(StudentHelper helper) {
        return new StudentServiceImpl(helper, null);
    }
}

Here, as I mentioned above, I am not getting any clue regarding how to make one of the bean as @Service @Transactional , which is to be called from the resource.在这里,正如我上面提到的,我没有得到任何关于如何将 bean 之一作为@Service @Transactional的任何线索,它将从资源中调用。 Could anyone please help here?有人可以帮忙吗? Thanks.谢谢。

You don't need to declare @Service on the class as that is used for auto detecting beans when you don't have @Bean configurations.您不需要在 class 上声明 @Service,因为当您没有 @Bean 配置时,它用于自动检测 bean。

For transactions, you can omit @Transactional from class and achieve the same functionality by manually creating proxy class for adding transaction while adding @Bean declaration.对于事务,您可以在 class 中省略 @Transactional,并通过手动创建代理 class 以在添加 @Bean 声明时添加事务来实现相同的功能。 Refer to following:参考以下:

Spring @Transactional on @Bean declaration instead of class Implementation Spring @Transactional on @Bean 声明而不是 class 实现

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

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