0

我正在创建 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
4

3 回答 3

0

创建你的类@Component 并在@ConfigurationProperties 中添加前缀值,就像这样。这对我有用,希望这也对你有用。

@Component
@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;
    }

}

您可以像这样使用此属性:

@Component
public class NotificationBase {

    private static NotificationEnvironment notificationEnvironment;

    @Autowired
    public NotificationBase(NotificationEnvironment notificationEnvironment){
        this.notificationEnvironment = notificationEnvironment;
    }

    public static void getEnv(){
        System.out.println(notificationEnvironment.getKey()); // null value
    }

}

于 2019-09-02T06:50:46.803 回答
0

这是OP作者提到的具体类定义之一。

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ConcreteNotification extends NotificationBase {

    @Autowired
    public ConcreteNotification(NotificationEnvironment notificationEnvironment) {
        super(notificationEnvironment);
    }
}

更新 NotificationBase 如下

public abstract class NotificationBase {

    NotificationEnvironment notificationEnvironment;

    public NotificationBase(NotificationEnvironment notificationEnvironment) {
        this.notificationEnvironment = notificationEnvironment;
    }

    public void getEnv(){
        System.out.println(notificationEnvironment.getKey());
    }
}

我用来获取配置值的控制器类

@RestController
public class ArticleCommentController {

    @Autowired
    ConcreteNotification concreteNotification;


    @RequestMapping(value = "/health_check", method = RequestMethod.GET)
    public void getDemo() {
        concreteNotification.getEnv();
    }
}

输出:

jkashdkjashd

于 2019-09-02T07:03:45.650 回答
0

您可以从类中自动连接@Configuration@Configuration

@Configuration 类可以使用@Autowired 引用任何其他@Configuration 类的实例。这是因为 @Configuration 类本身是作为单独的 Spring bean 实例化和管理的。

于 2019-09-02T06:40:56.597 回答