开发者

Injecting Properties using Spring & annotation @Value

开发者 https://www.devze.com 2023-03-14 04:53 出处:网络
I am trying to load a properties file into a Spring bean and then inject that bean into a cl开发者_运维技巧ass.

I am trying to load a properties file into a Spring bean and then inject that bean into a cl开发者_运维技巧ass.

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.

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

I saw this solution in another post:

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

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

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

Spring Config.

Injecting Properties using Spring & annotation @Value

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:

Injecting Properties using Spring & annotation @Value

Java Class:

Injecting Properties using Spring & annotation @Value


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?

  • 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.

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"/>

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

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

EDIT:

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.


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:

public class ClassWithInjectedProperty {

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

精彩评论

暂无评论...
验证码 换一张
取 消