Springboot中的@Value返回null [英] @Value in Springboot returns null

查看:72
本文介绍了Springboot中的@Value返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有位于资源中的application.properties

I have the application.properties, which is located in the resources

apllication.properties

hsm.provider=software
hsm.name=TestHsm
hsm.port=3001
hsm.ip=127.0.0.1
hsm.timeout=10000

控制器

@RestController
@RequestMapping("/hsm")
public class Controller {

  @Value("${hsm.ip}")
  private String ip;

  @Value("${hsm.port}")
  private String port;

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

  @Value("${hsm.timeout}")
  private String timeout;

  @Value("${hsm.provider}")
  private String provider;}
}

但是,当我运行应用程序时,所有变量都为NULL.我想念什么?

however when i run application, all variables remain NULL. What am i missing?

编辑这是从src文件夹开始的项目结构

EDIT This is the projectstructure as of the src folder

src
├───main
│   ├───java
│   │   └───com
│   │       └───xyz
│   │           └───hsmservice
│   │               └───hsm
│   │                   └───api
│   │                           Application.java
│   │                           Controller.java
│   │                           HSM.java
│   │
│   └───resources
│       │   application.properties
│       │
│       └───META-INF
│               plugin.xml
│
└───test
    ├───java
    │       LibraryTest.java
    │
    └───resources

编辑2 这是应用程序类

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

推荐答案

好吧,我用

Okay, i solved it with the top answer of this Question. I put the variables and @Values in the signature of the constructor and not as class variables.

这篇关于Springboot中的@Value返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆