Oct 11, 2015

Spring PropertyPlaceholderConfigurer Example

The Maven project structure is shown below 






1. Add Spring dependency

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
 xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;
 <modelVersion>4.0.0</modelVersion>
 <groupId>SpringPlaceholderDemo</groupId>
 <artifactId>SpringPlaceholderDemo</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <properties>
  <spring.version>4.0.0.RELEASE</spring.version>
<!--   <mysql.version>5.1.27</mysql.version>
  <hibernate-entitymanager.version>4.2.8.Final</hibernate-entitymanager.version> -->
 </properties>
 <dependencies>
  <dependency>
   <groupId>commons-logging</groupId>
   <artifactId>commons-logging</artifactId>
   <version>1.2</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.data</groupId>
   <artifactId>spring-data-jpa</artifactId>
   <version>1.8.0.RELEASE</version>
  </dependency>
  <dependency>
   <groupId>org.springframework.security</groupId>
   <artifactId>spring-security-core</artifactId>
   <version>${spring.version}</version>
   <exclusions>
    <exclusion>
     <groupId>org.springframework</groupId>
     <artifactId>spring-asm</artifactId>
    </exclusion>
   </exclusions>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context-support</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-expression</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-context</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-core</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-orm</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-aop</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-tx</artifactId>
   <version>${spring.version}</version>
  </dependency>
<!--   <dependency>
   <groupId>org.hibernate</groupId>
   <artifactId>hibernate-entitymanager</artifactId>
   <version>${hibernate-entitymanager.version}</version>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>${mysql.version}</version>
  </dependency> -->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-beans</artifactId>
   <version>${spring.version}</version>
  </dependency>
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.0</version>
  </dependency>

 </dependencies>

</project>
2. Create a EmployeeService interface

 package com.nadhu.spring.ser.api;

 public interface EmployeeService {

             public String getEmpWebServiceEndpointURL();

}

3. Create a EmployeeServiceImpl class

package com.nadhu.spring.ser.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.nadhu.spring.java.invoker.EmpInfoWebServiceInvoker;
import com.nadhu.spring.ser.api.EmployeeService;
/**
 * Created by Nadhamuni Kothapalle
 */
@Component
public class EmployeeServiceImpl implements EmployeeService{
 @Autowired
 private EmpInfoWebServiceInvoker empInfoWebServiceInvoker;
 @Override
 public String getEmpWebServiceEndpointURL() {
  return empInfoWebServiceInvoker.getEmpInfoServiceLocation();
 }
}

4. Create a EmpInfoWebServiceInvoker class

package com.nadhu.spring.java.invoker;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
@Component
public class EmpInfoWebServiceInvoker {
 private static String empInfoServiceLocation;
 public String getEmpInfoServiceLocation() {
  if (StringUtils.isNotBlank(empInfoServiceLocation)
    || StringUtils.isNotEmpty(empInfoServiceLocation)) {
   return empInfoServiceLocation;
  } else {
   return "Value is Not Correct";
  }
 }
 public static void setEmpInfoServiceLocation(String empInfoServiceLocation) {
  EmpInfoWebServiceInvoker.empInfoServiceLocation = empInfoServiceLocation;
 }
}
5. externalConfiguration.properties

empInfoServiceURL=http://www.abcdef.net/EmpInfoServicer.asmx?WSDL

6. Define the PropertyPlaceholderConfigurer

spring-config.xml

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
 xmlns:jpa="http://www.springframework.org/schema/data/jpa"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/data/jpa
 http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd" >
 <!-- For consider the using of annotations foe defining Spring Bean -->
 <context:annotation-config />
 <!-- For defining Spring Bean -->
 <context:component-scan
  base-package="com.nadhu.spring.ser.impl, com.nadhu.spring.java.invoker" />
 <bean id="propertyPlaceholderConfiguration"
  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations">
   <list>
    <value>file:///D:/configurationLocation/externalConfiguration.properties </value>
    <value>file:///D:/secureConfigurationLocation/secureExternalConfiguration.properties </value>
   </list>
  </property>
 </bean>
 <bean id="empInfoServiceLocation" class="com.nadhu.spring.java.invoker.EmpInfoWebServiceInvoker">
  <property name="empInfoServiceLocation" value="${empInfoServiceURL}" />
 </bean>
</beans>
7. Run the application (PlaceholderTest.java)

package com.nadhu.spring.java.exe;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.nadhu.spring.ser.api.EmployeeService;
/**
 * Created by Nadhamuni Kothapalle
 */
public class PlaceholderTest {
   

 private AbstractApplicationContext context;
 public static void main(String[] args) {

  PlaceholderTest placeHolderTest = new PlaceholderTest();
  System.out.println("Employee Service End Point URL: "+placeHolderTest.serviceObject().getEmpWebServiceEndpointURL());
 }
 private EmployeeService serviceObject() {
  context = new ClassPathXmlApplicationContext("spring-config.xml");
  //return setEmployeeService((EmployeeService) context.getBean("employeeService"));
  return (EmployeeService)context.getBean("employeeServiceImpl");

 }
}

8. Output

When we execute the application we get the values read from the properties file.

Employee Service End Point URL:http://www.abcdef.net/EmpInfoServicer.asmx?WSDL

 
 Thank you !

 

No comments: