zoukankan      html  css  js  c++  java
  • spring 资源访问

    spring 资源访问

            Resource resource=null;
            //访问网络资源
            resource=new UrlResource("file:bool.xml");
            //访问类加载路径下的资源
            resource=new ClassPathResource("book.xml");
            //访问文件系统资源
            resource=new FileSystemResource("book.xml");
            //访问字节数组资源
            String str="";
            byte[] fileBytes=str.getBytes();
            resource=new ByteArrayResource(fileBytes);
            //resource=new ServletContextResource()
            //resource=new InputStreamResource()
            InputStream inputStream= resource.getInputStream();
            boolean exists=resource.exists();
            boolean isOpen=resource.isOpen();
            String description=resource.getDescription();
            File file=resource.getFile();
            URL url= resource.getURL();
    
            ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");
            resource=ctx.getResource("book.xml");
            
            ctx=new FileSystemXmlApplicationContext("beans.xml");
            ctx=new FileSystemXmlApplicationContext("/beans.xml");
            //加载相对路径
            ctx=new FileSystemXmlApplicationContext("file:beans.xml");
            //加载绝对路径
            ctx=new FileSystemXmlApplicationContext("file:/beans.xml");
            ctx=new FileSystemXmlApplicationContext("classpath:beans.xml");
            //加载多个配置文件
            ctx=new FileSystemXmlApplicationContext("classpath*:beans.xml");
            ctx=new ClassPathXmlApplicationContext("beans*.xml");
            ctx=new FileSystemXmlApplicationContext("classpath*:bean*.xml");        

    配置管理

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.ImportResource;
    
    //修饰一个Java配置类
    @Configuration
    //导入XML配置
    @ImportResource("classpath:/beans.xml")
    public class AppConfig {
        //修饰一个方法,将该方法的返回值定义成容器中的一个Bean
        @Bean
        public Person person(){
            
        }
        @Bean(name = "stoneAxe")
        public Axe stoneAxe(){
            
        }
        @Bean(name = "steelAxe")
        public Axe steelAxe(){
            
        }
    }
  • 相关阅读:
    vim在插入模式下的粘贴
    关于外语学习的7个“美丽误会”(图)
    美国在线教育的启示:教育领域正在革命
    自动生成计算题
    字符串的截取,分割,替换
    将字符串转成byte,然后再转回来 日期相互转化字符串
    java编写的万年历代码
    小球弹砖块游戏(JAVA)
    随即输入一段字符串,统计出数字,大写字母。小写字母,空格
    邮箱验证信息
  • 原文地址:https://www.cnblogs.com/uptothesky/p/8290179.html
Copyright © 2011-2022 走看看