• SpringBoot(四)---配置文件占位符


    在我们配置properties文件时,可以通过文件占位符使用属性文件中的其他属性值。

    举个例子:

    ConfigBean.java

    package com.zk.myspringboot_003;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.stereotype.Component;
    
    @Component
    @ConfigurationProperties(prefix = "demo")
    @PropertySource(value = {"classpath:application.properties"})
    public class ConfigBean {
    	private String name;
    	
    	private String age;
    
    	private Map<String, String> login=new HashMap<String,String>();
    	
    	private List<String> urls=new ArrayList<String>();
    	
    	
    	
    	public Map<String, String> getLogin() {
    		return login;
    	}
    
    	public void setLogin(Map<String, String> login) {
    		this.login = login;
    	}
    
    	public List<String> getUrls() {
    		return urls;
    	}
    
    	public void setUrls(List<String> urls) {
    		this.urls = urls;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public void setName(String name) {
    		this.name = name;
    	}
    
    	public String getAge() {
    		return age;
    	}
    
    	public void setAge(String age) {
    		this.age = age;
    	}
    
    	@Override
    	public String toString() {
    		return "ConfigBean [name=" + name + ", age=" + age + ", login=" + login + ", urls=" + urls + "]";
    	}
    	
    	
    }
    

    SpringBootApp.java

    package com.zk.myspringboot_003;
    
    import java.io.IOException;
    import java.util.Properties;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.event.ApplicationStartedEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.annotation.ImportResource;
    import org.springframework.core.io.support.PropertiesLoaderUtils;
    
    @SpringBootApplication
    @ImportResource(locations={"classpath:beans.xml"})
    //@ImportResource:导入spring配置文件让配置文件的内容生效
    public class SpringBootApp implements ApplicationListener<ApplicationStartedEvent>{
    
    	public static void main(String[]args){
    		SpringApplication.run(SpringBootApp.class, args);
    	}
    
    	@Override
    	public void onApplicationEvent(ApplicationStartedEvent event) {
    		// TODO Auto-generated method stub
    		Properties properties;
    		try {
    			properties = PropertiesLoaderUtils.loadAllProperties("classpath:application.properties");
    			System.out.println(properties);
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
            
    	}
    }
    

    TestController.java

    package com.zk.myspringboot_003;
    
    import java.util.List;
    import java.util.Map;
    import java.util.Map.Entry;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.core.env.Environment;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class TestController {
    	
    	@Value("${demo.name}")
    	private String name;
    	@Value("${demo.age}")
    	private String age;
    	
    	
    	@Autowired
    	private Environment environment;
    //	@Autowired
    //	private ConfigBeanValue configbeanValue;
    	
    	@Autowired
        private ConfigBean configBeanProp;
    	@RequestMapping("/demo")
    	public String hello()
    	{
    		return "hello,"+name+",you are "+age+"demo1";
    	}
    	
    	@RequestMapping("/demo2")
    	public String getprops(){
    		String name=environment.getProperty("demo.name");
    		String age=environment.getProperty("demo.age");
    		List<String> urlslist=configBeanProp.getUrls();
    		Map<String,String> loginmap=configBeanProp.getLogin();
    		for(Entry<String, String> e:loginmap.entrySet())
    		{
    			System.out.println(e.getKey()+":"+e.getValue());
    		}
    		return "hello,"+name+".you are "+age+"demo2
    "+urlslist.toString()+"
    "+loginmap.get("username");
    	}
    	
    	@RequestMapping("/demo3")
    	public String getBean(){
    		String name=configBeanProp.getName();
    		String age=configBeanProp.getAge();
    		return "hello,"+name+".you are "+age+"demo3";
    	}
    }

    application.properties

    demo.name=zk${demo.age}
    demo.age=25${random.uuid}_age
    #map
    demo.login[username]=zhangdeshuai
    demo.login[password]=zhenshuai
    demo.login[callback]=http://www.flyat.cc
    #list
    demo.urls[0]=http://ztool.cc
    demo.urls[1]=http://ztool.cc/format/js
    demo.urls[2]=http://ztool.cc/str2image
    demo.urls[3]=http://ztool.cc/json2Entity
    demo.urls[4]=http://ztool.cc/ua  

    在配置文件中的占位符为

    ${demo.age}

    在运行代码后运行的结果如下:

  • 相关阅读:
    一个常见数据库操作错误的分析(四)
    一个常见数据库操作错误的分析(六)
    vc6 配置使用 boost 1.34.1
    string tips
    greta 编译,配置,使用
    xml学习笔记(一)
    火狐插件Firebug的使用
    浅谈编写高性能的Javascript代码 【重要】
    WebConfig加密/解密
    微软发布3款SQL Injection(SQL 注入)攻击检测工具
  • 原文地址:https://www.cnblogs.com/longlyseul/p/12528308.html
Copyright © 2020-2023  润新知