Example of util:properties in Spring

til: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

<beansxmlns="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:propertiesid="entProp"location="classpath:/entitlement.cfg"/><util:propertiesid="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")publicclassAppConfig{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")publicEntitlement entitlement(){Entitlement ent=newEntitlement();
		ent.setName(entName);
		ent.setTime(entTime);return ent;}@Bean(name="subscription")publicSubscription subscription(){Subscriptionsub=newSubscription();sub.setName(subName);sub.setType(subType);returnsub;}}



Entitlement.java

package com.concretepage;publicclassEntitlement{privateString name;privateint time;publicString getName(){return name;}publicvoid setName(String name){this.name = name;}publicint getTime(){return time;}publicvoid setTime(int time){this.time = time;}}



Subscription.java

package com.concretepage;publicclassSubscription{privateString name;privateString type;publicString getName(){return name;}publicvoid setName(String name){this.name = name;}publicString getType(){return type;}publicvoid setType(String type){this.type = type;}}



AppTest.java

package com.concretepage;import java.sql.SQLException;import org.springframework.context.annotation.AnnotationConfigApplicationContext;publicclassAppTest{publicstaticvoid main(String[] args)throwsSQLException{AnnotationConfigApplicationContext ctx =newAnnotationConfigApplicationContext();
 
		ctx.register(AppConfig.class);
		ctx.refresh();Entitlement ent=(Entitlement)ctx.getBean("entitlement");System.out.println(ent.getName());System.out.println(ent.getTime());Subscriptionsub=(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

posted @ 2013-06-08 15:56  悟寰轩-叶秋  阅读(2393)  评论(0编辑  收藏  举报