[英]Multiple profiles - spring bean injection
@Service
public interface DatabaseConnector {
public Model getModel();
}
@Configuration
@Profile({"!test"})
public class DatabaseConnectorT implements DatabaseConnector {
private final Model model;
@Autowired
public DatabaseConnectorT(@Value("${assemblerFile}") String assemblerFile) {
model = TDBFactory.assembleModel(assemblerFile);
}
}
I am getting the error, that this bean class cannot be instantiated and a default constructor should be given. 我收到错误消息,该bean类无法实例化,应提供默认构造函数。 Why should such one be required?
为什么需要这样一个? I have two different implementations of the interface which should be loaded dependent on the active profile.
我有两种不同的接口实现,应根据活动配置文件进行加载。 Configuration should be done with annotations.
配置应带有注释。
Default constructor is a constructor without parameters. 默认构造函数是不带参数的构造函数。
According to the error message I suppose that the class is being created via reflection. 根据错误消息,我认为该类是通过反射创建的。 So you must provide a way to create a class via reflection.
因此,您必须提供一种通过反射创建类的方法。
I assumed the value of assemblerFile in your constructor can't be determined via reflection and so the default constructor is called instead, which is not provided. 我假设无法通过反射确定构造函数中assemblerFile的值,因此将调用默认构造函数,但未提供该默认构造函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.