Spring4笔记2--Spring的第一个程序

Spring程序开发:

  1. 导入jar包(略)

  2. 创建Spring配置文件:

    Spring 配置文件的文件名可以随意,但 Spring 建议的名称为 applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xsi:schemaLocation="
 5         http://www.springframework.org/schema/beans  
 6         http://www.springframework.org/schema/beans/spring-beans.xsd">
 7     
 8     <!-- 注册bean,下面的注册,相当于在代码中写的
 9          ISomeService someService = new SomeServiceImpl();  
10     -->
11     <bean id="someService" class="com.tongji.service.SomeServiceImpl"/>
12 </beans>

  测试类:

 1 package com.tongji.test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.beans.factory.BeanFactory;
 5 import org.springframework.beans.factory.xml.XmlBeanFactory;
 6 import org.springframework.context.ApplicationContext;
 7 import org.springframework.context.support.ClassPathXmlApplicationContext;
 8 import org.springframework.context.support.FileSystemXmlApplicationContext;
 9 import org.springframework.core.io.ClassPathResource;
10 
11 import com.tongji.service.ISomeService;
12 import com.tongji.service.SomeServiceImpl;
13 
14 public class MyTest {
15     
16     //不使用Spring容器
17     //代码的问题是:SomeServiceImpl这个类,完全耦合到了测试类中
18     @Test
19     public void test01() {
20         ISomeService someService = new SomeServiceImpl();
21         someService.doSome();
22     }
23     
24     //从容器中获取Bean,使Bean类与测试类解耦合了
25     @Test
26     public void test02() {
27         //创建容器
28         ApplicationContext ac = 
29                 new ClassPathXmlApplicationContext("applicationContext.xml");
30         ISomeService someService = (ISomeService) ac.getBean("someService");
31         someService.doSome();
32     }
33     
34     //从D盘加载配置文件applicationContext.xml
35     @Test
36     public void test03() {
37         //创建容器
38         ApplicationContext ac = 
39                 new FileSystemXmlApplicationContext("d:/applicationContext.xml");
40         ISomeService someService = (ISomeService) ac.getBean("someService");
41         someService.doSome();
42     }
43     
44     //从项目的根下applicationContext.xml
45     @Test
46     public void test04() {
47         //创建容器
48         ApplicationContext ac = 
49                 new FileSystemXmlApplicationContext("applicationContext.xml");
50         ISomeService someService = (ISomeService) ac.getBean("someService");
51         someService.doSome();
52     }
53     
54     //直接使用BeanFactory容器
55     //ApplicationContext容器:在初始化容器时,就将容器中的所有对象进行了创建,内存占有大,但效率高
56     //BeanFactory容器:使用时才创建,相反
57     @Test
58     public void test05() {
59         //创建容器
60         BeanFactory bf = 
61                 new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
62         ISomeService someService = (ISomeService) bf.getBean("someService");
63         someService.doSome();
64     }
65 }

  解释:ApplicationContext 用于加载 Spring 的配置文件,在程序中充当“容器”的角色。其实现类有两个: 

    1. Spring 配置文件存放在项目的类路径下,则使用 ClassPathXmlApplicationContext 实现类进行加载。

    2. 若 Spring 配置文件存放在项目根路径或本地磁盘目录中,则使用 FileSystemXmlApplicationContext 实现类进行加载。

  BeanFactory 接口对象也可作为 Spring 容器出现。BeanFactory 接口是 ApplicationContext接口的父类。 若要创建 BeanFactory 容器,需要使用其实现类 XmlBeanFactory进行加载。而 Spring 配置文件以资源 Resouce 的形式出现在 XmlBeanFactory 类的构造器参数中。Resouce 是一个接口,其具有两个实现类:  
    1. ClassPathResource:指定类路径下的资源文件  
    2. FileSystemResource:指定项目根路径或本地磁盘路径下的资源文件。

原文地址:https://www.cnblogs.com/qjjazry/p/6361742.html