Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Read property value in Java file using sprng 3 annotation

Posted by Pankil Patel on May 6, 2013

Step 1: Add your property bean definition in applicationContext.xml:

<bean id=“myPropValue”     class=“org.springframework.beans.factory.config.PropertiesFactoryBean”>
               <property name=“location”value=“classpath:myPropertyFile.properties” />
</bean>
 

Step 2: Read Property by constructor injection of property bean into Spring service.:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 

@Service(“myService”)

public class MyServiceImpl implements MyService {

private Properties allValueProperties;

       @Autowired
       public MyServiceImpl (Properties myPropValue) {
              this.allValueProperties = myPropValue;
       }
       @Override
       public void springWebMethod() throws Exception {
                   String valueFromProperty = this.allValueProperties.getProperty(“enlighten.my.default.value.url”);
       }

}

Note: If you don’t want to inject property bean in to your each spring service bean where it is required, alternatively you can inject it into one common bean and create static map from it or create enum from that properties and use that static map or enum in your any class of your application.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: