Spring程序开发:

  1. 导入jar包(略)

  2. 创建Spring配置文件:

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

 <?xml version="1.0" encoding="UTF-8"?>
<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,下面的注册,相当于在代码中写的
ISomeService someService = new SomeServiceImpl();
-->
<bean id="someService" class="com.tongji.service.SomeServiceImpl"/>
</beans>

  测试类:

 package com.tongji.test;

 import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.core.io.ClassPathResource; import com.tongji.service.ISomeService;
import com.tongji.service.SomeServiceImpl; public class MyTest { //不使用Spring容器
//代码的问题是:SomeServiceImpl这个类,完全耦合到了测试类中
@Test
public void test01() {
ISomeService someService = new SomeServiceImpl();
someService.doSome();
} //从容器中获取Bean,使Bean类与测试类解耦合了
@Test
public void test02() {
//创建容器
ApplicationContext ac =
new ClassPathXmlApplicationContext("applicationContext.xml");
ISomeService someService = (ISomeService) ac.getBean("someService");
someService.doSome();
} //从D盘加载配置文件applicationContext.xml
@Test
public void test03() {
//创建容器
ApplicationContext ac =
new FileSystemXmlApplicationContext("d:/applicationContext.xml");
ISomeService someService = (ISomeService) ac.getBean("someService");
someService.doSome();
} //从项目的根下applicationContext.xml
@Test
public void test04() {
//创建容器
ApplicationContext ac =
new FileSystemXmlApplicationContext("applicationContext.xml");
ISomeService someService = (ISomeService) ac.getBean("someService");
someService.doSome();
} //直接使用BeanFactory容器
//ApplicationContext容器:在初始化容器时,就将容器中的所有对象进行了创建,内存占有大,但效率高
//BeanFactory容器:使用时才创建,相反
@Test
public void test05() {
//创建容器
BeanFactory bf =
new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
ISomeService someService = (ISomeService) bf.getBean("someService");
someService.doSome();
}
}

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

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

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

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

Spring4笔记2--Spring的第一个程序的更多相关文章

  1. Spring的第一个程序

    目录 一.Spring概述 1. Spring是什么? 2. IOC控制反转 二.Spring的第一个程序 1. 创建Maven项目 2. 加入maven依赖pom.xml 3. 定义接口和实体类 4 ...

  2. 借助Maven入手Spring Boot第一个程序

    目前网上有不少Spring Boot的入门文章,都很有帮助,本人最近在深入学习Spring Cloud,在搭建第一个Hello World程序时,感觉对于新手而言,介绍文章怎么详细都不为过,因为其中坑 ...

  3. Hibernate5笔记1--Hibernate简介和第一个程序

    Hibernate简介: Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hib ...

  4. Go 基础学习笔记(3)| 第一个程序 “helloworld”

       //第一个程序总要说的清楚才行.   //建议先运行起第一个程序实践后,再看后面的具体解答 一.helloworld 编写运行 1.编写源程序,在 ~ /hello/src  编写hello.g ...

  5. Spring框架第一篇之Spring的第一个程序

    一.下载Spring的jar包 通过http://repo.spring.io/release/org/springframework/spring/地址下载最新的Spring的zip包,当然,如果你 ...

  6. 听翁恺老师mooc笔记(2)-第一个程序--&运算符

    使用devC++写hello world 第一步:文件-新建-源代码.然后输入"输出hello world"程序: 注意:写程序时关闭中文输入法,否则语句输入的分号可能会被识别为错 ...

  7. PyQt4学习笔记1:PyQt4第一个程序

    创建一个 PyQt4 一般可以通过很少的步骤完成.通常的方法是用Qt 提供的QtDesigner工具创建界面.使用QtDesigner,可以方便地创建复杂的GUI界面.然后,可以在窗口上创建部件, 添 ...

  8. Python 自学笔记(二)第一个程序 Hello World

    一 打印 Hello world 1,输入 Python “Hello world” 即可 2,脚本文件输出Hello World 在命令行(cmd),输入 python 文件路径+文件名 3,为什么 ...

  9. Spring实战第五章学习笔记————构建Spring Web应用程序

    Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...

  10. Spring实战第一章学习笔记

    Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...

随机推荐

  1. js时间戳转换日期格式和日期计算

    一.时间戳转换日期 function formatDate(datetime) { // 获取年月日时分秒值 slice(-2)过滤掉大于10日期前面的0 var year = datetime.ge ...

  2. springmvc+mybatis 处理图片(二):显示图片

    数据库及配置文件等参考:springmvc+mybatis 处理图片(一):上传图片思路:把图片二进制信息写入到HttpServletResponse 的outputStream输出流中来显示图片.一 ...

  3. MT【160】格点

    (2017年清华大学  THUSSAT) 把不超过实数 $x$ 最大整数记为 $[x]$,任取互质且不小于 3 的正奇数 $m,n$,令$$I=\sum_{i=1}^{\frac{m-1}{2}}\l ...

  4. BZOJ2159 Crash 的文明世界 【第二类斯特林数 + 树形dp】

    题目链接 BZOJ2159 题解 显然不能直接做点分之类的,观察式子中存在式子\(n^k\) 可以考虑到 \[n^k = \sum\limits_{i = 0} \begin{Bmatrix} k \ ...

  5. 【codeforces 235E】 Number Challenge

    http://codeforces.com/problemset/problem/235/E (题目链接) 题意 给出${a,b,c}$,求${\sum_{i=1}^a\sum_{j=1}^b\sum ...

  6. 解题:CQOI 2015 选数

    题面 神仙题,不需要反演 首先上下界同时除以$k$,转换成取$n$个$gcd$为$1$的数的方案数,其中上界向下取整,下界向上取整 然后设$f[i]$表示选$n$个互不相同的数$gcd$为$i$的方案 ...

  7. HEOI 2017 游记

    HEOI2017也算是落下帷幕了,那就写一篇 流水账 游记好了. DAY 0 又是熟悉的大学,又是熟悉的机房 YD宾馆的房间依旧破的不行. 晚上在房间颓颓颓....=.= DAY 1 上午去试机,唯一 ...

  8. bzoj 4451 : [Cerc2015]Frightful Formula FFT

    4451: [Cerc2015]Frightful Formula Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 177  Solved: 57[Sub ...

  9. Backbone学习总结

    Backbone中文学习文档:http://www.css88.com/doc/backbone/ 来到公司已经有一段时间了,到现在深深的感觉到自己的能力弱的像只周黑鸭,又干涩又黝黑,充满了麻(手麻脑 ...

  10. 使用 swoole 加速你的 laravel

    在此前的另外一篇文章讨论过 opcache:php 性能优化之opcache - 让你的php性能提升 50% 再来复习一下吧,导致 php 慢的各种因素中解析性语言的特性可以说是罪魁祸首,再加上,每 ...