Spring Basics

1) Loading properties from a properties file in spring application
In Spring we have two ways of managing our application beans (a) BeanFactory and (b) ApplicationContext. BeanFactory provides basic functionality regarding bean management services whereas ApplicationContext provides more functionalities along with features provided by BeanFactory.

To read a property from (.properties) file we should use ApplicationContext, we can not use BeanFactory because it does not have access to applcation resources. In this article we'll see how we can access properties from a property file:

(a) spring configuration file - appliaction-context.xml

xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"

xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<bean id="propertyHolder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" scope="singleton" lazy-init="false">

<property name="locations">

<value>

classpath:data.properties

value>

property>

bean>

<bean id="sayHello" class="com.surya.mybeans.HelloSpringBean" depends-on="propertyHolder">

<property name="userName" value="${user.name}" />

bean>

beans>



(b) Bean Class in which we need to inject property value retrieved property file-HelloSpringBean.java

xml version="1.0" encoding="UTF-8"?>

package com.surya.mybeans;

public class HelloSpringBean {

private String userName;

public void sayHello() {

System.out.print("I am " + userName+ " ! Saying Hello to Spring world");

}

public void setUserName(String userName) {

this.userName = userName;

}

}


(c) Application context initializer, bean accessor

package com.surya.application.init;

import com.surya.mybeans.HelloSpringBean;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationInitlizer {

public static void main(String...strings ){

try{

ApplicationContext xmlBf=new ClassPathXmlApplicationContext("application-context.xml");

((HelloSpringBean)xmlBf.getBean("sayHello")).sayHello();

}catch(Exception ex ){

ex.printStackTrace();

}

}

}


I hope this is will help you quickly use this springs straight forward feature without searching any more help.

Thanks
-- Surya

Comments

Popular posts from this blog

AEM as a Cloud Service (AEMaaCS) – Architecture Overview

AEM 6.3 - Bundle Whitelisting - Deprecation of administrative authentication

AEM - Query list of components and templates

Custom synchronisation or rollout action for blueprint ?