Java 在Spring引导应用程序中实例化@Service@事务类的多个bean
我对Spring boot比较陌生。我正在开发一个springboot应用程序,需要为同一个POJO注入两个不同的bean 到目前为止,我的服务类别如下:Java 在Spring引导应用程序中实例化@Service@事务类的多个bean,java,spring,spring-boot,spring-annotations,Java,Spring,Spring Boot,Spring Annotations,我对Spring boot比较陌生。我正在开发一个springboot应用程序,需要为同一个POJO注入两个不同的bean 到目前为止,我的服务类别如下: @Service @Transactional public class StudentServiceImpl implements StudentService { private final StudentHelper studentHelper; private final Validator validator; p
@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);
}
...
}
@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);
}
}
在这里,正如我上面提到的,我没有得到任何关于如何将bean之一作为
@Service
@Transactional
的线索,这将从资源中调用。有人能帮忙吗?谢谢。您不需要在类上声明@Service,因为当您没有@Bean配置时,它用于自动检测Bean
对于事务,您可以从类中省略@Transactional,并通过在添加@Bean声明时手动创建用于添加事务的代理类来实现相同的功能。请参阅以下内容: