问题描述
嗨,我正在尝试使用策略设计模式。我在 TestServiceImpl 类中将 ReEncryptionoperation bean 设为 null。
这是我的界面
public interface ReEncryptionoperation {
void performOperation (String name);
}
这些是我的实现类
public class Test1 implements ReEncryptionoperation {
@Override
public void performOperation(String name){
return ....;
}
}
public class Test2 implements ReEncryptionoperation {
@Override
public void performOperation(String name) {
return ....;
}
}
这是我定义为 bean 的配置类
@Configuration
@Slf4j
public class TestConfiguration
{
@Bean("reEncryptionoperation")
public ReEncryptionoperation getReEncryptionoperation () throws ReEncryptionException {
if (annotationSupport) {
return new Test1();
}
return new Test2();
}
}
这是我尝试使用@Autowired 使用ReEncryptionoperation 的服务类。但我得到了空值。
@Component
@Slf4j
public class TestServiceImpl
{
@Autowired
private ReEncryptionoperation reEncryptionoperation;
public ReEncryptionResponse submitJob (
final ReEncryptionRequest reEncryptionRequest) throws ReEncryptionException
{
reEncryptionoperation.performOperation(test);
}
}
解决方法
你的配置看起来没问题。
检查 TestConfiguration
是否位于 spring 扫描的包中。
为了确保您的 bean 是在运行时创建的,请在方法 getReEncryptionOperation