我正在尝试在 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'
}
最佳答案
@yurii 您正在创建新的 MappingProprties 对象 new MappingProperties().getLogin()
,该对象将始终返回 null。您必须从 spring 上下文中获取 MappingProperties 对象。根据您的代码,spring DI 不知道您的 MappingProperites 的 new
对象。
(如果您想使用 new
那么每次您都必须注入(inject) login
字段的值,但我认为这不是一个好方法你 Spring 的工作)
我正在使用您的代码作为引用。
//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);
}
}
没有。使用属性文件的方法。我们将讨论其中两个(您将这些概念相互混合)。让我们说得非常清楚易懂。
第一种方法:
► 这样我们就要把字段值注入(inject)的属性文件名告诉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 @ConfigurationProperties:
Annotation for externalized configuration. Add this to a class definition or a {@code @Bean} method in a {@code @Configuration} class if you want to bind and validate some external Properties (e.g. from a .properties file).
//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();
...
注意:如果您对此有任何疑问,请告诉我
关于java - 无法在 Spring Boot 中获取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722844/