Spring Boot @Value属性

问题描述 投票:26回答:7

我有一个Spring Boot应用程序,在其中一个类中,我尝试使用@Value从application.properties文件中引用一个属性。但是,该财产没有得到解决。我查看了类似的帖子,并尝试按照建议,但这没有帮助。这堂课是:

@Configuration
@ComponentScan
@EnableAutoConfiguration
public class PrintProperty {

  @Value("${file.directory}")
  private String fileDirectory;

  public void print() {
    System.out.println(fileDirectory);
  }
}

我在application.properties中有属性file.directory。我也有其他领域。

spring spring-boot spring-annotations
7个回答
17
投票

我和你一样有同样的问题。这是我的错误代码。

@Component
public class GetExprsAndEnvId {
    @Value("hello")
    private String Mysecret;


    public GetExprsAndEnvId() {
        System.out.println("construct");
    }


    public void print(){
        System.out.println(this.Mysecret);
    }


    public String getMysecret() {
        return Mysecret;
    }

    public void setMysecret(String mysecret) {
        Mysecret = mysecret;
    }
}

这不是问题,但我们需要像这样使用它:

@Autowired
private GetExprsAndEnvId getExprsAndEnvId;

不是这样的:

getExprsAndEnvId = new GetExprsAndEnvId();

12
投票

确保您的application.properties文件位于src / main / resources / application.properties下。是一种方法。然后按如下方式添加@PostConstruct

示例Application.properties

file.directory = somePlaceOverHere

示例Java类

@ComponentScan
public class PrintProperty {

  @Value("${file.directory}")
  private String fileDirectory;

  @PostConstruct
  public void print() {
    System.out.println(fileDirectory);
  }
}

上面的代码将打印出“somePlaceOverhere”


6
投票

我想提一下,我使用了春季启动版本1.4.0,因为这个版本你只能写:

@Component
public class MongoConnection {

@Value("${spring.data.mongodb.host}")
private String mongoHost;

@Value("${spring.data.mongodb.port}")
private int mongoPort;

@Value("${spring.data.mongodb.database}")
private String mongoDB;
}

然后随时注入课程。


2
投票

要从application.properties中读取值,我们需要使用@SpringBootApplication和使用@Component读取的类或其中的各种类来注释我们的主类。下面是我从application.properties读取值的示例,它在调用Web服务时工作正常。如果您按原样部署相同的代码并尝试从http://localhost:8080/hello访问,您将获得存储在application.properties中的关键消息的值。

package com.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
public class DemoApplication {

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

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

    @RequestMapping("/hello")
    String home() {
        return message;
    }

}

试试让我知道


1
投票

我有类似的问题,上面的例子并没有帮助我阅读属性。我已经发布了完整的类,它将帮助您从SpringBoot应用程序中的application.properties文件中读取属性值,如下所示。

Spring Boot - Environment @Autowired throws NullPointerException


0
投票

你没有在OP中包含包声明,但有可能@SpringBootApplication@ComponentScan都没有扫描你的@Component

@ComponentScan Javadoc说:

可以指定basePackageClassesbasePackages(或其别名value)来定义要扫描的特定包。如果未定义特定包,则将从声明此批注的类的包进行扫描。

ISTR之前浪费了大量时间,发现将应用程序类移动到应用程序包树中的最高包是最简单的。

最近我遇到了一个问题,即在插入值之前正在读取属性。 Jesse的答案很有帮助,因为@PostConstruct似乎是最早可以阅读插入值的人,当然你应该让Spring称之为。


0
投票

您的问题是您的配置需要static PropertySourcesPlaceholderConfigurer Bean定义。我强调静态,因为我有一个非静态的,它不起作用。

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
© www.soinside.com 2019 - 2024. All rights reserved.