我正在创建 pojo 类并存储 application.properties 变量,但我得到空值
注意:需要从我的 Abstract 类访问 env
POJO类
package mynt.xyz.c4.pushnotif.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration("notificationEnvironment")
@ConfigurationProperties(prefix = "app.notif")
public class NotificationEnvironment {
private String key;
private String url;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
使用@autowired 初始化类
public abstract class NotificationBase {
@Autowired
NotificationEnvironment notificationEnvironment;
public void getEnv(){
system.out.println(notificationEnvironment.getKey()); // null value
}
}
扩展到我的 NotificationBaseClass 的具体类
@Component
@Qualifier("androidNotification")
public class AndroidNotification extends NotificationBase implements Notification {
public AndroidNotification(String message, String title, String datalink, List<String> instanceIds) {
super(message, title, datalink, instanceIds);
}
AndroidNotification(){
super();
}
@Override
public void send() {
this.getEnv();
}
}
应用程序属性
app.notif.key=jkashdkjashd
app.notif.url=https/some.url