Results 1 to 2 of 2

Thread: autowiring properties values

  1. #1
    Join Date
    Jun 2008
    Location
    Jacksonville, Florida
    Posts
    147

    Default autowiring properties values

    I have a bean that has a getter/setter for a String attribute called serverBase.

    I have a properties file that is configured in my spring context. I would like to autowire the serverBase on my bean with the value from my properties file called "server_base". how do I do it?
    Here are my 3 points of interest:
    propreties file (env.properties):
    Code:
    server_base=http://192.168.100.52
    Here is my spring context:
    Code:
    <beans xmlns="http://www.springframework.org/schema/beans" 
    	xmlns:aop="http://www.springframework.org/schema/aop" 
    	xmlns:context="http://www.springframework.org/schema/context" 
    	xmlns:jee="http://www.springframework.org/schema/jee" 
    	xmlns:tx="http://www.springframework.org/schema/tx" 
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    	xsi:schemaLocation="http://www.springframework.org/schema/aop 
    		http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
    		http://www.springframework.org/schema/beans 
    		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    		http://www.springframework.org/schema/context 
    		http://www.springframework.org/schema/context/spring-context-3.0.xsd   
    		http://www.springframework.org/schema/jee 
    		http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
    		http://www.springframework.org/schema/tx 
    		http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
    
       <context:property-placeholder location="classpath*:META-INF/spring/*.properties,classpath:email.properties,classpath:environment.properties"/>
         <context:spring-configured/>
         <context:component-scan base-package="net.itxlabs.sanddollar">
            <context:exclude-filter expression=".*_Roo_.*" type="regex"/>
            <context:exclude-filter expression="org.springframework.stereotype.Controller" type="annotation"/>
        </context:component-scan>
    </beans>
    And here is my class that I need the property injected into:

    Code:
    @Configurable
    public class MyRegistryBean{
    	
    	@Autowired
    	@Qualifier("properties.server_base")
    	private String serverBase;
    
       public String getServerBase()
       {
       return this.serverBase;
       }
    
       public void setServerBase(String serverBase)
       {
           this.serverBase=serverBase;
       }
    }

  2. #2
    Join Date
    Sep 2009
    Location
    Vilnius, Lithuania
    Posts
    118

    Default

    @Autowired should be used on setters, not getters. But it is not needed for wiring simple values like in your case.

    @Qualifier is for specifying the sub-type of dependency that you are willing to inject. But it should only be used with @Autowired.

    <contextroperty-placeholder/> is for replacing properties in bean definitions using ${placeholders.like.this}.

    So, what you need is to expose your env.properties file as a Spring bean:

    Code:
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:util="http://www.springframework.org/schema/util">
    
        <util:properties id="envProperties" location="classpath:env.properties"/>
    
    </beans>
    and then wire it to your bean property using @Value annotation and Expression Language:

    Code:
    @Value("#{envProperties['server_base']}")
    public void setServerBase(String serverBase) {
        this.serverBase=serverBase;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •