How to insert a single property value into a string using spring 2.5.x?

I would really like to annotate a method with a reference to one property in the properties file for injection.

@Resource("${my.service.url}") private String myServiceUrl; 

Of course, this syntax does not work;) That's why I ask here.

I know that I can insert the full properties file, but it just seems excessive, I don't want the properties file - I need a configured value.

Edit: I can only see examples of PropertyPlaceholderConfigurer, where XML is used to bind properties to this field. I still can’t understand how this can be achieved using annotations?

+6
java spring
source share
5 answers

There is a thread about this on the Spring forum. The short answer is that there really is no way to insert one property using annotations.

I have heard that support for using annotations will be improved in Spring 3.0, so this is likely to be reviewed soon.

+4
source share

I know what has passed since the publication, but I managed to stumble upon a solution to this problem for spring 2.5.x

You can create instances of "String" beans in the spring xml configuration, which can then be added to annotated components

 @Component public class SomeCompent{ @Autowired(required=true @Resource("someStringBeanId") private String aProperty; ... } <beans ....> <context:component-scan base-package="..."/> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> ... </bean> <bean id="someStringId" class="java.lang.String" factory-method="valueOf"> <constructor-arg value="${place-holder}"/> </bean> </beans> 
+7
source share

I created a project that solves this problem for Spring 2.5. *:

http://code.google.com/p/spring-property-annotations/

For Spring 3, you can use the @Value annotation ("$ {propery.key)".

+5
source share

you can do this if you use the XML configuration. Just configure the PropertyPlaceholderConfigurer and specify the value of the property in the configuration

 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:com/foo/jdbc.properties</value> </property> </bean> <bean ...> <property name="myServiceUrl" value="${my.service.url}"/> </bean> 
0
source share

You can try to enter the value of the property "my.service.url" into the bean file.

Take a look at: http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-placeholderconfigurer

NTN.

-2
source share

All Articles