我承认我对春天还很陌生.我在一个Spring Boot应用程序中工作,其中我正在使用一些旧代码.在许多DAO类中,构造函数会通过覆盖抽象方法来实例化抽象类,如下所示
@Component("exampleDAO_1") // the DAO classes are already managed by Spring
public class ExampleDAO_1 implements IExampleDAO_1 {
...
private static final String PROPERTY_NAME = "property_name_1";
AbstractClass property;
public ExampleDAO_1() {
property = new AbstractClass() {
@Override
protected String abstractMethod() {
return PROPERTY_NAME;
}
};
}
...
}
抽象类看起来就像这样
public abstract class AbstractClass {
@Value("${spring.profiles.active}") // Ultimately I want the class managed by Spring so I can access this value properly
private String activeProfile;
protected AbstractClass() {
this(true);
}
protected AbstractClass(boolean withEnvironment) {
...
// use abstractMethod() here to further set some other fields in the class
...
}
protected abstract String abstractMethod();
}
我想注入抽象类,而不是通过通常的new
关键字实例化它并覆盖抽象方法,这样我就可以管理对象Spring.做这件事最好的办法是什么?
我试着首先使类具体化,然后将属性名传递到构造函数中,然后将值作为新字段保存在类中,但我不确定从哪里开始,或者这是否是正确的开始方式.如果我的要求或所说的没有意义,我向你道歉.