Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 工厂方法从DI容器获取新实例_Java_Spring_Spring Boot_Dependency Injection_Factory Pattern - Fatal编程技术网

Java 工厂方法从DI容器获取新实例

Java 工厂方法从DI容器获取新实例,java,spring,spring-boot,dependency-injection,factory-pattern,Java,Spring,Spring Boot,Dependency Injection,Factory Pattern,我试图在JavaSpring引导应用程序中创建一个工厂方法。但我不想手动实例化一个对象,而是想从DI容器中获取它。可能吗 public interface PaymentService { public Payment createPayment(String taskId); } public class PaymentServiceImplA implements PaymentService { private JobService jobService; pri

我试图在JavaSpring引导应用程序中创建一个工厂方法。但我不想手动实例化一个对象,而是想从DI容器中获取它。可能吗

public interface PaymentService {
    public Payment createPayment(String taskId);
}

public class PaymentServiceImplA implements PaymentService {
    private JobService jobService;
    private ApplicationService applicationService;
    private UserService userService;
    private WorkService workService;

    @Inject
    public PaymentServiceImplA(JobService jobService, UserService userService, WorkService workService,
        ApplicationService applicationService) {
        this.jobService = jobService;
        this.applicationService = applicationService;
        this.userService = userService;
        this.workService = workService;
        //removed other constructor injected dependencies
    }
}
调用getBean方法时,获取错误“没有类型为'com.test.mp.service.PaymentServiceImplA'的合格bean可用”

@Configuration
public class PaymentFactory {

    private ApplicationContext applicationContext;

    @Inject
    public PaymentFactory(ApplicationContext applicationContext) {      
        this.applicationContext = applicationContext;
    }

    @Bean
    public PaymentService paymentService(){
        //Using getBean method doesn't work, throws error mentioned above             
        if(condition == true) 
            return applicationContext.getBean(PaymentServiceImplA.class);
        else
            return applicationContext.getBean(PaymentServiceImplB.class);

    }
}

这可以在配置文件中再创建两个bean后解决。i、 e

@Bean
public PaymentService paymentServiceA(){
 return new PaymentServiceImplA();
}

@Bean
public PaymentService paymentServiceB(){
 return new PaymentServiceImplA();
}
返回的bean应该是:

   @Bean
    public PaymentService paymentService(){            
        if(condition == true) 
            return paymentServiceA();
        else
            return paymentServiceB();

    }

是的,在ServiceLocatoryFactoryBean的帮助下这是可能的。事实上,若我们编写工厂代码来创建实现对象,那个么在那个实现类中,若我们注入任何存储库或其他对象,那个么它将抛出异常。原因是,如果用户创建了对象,那么对于这些对象,spring不允许注入依赖项。因此,最好给出使用Spring通过工厂模式创建实现对象的责任。试试ServiceLocatorFactoryBean

这就是我现在解决这个问题的方法。通过向bean方法注入实例化实现对象所需的依赖项

@Configuration
public class PaymentFactory {

    //private ApplicationContext applicationContext;

    public PaymentFactory() {      
        //this.applicationContext = applicationContext;
    }

    @Bean
    public PaymentService paymentService(JobService jobService, UserService userService
    , WorkService workService, ApplicationService applicationService){

        if(condition == true){
            return new PaymentServiceImplA(jobService, userService, workService,
    applicationService);
        }
        else {
            return new PaymentServiceImplB(jobService, userService, workService,
    applicationService);
        }
    }
}

它应该是可用的,因为?我没有看到
@服务
,因此它不会被检测到。此外,我建议您编写自己的
@条件
规则(根据
条件
的不同,您甚至可以使用许多现有的容器中的一个。我想使用DI容器,因为实现类需要在构造函数中注入许多对象。这些对象反过来又有许多依赖项,所以我不想手动实例化它们。我将检查@Conditional规则。谢谢s!这与没有
@Service
有什么关系?我强烈建议看一看条件规则(这些规则也可以应用于带注释的类)。此外,您还可以添加任何注释(组件/服务)在PaymentServiceImplA和PaymentServiceImplB类上,以避免手动写入配置。谢谢!我将尝试此操作并还原。而不是使用
返回新的PaymentServiceImplA()
是否有办法从DI容器中获取对象。因为实例化对象需要很多if嵌套依赖项。谢谢!我会试试这个。如果您可以添加任何示例代码,也会很有帮助。