无法将属性值注入类参数(@Value annotatnion)

问题描述 投票:0回答:1

我正在学习Spring Boot,我可能有一个非常简单的问题,但对我来说还不够清楚。我正面临@Value注释的一些问题 - 我想知道为什么apprication属性不能注入class参数。

我使用Spring Initializr准备了一些非常基本的项目,并在我的“application.properties”资源中添加了一个属性。此外,我创建了另外两个类:“YellowCar”(工作正常)和“RedCar”(不起作用 - 参数无法正确注入)。

“application.properties”文件:

car.age=15

我申请的主要类别:

package com.example.helper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.env.Environment;

@SpringBootApplication
public class HelperApplication implements CommandLineRunner {

    @Autowired
    private Environment env;

    public static void main(String[] args) {
        SpringApplication.run(HelperApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println (new RedCar(env));
        System.out.println (new YellowCar());
    }

}

通过将Environment变量传递给构造函数来构建RedCar:

package com.example.helper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

@Component
public class RedCar {

    private int age;

    @Autowired
    public RedCar (Environment env) {
        this.age = new Integer(env.getRequiredProperty("car.age")).intValue();
    }

    @Override
    public String toString() {
        return "Car [age=" + age + "]";
    }

}

构建YellowCar时不将环境变量传递给构造函数,而是使用@Value注释:

package com.example.helper;

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

@Component
public class YellowCar {

    @Value("${car.age}")
    private int age;

    @Override
    public String toString() {
        return "YellowCar [age=" + age + "]";
    }

}

这是程序输出:

Car [age=15]
YellowCar [age=0]

如您所见,YellowCar的年龄未正确注入(等于0)。

我的目标:我不想将环境对象传递给其他类的构造函数...我想使用@Value annotatnio代替。有人可以解释一下:1)为什么我的代码不起作用? 2)如何更新此代码以获得以下输出?

Car [age=15]
YellowCar [age=15]

谢谢!

spring properties annotations autowired application.properties
1个回答
0
投票

发生这种情况,因为您直接实例化YellowCar。为了使事情有效,@Autowire YellowCarHelperApplication内并使用这个注入的实例(未经测试):

@Autowired
private YellowCar yellowCar;

public static void main(String[] args) {
    SpringApplication.run(HelperApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    System.out.println (new RedCar(env));
    System.out.println (yellowCar);
}

说明:

当你使用new创建类时,Spring对这个新实例一无所知,因此,它无法注入任何东西。但是当实例是通过Spring基础结构创建的时候(我在这里尝试不使用很多俚语),它会注入所有字段:当你使用@Component注释标记类或使用@Bean注释创建方法时,你告诉Spring,你希望它是一个Bean。在应用程序启动时,spring会创建此bean的实例 - 默认情况下每个上下文只有一个实例 - 并将此实例注入所有其他bean(如果请求)。在这种情况下,这个实例正在由spring基础结构处理(确切的类就是它 - AutowiredAnnotationBeanPostProcessor),其他bean的实例将被注入到我们的bean中。

UPD:你可以用RedCar做同样的事情:

@Autowired
private YellowCar yellowCar;

@Autowired
private RedCar redCar;

public static void main(String[] args) {
    SpringApplication.run(HelperApplication.class, args);
}

@Override
public void run(String... args) throws Exception {
    System.out.println (redCar);
    System.out.println (yellowCar);
}
© www.soinside.com 2019 - 2024. All rights reserved.