记录一次SpringBoot中@Value注入失败

1. 第一步检测语法是否正确

@Value("${hdfs.name}")
private String hdfs;

2.第二步检测配置文件中是否有进行配置(application.properties)

hdfs.name=jilin

3.第三步检测是否增加了@Component注解

注意:在spring中,使用了spring的注解,那么就需要使用spring来进行管理对象,而不能自己进行new,否则就会导致失败。

@Autowired为自动装配,将对象自动注入到类中使用.
@Autowired注入有两个条件,被注入的类的对象交给了spring管理;同时使用的类的对象也要交给spring管理.两个条件都满足才能注入.

@Component
public class Test {


    @Value("${hdfs.name}")
    private String hdfs;
    private static Properties properties = new Properties();
    static {
        InputStream resourceAsStream =
                Test.class.getResourceAsStream("/common/testFile.properties");
        try {
            properties.load(resourceAsStream);
        }catch (IOException ioe){
            ioe.printStackTrace();
        }finally{

        }
    }

    public String getHdfs() {
        return hdfs;
    }

    public void setHdfs(String hdfs) {
        this.hdfs = hdfs;
    }

}

测试一下是否注入成功,仅为测试,所以直接写在下了启动类下

@SpringBootApplication
@Component
@RestController
public class DemoApplication {

    @Autowired
    private Test test;
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @RequestMapping("/get")
    public void get(){
        System.out.println(test.getHdfs());
    }

}

postman发送请求,看是否调用成功。成功!!!

4.第四步检测代码中的书写方式

不要再无参构造器中,进行new对象的操作。否则就会造成@Value注解失败。(本人就是踩到这步坑)。

  初始化上下文可以使用@PostConstruct注解来进行初始化,他会在spring加载完信息后,进行调用并且只调用一次。

5.@Value无法注入static属性

注意:使用@Value直接放在static的属性上是无法注入内容的!!!此方式会一直是null.如下图

发现@value不能直接注入值给静态属性,spring 不允许/不支持把值注入到静态变量中;

spring支持set方法注入,我们可以利用非静态setter 方法注入静态变量,并且使用@Value的类必须交个spring进行管理.就如同@Autowired有时无法注入一样.

改进

使用setter方法进行属性的赋值,并且setter方法不能有static

idea自动生成的方法会有static,需要手动删除.

@Component
public class Test {

    private static String hdfs;
    private static Properties properties = new Properties();
    static {
        InputStream resourceAsStream =
                Test.class.getResourceAsStream("/common/testFile.properties");
        try {
            properties.load(resourceAsStream);
        }catch (IOException ioe){
            ioe.printStackTrace();
        }finally{

        }
    }

    public String getHdfs() {
        return hdfs;
    }

    @Value("${hdfs.name}")
    public void setHdfs(String hdfs) {
        this.hdfs = hdfs;
    }

}

重新测试,结果无误!!!!


另外一种读取配置文件方式:使用@PropertySource注解

如果不是application.properties,而是其他的properties的配置文件,采用PropertySource注解

classpath:后可以跟路径 如@PropertySource("classpath:common/testFile.properties") 表示 resource目录下common文件夹下有个testFile.properties文件

@Component
@PropertySource("classpath:zookeeper.properties")
public class ZookeeperConfig {

    //zk connect config
    @Value("${zookeeper.quorum:39.100.43.16:2181}")
    private String serverList;

    @Value("${zookeeper.retry.base.sleep:100}")
    private int baseSleepTimeMs;

    @Value("${zookeeper.retry.max.sleep:30000}")
    private int maxSleepMs;

    @Value("${zookeeper.retry.maxtime:10}")
    private int maxRetries;

    @Value("${zookeeper.session.timeout:60000}")
    private int sessionTimeoutMs;

    @Value("${zookeeper.connection.timeout:30000}")
    private int connectionTimeoutMs;

    @Value("${zookeeper.connection.digest: }")
    private String digest;

    @Value("${zookeeper.dolphinscheduler.root:/dolphinscheduler}")
    private String dsRoot;

    @Value("${zookeeper.max.wait.time:10000}")
    private int maxWaitTime;
}

转载:https://www.cnblogs.com/nhdlb/p/11741228.html

原文地址:https://www.cnblogs.com/erlou96/p/13718122.html