Spring util:properties Example

By Arvind Rai, November 07, 2021
On this page we will learn using Spring util:properties element. The <util:properties> element is used to load java.util.Properties file. The <util:properties> is the concise form of PropertiesFactoryBean bean.
Find the code for PropertiesFactoryBean bean.
<bean id="entProp" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="location" value="classpath:/entitlement.cfg"/>
</bean> 
Find the concise representation of above code using <util:properties> element.
<util:properties id="entProp" location="classpath:/entitlement.cfg"/> 

Using <util:properties>

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.xsd
    http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util.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 

Reference

Using <util:properties/>
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us