基于依赖类为公共依赖项提供不同的构造函数参数

时间:2015-11-03 08:52:57

标签: java spring spring-boot java-8

我使用Spring Boot并使用注释对其进行配置。说我有以下课程:

@FunctionalInterface
@Component
public interface VerificationStrategy {
    void verify(Foo foo) throws Exception;
}

@Component
public class A {
    @Autowired
    public A(VerfificationStrategy assumption) {}
}

@Component
public class B {
    @Autowired
    public B(A a) {}
}

@Component
public class C {
    @Autowired
    public C(A a) {}
}

现在我想要实现的目标:

  • B的实例应接收A的实例,其中一个实现为VerificationStrategy

  • C的实例应该接收A的实例以及VerificationStrategy的其他实现

使用注释实现这一目标的最优雅,最有效的方法是什么?

1 个答案:

答案 0 :(得分:1)

AFAIK使用Spring是不可能的。您需要创建两个A类实现。

但你可以这样做:

int sum=0;
for (int x = 0; x < Tsum1.length; x++) {
    sum= sum+ (Tsum2[x] - Isum2[x]);
}