Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/spring-boot/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在主类中加载@Value注释?_Java_Spring Boot - Fatal编程技术网

Java 如何在主类中加载@Value注释?

Java 如何在主类中加载@Value注释?,java,spring-boot,Java,Spring Boot,为什么主类不能读取@Value注释?在我的其他类中,我必须将@Configuration、@ComponentScan、@EnableAutoConfiguration置于类之上,然后将@PostConstruct置于构造函数之上。我能够从application.properties中检索这些值。对于主类,我怎样才能做到这一点呢?问题是您不能将@值设置为静态,因为Spring可能还没有初始化它们。但是,在初始化spring之前,您可以通过从系统/环境变量而不是通过属性文件检索它们来获得所需的值

为什么主类不能读取@Value注释?在我的其他类中,我必须将@Configuration、@ComponentScan、@EnableAutoConfiguration置于类之上,然后将@PostConstruct置于构造函数之上。我能够从application.properties中检索这些值。对于主类,我怎样才能做到这一点呢?

问题是您不能将
@值设置为静态,因为Spring可能还没有初始化它们。但是,在初始化spring之前,您可以通过从系统/环境变量而不是通过属性文件检索它们来获得所需的值


String value=System.getProperty(“SOME_KEY”)

问题是您不能使
@值成为静态的,因为Spring可能还没有初始化它们。但是,在初始化spring之前,您可以通过从系统/环境变量而不是通过属性文件检索它们来获得所需的值


String value=System.getProperty(“SOME_KEY”)

因此,3年后,我向您展示了一种实现这一点的方法

@SpringBootApplication
public class App {

    @Value("${Application}")
    private String application;
    @Value("${APP_SERVER_CPU_ENABLED}")
    private String APP_SERVER_CPU_ENABLED;
    @Value("${APP_SERVER_MEMORY_ENABLED}")
    private String APP_SERVER_MEMORY_ENABLED;

    @Autowired
    MonitoringItems mI;

    public static void main(String[] args) {
    SpringApplication.run(App.class, args);
    MonitoringItems mI=null;                    

    try {
        System.out.println(APP_SERVER_CPU_ENABLED);
        System.out.println(APP_SERVER_MEMORY_ENABLED);

if (APP_SERVER_MEMORY_ENABLED.equalsIgnoreCase("true") && APP_SERVER_CPU_ENABLED.equalsIgnoreCase("true")) {
  //do something 
  }
当然,您可以在文件属性(如application.properties)中创建属性,例如:

  @SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Demo1Application .class, args);
        Test test = context.getBean(Test.class);

        String password = test.getPassword();

        System.out.println(password);

    }

    @Component
    class Test {

        @Value("${password}")
        private String password;

        public String getPassword() {
            return password;
        }
    }
}

3年后,我向你展示了一个这样做的技巧

@SpringBootApplication
public class App {

    @Value("${Application}")
    private String application;
    @Value("${APP_SERVER_CPU_ENABLED}")
    private String APP_SERVER_CPU_ENABLED;
    @Value("${APP_SERVER_MEMORY_ENABLED}")
    private String APP_SERVER_MEMORY_ENABLED;

    @Autowired
    MonitoringItems mI;

    public static void main(String[] args) {
    SpringApplication.run(App.class, args);
    MonitoringItems mI=null;                    

    try {
        System.out.println(APP_SERVER_CPU_ENABLED);
        System.out.println(APP_SERVER_MEMORY_ENABLED);

if (APP_SERVER_MEMORY_ENABLED.equalsIgnoreCase("true") && APP_SERVER_CPU_ENABLED.equalsIgnoreCase("true")) {
  //do something 
  }
当然,您可以在文件属性(如application.properties)中创建属性,例如:

  @SpringBootApplication
public class Demo1Application {

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(Demo1Application .class, args);
        Test test = context.getBean(Test.class);

        String password = test.getPassword();

        System.out.println(password);

    }

    @Component
    class Test {

        @Value("${password}")
        private String password;

        public String getPassword() {
            return password;
        }
    }
}

[oops,编辑注释]您的应用程序类不是spring管理的bean。不确定spring是否会将值设置为静态成员。[哦,编辑注释]您的应用程序类不是spring管理的bean。不确定spring是否会将值设置为静态成员。是否可以在另一个类中设置这些变量,然后将其调用到主类?否。在类中使用
@Autowired
@Value
要求类本身是Bean,它仅在Spring初始化自身并将bean添加到其容器时创建。如果要创建一个类,然后创建一个实例并尝试获取值,它们将为null=)是否可以在另一个类中设置这些变量,然后将其调用到主类?否。在类中使用
@Autowired
@Value
要求该类本身就是一个Bean,它仅在Spring初始化自身并将bean添加到其容器时创建。如果您要创建一个类,然后创建该类的一个实例并尝试获取这些值,它们将为null=)