我正在尝试在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。您必须从春季上下文中获取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] 删除。
我来说两句