Spring4笔记2--Spring的第一个程序
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的第一个程序的更多相关文章
- Spring的第一个程序
目录 一.Spring概述 1. Spring是什么? 2. IOC控制反转 二.Spring的第一个程序 1. 创建Maven项目 2. 加入maven依赖pom.xml 3. 定义接口和实体类 4 ...
- 借助Maven入手Spring Boot第一个程序
目前网上有不少Spring Boot的入门文章,都很有帮助,本人最近在深入学习Spring Cloud,在搭建第一个Hello World程序时,感觉对于新手而言,介绍文章怎么详细都不为过,因为其中坑 ...
- Hibernate5笔记1--Hibernate简介和第一个程序
Hibernate简介: Hibernate是一个开放源代码的ORM(对象关系映射)框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hib ...
- Go 基础学习笔记(3)| 第一个程序 “helloworld”
//第一个程序总要说的清楚才行. //建议先运行起第一个程序实践后,再看后面的具体解答 一.helloworld 编写运行 1.编写源程序,在 ~ /hello/src 编写hello.g ...
- Spring框架第一篇之Spring的第一个程序
一.下载Spring的jar包 通过http://repo.spring.io/release/org/springframework/spring/地址下载最新的Spring的zip包,当然,如果你 ...
- 听翁恺老师mooc笔记(2)-第一个程序--&运算符
使用devC++写hello world 第一步:文件-新建-源代码.然后输入"输出hello world"程序: 注意:写程序时关闭中文输入法,否则语句输入的分号可能会被识别为错 ...
- PyQt4学习笔记1:PyQt4第一个程序
创建一个 PyQt4 一般可以通过很少的步骤完成.通常的方法是用Qt 提供的QtDesigner工具创建界面.使用QtDesigner,可以方便地创建复杂的GUI界面.然后,可以在窗口上创建部件, 添 ...
- Python 自学笔记(二)第一个程序 Hello World
一 打印 Hello world 1,输入 Python “Hello world” 即可 2,脚本文件输出Hello World 在命令行(cmd),输入 python 文件路径+文件名 3,为什么 ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- Spring实战第一章学习笔记
Spring实战第一章学习笔记 Java开发的简化 为了降低Java开发的复杂性,Spring采取了以下四种策略: 基于POJO的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...
随机推荐
- HDU 6166 Senior Pan(二进制分组+最短路)
题意 给出一个\(n\)个点\(m\)条边的有向图\((n,m<=100000)\),从中选择\(k\)个点\((k<=n)\),问这k个点两两之间的最短路最小值是多少? 思路 直接的想法 ...
- 打开eclipse编译后的.class文件
众所周知,用文本编辑器打开.class文件会乱码.我们可以使用命令行打开.class文件项目结构: 代码: public class Synchronized { public static void ...
- vs2017自动生成的#include“stdafx.h”详解及解决方案
vs2017自动生成的#include“stdafx.h”详解及解决方案 问题描述: 在高版本的Visual Studio的默认设置中,会出现这么一个现象,在新建项目之后,项目会自动生成#includ ...
- 【刷题】BZOJ 4816 [Sdoi2017]数字表格
Description Doris刚刚学习了fibonacci数列.用f[i]表示数列的第i项,那么 f[0]=0 f[1]=1 f[n]=f[n-1]+f[n-2],n>=2 Doris用老师 ...
- AtCoder Grand Contest 003
AtCoder Grand Contest 003 A - Wanna go back home 翻译 告诉你一个人每天向哪个方向走,你可以自定义他每天走的距离,问它能否在最后一天结束之后回到起点. ...
- 【模板】网络流-最大流模板(Dinic)
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> u ...
- Rotting Oranges - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Rotting Oranges - LeetCode 注意点 解法 解法一:bfs.首先先统计所有新鲜的橘子数目fresh,如果fresh大于0则一直执行 ...
- Valid Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...
- Nginx web服务优化 (一)
1.Nginx基本安全优化 a.更改配置文件参数隐藏版本 编辑nginx.conf配置文件增加参数,实现隐藏Nginx版本号的方式如下.在nginx配置文件nginx.conf中的http标签段内加入 ...
- Activity的setResult方法
Activity的setResult方法http://blog.csdn.net/dinglin_87/article/details/8970144 调用setResult()方法必须在finish ...