zoukankan      html  css  js  c++  java
  • day38 04-Spring加载配置文件

     Spring的工厂类ApplicationContext有两个子类:ClassPathXmlApplicationConext和FileSystemXmlApplication都可以用来加载配置文件。

    Ctrl+T查看ApplicationContext的子类:

    如果把ApplicationContextx.xml放到工程下面或者是其他路径下它都可以加载的。

    applicationContextx.xml在web工程目录里面就行。如果不在工程目录里,比如在D盘那就写个ApplicationContext applicationContext = new FileSystemXmlApplicationContext("D:applicationContext.xml");如果不在工程里面你那个web项目肯定就不行了。web项目最后要与Struts 2等整合之后,肯定是一启动服务器之后就要加载这个配置文件,找不到了。


    package cn.itcast.spring3.demo1;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.context.support.FileSystemXmlApplicationContext;
    
    public class SpringTest1 {
        @Test
        //传统方式
        public void demo1(){
            //造成程序紧密耦合.
            //应该采用工厂+配置文件+反射的机制
            HelloService helloService = new HelloServiceImpl();
            helloService.sayHello();
        }
        @Test
        //Spring开发
        public void demo2(){
            //创建一个Spring的工厂类.
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//ClassPathXmlApplication加载的是classe//s下的文件
            //ApplicationContext就是Spring的工厂类
            //不写applicationContextx.xml的全路径,默认会去WEB-INF下面找applicationContextx.xml
            //但是现在applicationContextx.xml写好之后已经发布到WEB-INF的classes里面
            HelloService helloService = (HelloService) applicationContext.getBean("helloService");
            helloService.sayHello();
        }
        @Test
        //加载磁盘路径下的配置文件:
        public void demo3(){
            ApplicationContext applicationContext = new FileSystemXmlApplicationContext("applicationContext.xml");
            HelloService helloService = (HelloService) applicationContext.getBean("helloService");
            helloService.sayHello();
        }
        
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 别去schema,schema是文件,本地的文件,你得引那个头 -->
    
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    <!-- 把接口和实现类在这个配置文件中配置,有了实现类的全路径之后到时候才能用工厂反射 -->
       
       <!-- 通过一个<bean>标签来设置类的信息,通过id属性为类起个标识. -->
        <!-- 接口,实现类,配置文件也都有了 -->
        <!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
        <!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
             也是这么做的,只不过工厂由Spring提供好了
         -->
        <bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
             <!-- 使用<property>标签注入属性 
             value指的是普通值
             ref指的是对象
             -->
        <property name="info"  value="传智播客(磁盘路径)"></property>
        </bean>
    </beans>
  • 相关阅读:
    事件的基本概念
    c# 语法 字符串内插
    tcp 的编程例子
    Action 语法的简介
    wcf 的小介绍
    Socket编程
    c# base new 等关键字基础
    虚函数和继承的关系
    arraylist,list ,数组区别
    Python爬虫-selenium模拟豆瓣电影鼠标下拉
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6718550.html
Copyright © 2011-2022 走看看