• 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

  • 相关阅读:
    Django: 项目实战从0开始---实现登录注册系统。(7)
    Django: 项目实战从0开始---实现登录注册系统。(6)
    Django: 项目实战从0开始---实现登录注册系统。(5)
    Django: 项目实战从0开始---实现登录注册系统。(4)
    Django: 项目实战从0开始---实现登录注册系统。(3)
    Django: 项目实战从0开始---实现登录注册系统。(2)
    Django: 项目实战从0开始---实现登录注册系统。(1)
    Centos 7: Python3运维脚本(查看服务器信息并发送邮件)
    vue 错误记录
    ide 下spingboot 实现热部署
  • 原文地址:https://www.cnblogs.com/sunxucool/p/3126943.html
Copyright © 2020-2023  润新知