Home  >  Spring Core

Example of util:properties in Spring

By Arvind Rai, April 05, 2013
util:properties in Spring reads configuration file from a location. And id is defined by util:properties and that id can be used to read property from configuration file. In the below example we have a configuration file in the classpath. And I will read it with help of @Value.

app-conf.xml
<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"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
     
    <util:properties id="entProp" location="classpath:/entitlement.cfg" />
    
    <util:properties id="subProp" location="classpath:/subscription.cfg" />
    
</beans>
 


AppConfig.java
package com.concretepage;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@ImportResource("classpath:/app-conf.xml")
public class AppConfig  {
    private @Value("#{entProp['entitlement.name']}") String entName;
    private @Value("#{entProp['entitlement.time']}") int entTime;
    
    private @Value("#{subProp['subscription.name']}") String subName;
    private @Value("#{subProp['subscription.type']}") String subType;
			
	@Bean(name="entitlement")
	public Entitlement entitlement(){
		Entitlement ent= new Entitlement();
		ent.setName(entName);
		ent.setTime(entTime);
		return ent;
	}
	
	@Bean(name="subscription")
	public Subscription subscription(){
		Subscription sub= new Subscription();
		sub.setName(subName);
		sub.setType(subType);
		return sub;
	}
	
}

 


Entitlement.java
package com.concretepage;

public class Entitlement {
	private String name;
	private int time;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getTime() {
		return time;
	}
	public void setTime(int time) {
		this.time = time;
	}
}
 


Subscription.java
package com.concretepage;

public class Subscription {
	private String name;
	private String type;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}	
}
 


AppTest.java
package com.concretepage;
import java.sql.SQLException;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class AppTest {
	public static void main(String[] args) throws SQLException {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
 
		ctx.register(AppConfig.class);
		ctx.refresh();

		Entitlement ent= (Entitlement)ctx.getBean("entitlement");
	    System.out.println(ent.getName());		
	    System.out.println(ent.getTime());
		
		Subscription sub= (Subscription)ctx.getBean("subscription");
	    System.out.println(sub.getName());		
	    System.out.println(sub.getType());
	}
}
 


entitlement.cfg
entitlement.name= Entitlement
entitlement.time= 10
 


subscription.cfg
subscription.name=Ram
subscription.type= Monthly
 


Output
Entitlement
10
Ram
Monthly
 
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us