Java 使用 Spring 和注解 @Value 注入属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6425795/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:48:54  来源:igfitidea点击:

Injecting Properties using Spring & annotation @Value

javaspringdependency-injection

提问by haju

I am trying to load a properties file into a Spring bean and then inject that bean into a class.

我正在尝试将属性文件加载到 Spring bean 中,然后将该 bean 注入到一个类中。

The only part I can't get to work seems to be using the @Resourcereference.Can someone connect the last piece for me? I get a null value every time. Doesn't seem to want to inject the value.

我无法开始工作的唯一部分似乎是使用@Resource引用。有人可以为我连接最后一部分吗?我每次都得到一个空值。似乎不想注入值。

[EDIT] - I originally thought using the @Resourcewas the best way but the proposed solution I found easier.

[编辑] - 我最初认为使用@Resource是最好的方法,但我发现建议的解决方案更容易。

I saw this solution in another post:

我在另一篇文章中看到了这个解决方案:

Reference Solution Link:Inject Property Value into Spring - posted by DON

参考解决方案链接:将属性值注入 Spring - 由 DON 发布

Credit to Don for the post but I just wasn't sure how to finish it with the @Resource.

感谢 Don 的帖子,但我不确定如何用@Resource完成它。

Debugging Results:The variable value appPropertiesis always null. It's not being injected.

调试结果:变量值appProperties始终为空。不是注射。

Spring Config.enter image description here

弹簧配置。enter image description here

Sample Class:

示例类:

package test;

import java.util.Properties;
import javax.annotation.Resource;


public class foo {
    public foo() {}
    @Resource private java.util.Properties appProperties;
}

Based on the advice in the approved solution below. Here are the changes I made.

基于以下已批准解决方案中的建议。这是我所做的更改。



Solution Update:

解决方案更新:

Spring Config: enter image description here

弹簧配置: enter image description here

Java Class: enter image description here

Java类: enter image description here

采纳答案by abalogh

For your solution to work you would also need to make foo a Spring managed bean; because otherwise how would Spring know that it has to deal with any of your annotations on your class?

为了使您的解决方案起作用,您还需要使 foo 成为 Spring 管理的 bean;因为否则 Spring 怎么会知道它必须处理您类上的任何注释?

  • You can either specify it in your appcontext xml as a bean with ..class="foo"
  • Or use component-scanand specify a base package which contains your fooclass.
  • 您可以在 appcontext xml 中将其指定为 bean ..class="foo"
  • 或者使用component-scan并指定包含您的foo类的基本包。

Since I'm not entirely sure this is exactly what you want (don't you want a .properties fileto be parsed by Spring and have it's key-value pairs available instead of a Propertiesobject?), I'm suggesting you another solution: Using the utilnamespace

由于我不完全确定这正是您想要的(您不希望Spring 解析.properties文件并使用它的键值对而不是Properties对象吗?),我建议您使用另一种解决方案: 使用util命名空间

<util:properties id="props" location="classpath:com/foo/bar/props.properties"/>

And reference the values inside your beans (also, have to be Spring managed):

并引用 bean 中的值(也必须由 Spring 管理):

@Value("#{props.foo}")
public void setFoo(String foo) {
    this.foo = foo;
}

EDIT:

编辑:

just realized that you are importing org.springframework.context.ApplicationContextin your class which is probably unnecessary. I strongly encourage you to read Spring referenceat least the first few chapters, because a) it's a great read b) you will find it much easier to understand Spring if the basics are clear.

刚刚意识到你正在org.springframework.context.ApplicationContext你的班级中导入这可能是不必要的。我强烈建议您至少阅读前几章的Spring 参考,因为 a) 这是一本很好的读物 b) 如果基础知识清晰,您会发现理解 Spring 会容易得多。

回答by alexey28

Just one more solution using properties placeholder.

使用属性占位符的另一种解决方案。

The spring context:

春天的背景:

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <context:component-scan base-package="your.packege" />

    <context:property-placeholder location="classpath*:*.properties"/>

</beans>

The java class where you want inject properties values:

您要在其中注入属性值的 java 类:

public class ClassWithInjectedProperty {

    @Value("${props.foo}")
    private String foo;
}