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>

最新文章

  1. UNITY 移动到指定位置的写法
  2. HDU 2586
  3. HDU 2853 最大匹配&amp;KM模板
  4. 利用SQL语句查询一个数据库中的所有表
  5. Wordpress-数据库结构分析(转)
  6. Android JIN返回结构体
  7. JSP九个隐式对象及作用域
  8. UML学习-活动图创建
  9. js判断是否为pc端或移动端
  10. 24篇HTTP博客
  11. selenuim,qtp,loadrunner,jmeter有何区别,想学个脚本语言python和测试工具应该从哪里入门呢。
  12. ubuntu如何进入local、bin目录
  13. [2015-11-10]分享一个调用msbuild生成解决方案并打包发布的批处理脚本
  14. 【原创】抓个Firefox的小辫子,围观群众有:Chrome、Edge、IE8-11
  15. Python3学习之路~8.5 SocketServer实现多并发
  16. Python—合并两个有序列表
  17. [工作日志]2018-11-15 主要: 改bug
  18. MYSQL查询优化:数据类型与效率
  19. kegg富集分析之:KEGGREST包(9大功能)
  20. POJ 1154

热门文章

  1. IDEA Error:java: Compilation failed: internal java compiler error 解决方案
  2. iOS之CAScrollLayer属性简介和使用
  3. Thrift(PHP)入门无错篇章(一)
  4. 关于Spring Cloud Feign的一些记录!
  5. Pandas对于CSV的简单操作
  6. sqlserver存储过程事务回滚
  7. js格式化数字为金额
  8. Java基础知识(多线程和线程池)
  9. 嘴巴题4 「BZOJ1827」[Usaco2010 Mar] gather 奶牛大集会
  10. [转]TabControl Style in WPF