无法在Spring Boot中获取属性

尤里

我正在尝试在SpringBoot应用程序中使用属性。但是当我尝试打电话时,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'
}
Harpreet Sandhu-TheRootCoder

@yurii您正在创建新的MappingProprties对象,new MappingProperties().getLogin()对象将始终返回null。您必须从春季上下文中获取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);
 }
}

没有 属性文件的方法。我们将讨论其中两个(您正在将这些概念相互混合)。让我们非常清楚地理解。

第一种方式:

►这样,我们必须告诉属性文件的名称,以便注入字段值

►创建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;
 }
}

►作为每文档@ConfigurationProperties

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

//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();
...

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Spring Boot在Gradle中获取包的属性

Spring Boot Config Client无法从Config Server获取属性

在 spring boot 中从 application.properties 获取属性

如何在Spring Boot中从系统属性获取值

如何在Spring Boot中获取文件属性的内容

如何从spring boot jpa查询中获取特定属性

无法从JUnit的Spring Boot中读取应用程序属性?

无法找到属性的设置方法:Spring Boot中的错误

Spring Boot @ConfigurationProperties无法从环境中检索属性

我无法在 Spring Boot 中获取实体 ID

Spring MVC无法获取会话中设置的属性

Spring Boot Actuator:无法绑定下的属性

如何在Spring Boot中实现BeanDefinitionRegistryPostProcessor的Configuration类中获取属性文件

@Preauthorize中的Spring Boot属性

从spring-boot环境获取属性映射

获取对象的属性值Spring Boot Java

Spring Boot AOP无法获取注释参数

云客户端配置应用程序无法从Config Server Spring Boot获取属性

Spring Boot 2.0.5无法绑定spring-cloud-config服务器中的属性列表

Spring Boot:无法配置

Spring无法解析属性

Spring Boot自动配置无法加载Spring Kafka属性

Spring无法获取映射

Spring Boot中属性文件中的变量

如何使用服务中的组件从 Spring Boot 中的 application.properties 获取值以加密实体中的属性

无法在Spring Boot 1.4中扩展WebMvcAutoConfigurationAdapter

Spring boot 无法在Tomcat实例中启动

在Spring Boot中无法使用@Value注释

无法在Spring Boot 2.3.3中构建映像