Enlightensoft's Blog

Helping in your each step

  • Categories

  • Authors

Posts Tagged ‘value in’

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

Posted in Spring | Tagged: , , , , , , | Leave a Comment »