【问题标题】:Spring does not load propertiesSpring不加载属性
【发布时间】:2018-03-15 16:28:32
【问题描述】:

目前我正在学习使用 Spring,但我在添加一些属性方面已经失败了。

一开始我尽量保持简单。 我有一个 Maven 项目并在资源文件夹中创建了一个 config.properties。

test.abc = def

然后我用以下代码创建了一个控制器类:

@PropertySource("classpath:config.properties")
public class Controller {

    @Value("${test.abc}")
    private String abc;

    public Controller() {
        System.out.println(abc);
    }
}

我的主课是这样的:

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BookingdisplayApplication {
    public static void main(String[] args) {
        new Controller();
    }
}

现在,当我通过 Intellij 或 mvnw.cmd spring-boot:run 运行我的代码时,我在输出中得到了 null。我做错了什么?

【问题讨论】:

  • 您使用构造函数 new 创建控制器,它不受 Spring 管理。请阅读 Spring Beans 的文档。

标签: spring maven intellij-idea properties


【解决方案1】:

试试这个代码:

@Configuration
@PropertySource("classpath:config.properties")
public class AppConfig {

    @Value("${test.abc}")
    private String abc;

  //Used in addition of @PropertySource
  @Bean
  public static PropertySourcesPlaceholderConfigurer   propertySourcesPlaceholderConfigurer() {
      return new PropertySourcesPlaceholderConfigurer();
  }

}

【讨论】:

  • 我添加了缺失的部分,但效果不佳
【解决方案2】:

请理解,要解析@Values 中的${},您必须在XML 或注解配置文件中注册一个静态PropertySourcesPlaceholderConfigurer。

@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigInDev() {
    return new PropertySourcesPlaceholderConfigurer();
}

或,

@Autowired
private org.springframework.core.env.Environment env;

//define in your class or method
String abc = env.getProperty("test.abc");

【讨论】:

  • 我尝试了第一件事,它不起作用。我也尝试了第二件事,但 env 返回 null
  • 用@Configuration注解你的控制器类
  • 我也试过了,根据你的建议,还是不行
  • 是的,因为您的控制器类不是由 Spring 实例化的,所以您使用 'new' 关键字来创建控制器类的对象。只有当 Spring 容器负责 bean 初始化时,自动装配才会起作用。上述两种解决方案只有在 Spring 起作用时才有效。
【解决方案3】:

你的控制器的生命周期是由 Spring 管理的吗?您可能需要在类中添加注解@Controller 或定义该类的bean。

编辑: 我认为更好的方法是使用构造函数注入:

Controller.java:

public class Controller {

    private String abc;

    public Controller(String abc) {
        this.abc = abc;
        System.out.println(abc);
    }
}

BookingdisplayApplication.java:

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class BookingdisplayApplication {

    // instantiating bean via Spring:
    @Bean
    public Controller controller(@Value("${test.abc}") String testAbc) {
        return new Controller(testAbc);
    }

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

你也不需要使用@PropertySource 注解——如果你使用属性文件

[src-root]/application.properties 

(在源码根目录下),它会被Spring自动找到。

【讨论】:

  • 好吧,我对 Spring 还很陌生。我将我的主要课程添加到该主题中。你是这个意思吗?我没有更多的
  • 不,你只是在你的Controller类中添加了'@Controller'注解,这样可以确保Spring会创建一个新的实例,或者你定义带有'@Bean'注解的getter,见docs.spring.io/spring/docs/current/spring-framework-reference/…
  • 我在控制器类的顶部添加了@Controller,但它仍然为空
猜你喜欢
  • 1970-01-01
  • 2014-12-21
  • 1970-01-01
  • 1970-01-01
  • 2019-10-18
  • 1970-01-01
  • 2019-08-09
  • 2011-02-16
  • 2014-04-08
相关资源
最近更新 更多