• spring 08-Spring框架Resource资源注入


    通过配置文件applicationContext.xml去进行Resource资源读取

    定义一个专门负责资源处理的程序:

    package cn.liang.util;
    import java.util.Scanner;
    import org.springframework.core.io.Resource;
    public class ResourceUtil {
    	private Resource src ;	// 此资源将通过Spring的配置文件管理注入
    	public void setSrc(Resource src) {
    		this.src = src;
    	}
    	public void print() throws Exception {	// 直接输出资源的内容
    		if (this.src.exists()) {	// 表示该资源存在
    			Scanner scan = new Scanner(this.src.getInputStream()) ;
    			scan.useDelimiter(" ") ;
    			while (scan.hasNext()) {
    				System.out.println(scan.nextLine());
    			}
    			scan.close(); 
    		} 
    	}
    }
    

    配置applicationContext.xml文件

    读取CLASSPATH资源:

    <bean id="resourceUtil" class="cn.liang.util.ResourceUtil">
    <property name="src" value="classpath:applicationContext.xml"/>
    </bean>
    

    读取网络资源

    <bean id="resourceUtil" class="cn.liang.util.ResourceUtil">
    <property name="src" value="http://www.baidu.com"/>
    </bean>
    

    读取文件资源

    <bean id="resourceUtil" class="cn.liang.util.ResourceUtil">
    <property name="src" value="file:/etc/hosts"/>
    </bean>
    

    测试程序:

    package cn.liang.demo;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import cn.liang.util.ResourceUtil;
    public class ResourceDemo03 {
    	public static void main(String[] args) throws Exception {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    		ResourceUtil ru = ctx.getBean("resourceUtil", ResourceUtil.class);
    		ru.print(); 
    	}
    }
    

    通过配置文件applicationContext.xml去进行多个Resource资源读取

    定义一个专门负责资源处理的程序:

    package cn.liang.util;
    import java.util.Scanner;
    import org.springframework.core.io.Resource;
    public class ResourceUtils {
    	private Resource src[]; // 此资源将通过Spring的配置文件管理注入
    
    	public void setSrc(Resource[] src) {
    		this.src = src;
    	}
    
    	public void print() throws Exception { // 直接输出资源的内容
    		for (int x = 0; x < this.src.length; x++) {
    			if (this.src[x].exists()) { // 表示该资源存在
    				System.out.println(this.src[x].getFilename() + ":");
    				Scanner scan = new Scanner(this.src[x].getInputStream());
    				scan.useDelimiter(" ");
    				while (scan.hasNext()) {
    					System.out.println(scan.nextLine());
    				}
    				scan.close();
    			}
    			System.out.println("**********************************");
    		}
    	}
    }
    

    配置applicationContext.xml文件

    <bean id="resourceUtils" class="cn.liang.util.ResourceUtils">
    	<property name="src">
    		<array>
    			<value>classpath:applicationContext.xml</value>
    			<value>http://www.baidu.com</value>
    			<value>file:/etc/hosts</value>
    		</array>
    	</property>
    </bean>
    

    测试程序:

    package cn.liang.demo;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import cn.liang.util.ResourceUtils;
    public class ResourceDemo04 {
    	public static void main(String[] args) throws Exception {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    		ResourceUtils ru = ctx.getBean("resourceUtils", ResourceUtils.class);
    		ru.print(); 
    	}
    }
    

    Resource通配符

    • 最常见的资源文件就是“*.properties”文件
    • Resource处理资源时支持三种通配符
      • ?:匹配任意一位字符
        • test-? --匹配test-1、test-2、test-x
      • *:匹配任意多位字符
        • a/*/test --表示在a这个父目录中一级子目录下所有的test文件
      • **:匹配任意多级目录的字符串内容
        • a/**/test --表示不管什么目录下的指定名称文件都进行匹配

    定义一个专门负责资源处理的程序:

    package cn.liang.util;
    import java.util.Scanner;
    import org.springframework.core.io.Resource;
    public class ResourceUtils {
    	private Resource src[]; // 此资源将通过Spring的配置文件管理注入
    
    	public void setSrc(Resource[] src) {
    		this.src = src;
    	}
    
    	public void print() throws Exception { // 直接输出资源的内容
    		for (int x = 0; x < this.src.length; x++) {
    			if (this.src[x].exists()) { // 表示该资源存在
    				System.out.println(this.src[x].getFilename() + ":");
    				Scanner scan = new Scanner(this.src[x].getInputStream());
    				scan.useDelimiter(" ");
    				while (scan.hasNext()) {
    					System.out.println(scan.nextLine());
    				}
    				scan.close();
    			}
    			System.out.println("**********************************");
    		}
    	}
    }
    

    配置applicationContext.xml文件

    <bean id="resourceUtils" class="cn.liang.util.ResourceUtils">
    	<property name="src">
    		<array>
    			<value>classpath:**/LICENSE*</value>
    		</array>
    	</property>
    </bean>
    

    测试程序:

    package cn.liang.demo;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import cn.liang.util.ResourceUtils;
    public class ResourceDemo04 {
    	public static void main(String[] args) throws Exception {
    		ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
    		ResourceUtils ru = ctx.getBean("resourceUtils", ResourceUtils.class);
    		ru.print(); 
    	}
    }
    

    文件部署:

    localhost:main liang$ tree resources/
    resources/
    ├── applicationContext.xml
    ├── file1
    │   └── LICENSE.txt
    ├── file2
    │   ├── LICENSE-1.txt
    │   └── LICENSE-2.txt
    ├── file3
    │   └── file3-1
    │       └── LICENSE-x.txt
    └── log4j.properties
    

    输出结果:

    LICENSE.txt:
    test null
    **********************************
    LICENSE-1.txt:
    test 01
    **********************************
    LICENSE-2.txt:
    test 02 
    **********************************
    LICENSE-x.txt:
    test x
    **********************************
    
  • 相关阅读:
    提升云桌面登录账号安全,宁盾双因素认证“护航”齐鲁制药移动办公
    关于双因素认证(2FA),这些基础知识你一定要知道
    3天时间,如何用双因素认证帮5000名员工实现远程办公账号安全
    企业快速发展,新技术新场景频出,如何构建身份管理体系支撑企业持续发展变革?
    关于ADFS的局限性,你了解多少?
    初创公司如何布局零信任网络安全?
    什么是身份和访问管理(IAM)?
    为什么是时候迎接远程办公了?
    无密码就是最好的密码,深入解析免密认证的方法和实例
    带你了解免密认证的优势和挑战
  • 原文地址:https://www.cnblogs.com/liangjingfu/p/10059673.html
Copyright © 2020-2023  润新知