【问题标题】:Unable to get properties in Spring Boot无法在 Spring Boot 中获取属性
【发布时间】:2018-01-25 03:28:32
【问题描述】:

我正在尝试在 SpringBoot Appilcation 中使用属性。但是当我试图打电话给new MappingProperties().getLogin() 时,我总是得到一个空值。拜托,谁能解释一下我做错了什么?

应用类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.support.SpringBootServletInitializer;

@EnableConfigurationProperties
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

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

这是我尝试访问属性的方式

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
@PropertySource("classpath:mapping.properties")
@ConfigurationProperties(prefix = "user")
public class MappingProperties {
    private String login;

    public String getLogin(){
        return login;
    }

    public void setLogin(String login){
        this.login = login;
    }
}

这里是我的mapping.properties 文件,它位于src\main\resources\mapping.properties

user.login = /login

还有我的build.gradle

buildscript {
    repositories {
        maven { url 'http://repo.spring.io/plugins-release' }
    }
    dependencies {
        classpath 'io.spring.gradle:propdeps-plugin:0.0.9.RELEASE'
    }
}

plugins {
    id 'org.springframework.boot' version '1.5.4.RELEASE'
}

configure(allprojects) {
    apply plugin: 'propdeps'
    apply plugin: 'propdeps-maven'
    apply plugin: 'propdeps-idea'
    apply plugin: 'propdeps-eclipse'
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-actuator:1.5.4.RELEASE'
    compile 'org.springframework.boot:spring-boot-devtools:1.5.4.RELEASE'

    optional "org.springframework.boot:spring-boot-configuration-processor"

    testCompile 'org.springframework.boot:spring-boot-starter-test:1.5.4.RELEASE'
}

【问题讨论】:

  • 尝试将@Configuration 添加到MappingProperties.java 对象...加上@Vitolds 建议的注释
  • @georgesvan 你为什么要这样做?如果它是一个配置文件,那么注释它...... MappingProperties 似乎是
  • @Component@Configuration 都没有帮助
  • 啊,你在Application类上也缺少@EnableAutoConfiguration
  • 不是包含在@SpringBootApplication中吗?

标签: java spring spring-boot properties


【解决方案1】:

@yurii 您正在创建 MappingProprties new MappingProperties().getLogin() 的新对象,该对象将始终返回 null。您必须从 spring 上下文中获取 MappingProperties 的对象。根据您的代码,spring DI 不知道您的 new MappingProperites 对象。

(如果你想使用new,那么每次你都必须注入login 字段的值,但我认为这不是春季工作的好方法) 我正在使用您的代码作为参考。

//simple and basic config for any spring boot application
@SpringBootApplication 
@EnableAutoConfiguration 
public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

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

没有。使用属性文件的方法。我们将讨论其中的两个(您将这些概念相互混合)。让我们说得非常清楚。

第一种方式:

► 这样我们就得把字段值注入的properties文件名告诉spring

► 创建 anyname.properties 文件 anyname.properties

user.login:/login

► 创建新的配置 java 文件 PropConfig.java

@Configuration
@PropertySource(value="classpath:anyname.properties")
public class PropConfig{

//you can inject property values anywhere in the code that is under spring context(e.g. @Service, @Repository, @Component etc)
@Value("${user.login}")
private String login;

public String getUserLogin(){
    return login;
}
}

第二种方式:

► 我们将使用注解@ConfigurationProperties

► 创建 NewProps.java 文件

@ConfigurationProperties(prefix = "user")
public class NewProps{
//value of this field will be automatically mapped to "user.login" in "anyName.properties" file
private String login;
 public String getUserLogin(){
    return login;
 }
}

► 根据documentation of @ConfigurationProperties

外部化配置的注释。将此添加到课程中 定义或 如果要绑定和验证,请在 {@code @Configuration} 类中使用 {@code @Bean} 方法 一些外部属性(例如,来自 .properties 文件)。

//here we are making property values as fields of custom bean
//now property values will be accessed in a way, as field of some ordinary java bean
@Configuration
public class NewConfig {

 @Bean
 NewProps newProps(){
    return new NewProps();
 }
}

► 访问属性值

@Autowired
NewProps newProps;


//in your code
...
newProps.getUserLogin();
...

注意:如果您对此有任何疑问,请告诉我

【讨论】:

  • 如果我有 2 个属性文件怎么办。它是否应该从两个文件中获取属性。它不适合我。有什么原因吗?
  • 你用过@PropertySource(value="classpath:anyname2.properties")
【解决方案2】:

您必须通过在 Spring 容器中注入或连接它们来管理 2 个类之间的依赖关系。

因此,通过自动装配将 MappingProperties 作为实例变量注入到调用类中

@Autowired
private MappingProperties mappingProperties; 

...
mappingProperties.getLogin();
...

【讨论】:

    【解决方案3】:

    Spring 不知道您的登录字段,我的意思是它不知道必须在那里注入一些东西。你应该告诉 spring 注入值。

    尝试使用@Value("${login}") 注释private String login;

    或者去掉@ConfigurationProperties(prefix = "user"),用@Value("${user.login}")注解

    【讨论】:

    • @Value 不支持宽松绑定
    • @georgesvan 你能详细说明一下吗?答案的语法对我来说似乎有效
    • 感谢您的回答,但没有任何帮助。
    • 另外,如果是@Value,它不是必须看起来像@Value("${login}")吗?
    • @yurii 你是对的。那将是正确的语法
    猜你喜欢
    • 1970-01-01
    • 2019-03-15
    • 2015-09-22
    • 2020-02-14
    • 1970-01-01
    • 2020-02-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多