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的轻量级和最小侵入性编程: 通过依赖注入和面向接口实现松耦合: 基于切面 ...
随机推荐
- 在输入密码框中眼睛睁开可以看见数字,眼睛闭上看不见数字怎么用JS实现
无论做那个项目,登录注册页面总是避免不了的,那怎么用js来控制密码的显示和隐藏呢?先看一下效果图: HTML代码如下: <div> <label for=" ...
- 2018 焦作icpc现场赛总结
Day 0 没有直达焦作的飞机,所以选择了先到新郑机场,再转乘城际列车.城际列车猜是专门给学生开通的吧,每天只有来和回一共两趟(所以机票选择的余地也不多).买的时候只有无座票了,本来以为会一直站着,但 ...
- Python学习---日期时间
在Python里面日期时间的功能主要由几个模块提供:time,calendar,datetime,date等 time主要用到的功能函数: #!/usr/bin/python3 # coding:ut ...
- [BZOJ1500][NOI2005]维修数列 解题报告 Splay
Portal Gun:[BZOJ1500][NOI2005]维修数列 有一段时间没写博客了,最近在刚数据结构......各种板子背得简直要起飞,题目也是一大堆做不完,这里就挑一道平衡树的题来写写好了 ...
- 【BZOJ4242】水壶(克鲁斯卡尔重构树,BFS)
[BZOJ4242]水壶(克鲁斯卡尔重构树,BFS) 题面 BZOJ然而是权限题. Description JOI君所居住的IOI市以一年四季都十分炎热著称. IOI市是一个被分成纵H*横W块区域的长 ...
- 查看ubuntu版本号
No.1 cat /etc/issue No.2 cat /proc/version No.3 uname -a No.4 lsb_release -a
- 《Linux内核设计与实现》学习总结 Chap4
第四章 进程调度 调度程序负责决定将哪个进程投入运行,何时运行以及运行多长时间,进程调度程序可看做在可运行态进程之间分配有限的处理器时间资源的内核子系统.只有通过调度程序的合理调度,系统资源才能最大限 ...
- 20170520 DP阶段总结
DP的力量不是无穷的. 但是,因为它叫做“动态规划”,它在OI界如鱼得水.这个“动态”不是指“离线”与“在线”,也不是什么“可持久化”.它只是把问题抽象为一个个“阶段”,在每一个“阶段”中作出或繁或简 ...
- C++ public private protect 继承关系(链接)
基础链接 总结: public继承基类成员访问权限没有变化; protected继承基类public和protected权限变为protected,基类private不变. private继承基类p ...
- laravel mapSpread 例子
$collection = collect(range(1, 9)); $chunks = $collection->chunk(2); $labeld = $chunks->mapSpr ...