使用Spring&注释@Value [英] Injecting Properties using Spring & annotation @Value

查看:103
本文介绍了使用Spring&注释@Value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我无法上班的唯一部分要使用 @Resource 引用。有人可以为我连接最后一块?我每次都得到一个空值。似乎不想注入这个价值。

The only part I can't get to work seems to be using the @Resource reference.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 是最好的方法,但是提出的解决方案我发现更容易。

- I originally thought using the @Resource was the best way but the proposed solution I found easier.

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

I saw this solution in another post:

参考解决方案链接 a href =http://stackoverflow.com/questions/317687/inject-property-value-into-spring-bean>将属性值注入Spring - 由DON发布

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

信任唐的帖子,但我不知道如何使用 @Resource 完成它。

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

调试结果
变量值 appProperties 始终为空。它不会被注入。

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

Spring Config。

样本类:

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.

Spring Config:

Spring Config:

Java类:

Java Class:

推荐答案

为了让您的解决方案工作,您还需要使foo成为一个Spring托管bean;因为否则Spring会知道它必须处理你课上的任何注释?

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?


  • 你可以在你的appcontext中指定它xml as a bean with .. class =foo

  • 或使用 component-scan 并指定一个包含您的 foo 类的基本包。

  • You can either specify it in your appcontext xml as a bean with ..class="foo"
  • Or use component-scan and specify a base package which contains your foo class.

由于我不完全确定这是你想要的(不要你想要一个.properties 文件由Spring解析,并且它的键值对可用,而不是一个属性 object?),我建议你另一个解决方案:使用 util 命名空间

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

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

并引用你的bean内的值(也必须是Spring管理的):

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

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

编辑:

org.springframework.context.ApplicationContext 可能是不必要的。我强烈建议您至少阅读 Spring参考前几章,因为a)这是一个很好的阅读b)如果基础知识清楚,你会发现更容易理解Spring。

just realized that you are importing org.springframework.context.ApplicationContext in your class which is probably unnecessary. I strongly encourage you to read Spring reference at 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.

这篇关于使用Spring&amp;注释@Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆