Spring框架第一篇之Spring的第一个程序
一、下载Spring的jar包
通过http://repo.spring.io/release/org/springframework/spring/地址下载最新的Spring的zip包,当然,如果你是在使用maven工程的话,可以不用下载Zip包,可以直接在maven工程的pom.xml文件中添加Spring的依赖即可。
二、创建工程导入jar包
第一篇的内容记录一些入门知识点,所以只需要导入几个必要的基础包则可,这里项目只导入Spring的以下几个包:
spring-core-4.3.9.RELEASE.jar
spring-beans-4.3.9.RELEASE.jar
spring-context-4.3.9.RELEASE.jar
spring-expression-4.3.9.RELEASE.jar
除此之外,还需要导入两个日志jar包:
log4j-1.2.15.jar
commons-logging-1.1.3.jar
三、创建测试工程
整个工程目录如下图所示:
3.1 编写ISomeService接口类
package com.ietree.spring.basic.ioc; /**
* 接口类
*
* @author Root
*/
public interface ISomeService { void doSomeThing(); }
3.2 编写SomeServiceImpl类,实现ISomeService
package com.ietree.spring.basic.ioc; /**
* 实现类
*
* @author Root
*/
public class SomeServiceImpl implements ISomeService { public SomeServiceImpl() {
System.out.println("执行无参构造器,创建SomeServiceImpl对象");
} @Override
public void doSomeThing() {
System.out.println("执行doSomeThing()方法...");
} }
3.3 编写applicationContext.xml配置文件
其中xml中的schema配置可以直接从Spring官方文档中复制(spring-framework-4.3.9.RELEASE\docs\spring-framework-reference\html\xsd-configuration.html)
所以将schema复制之后需要配置对应的bean,这里的id可以随便命名,只需要对应需要创建的类即可,之后需要根据配置的bean的id值来获取对象,不要再通过最早的那种通过new Object()的方式获取了。
<?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"> <!--
注册Service
这里相当于容器做了SomeServiceImpl myService = new SomeServiceImpl();
-->
<bean id="myService" class="com.ietree.spring.basic.ioc.SomeServiceImpl"/> </beans>
以上步骤配置完成之后,Spring的环境就算简单搭建完成了,现在来测试一下。
四、获取对象的几种方式
1、通过new Object();方式获取对象
@Test
public void test01() {
ISomeService service = new SomeServiceImpl();
service.doSomeThing();
}
2、通过ClassPathXmlApplicationContext对象加载Spring的配置文件,采用getBean的方式获取对象
@Test
public void test02() { // 创建容器对象,加载Spring配置文件
// ClassPathXmlApplicationContext会从类路径下查找配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); ISomeService service = (ISomeService) ac.getBean("myService");
service.doSomeThing();
}
3、通过FileSystemXmlApplicationContext对象加载Spring的配置文件,采用getBean的方式获取对象
@Test
public void test03() { // FileSystemXmlApplicationContext会从项目的根下查找配置文件,或者从当前系统的D盘根目录下查找配置文件
ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); ISomeService service = (ISomeService) ac.getBean("myService");
service.doSomeThing();
}
4、通过BeanFactory对象加载Spring的配置文件,采用getBean的方式获取对象
// ApplicationContext与BeanFactory容器的区别:
// 1)ApplicationContext容器在进行初始化时,会将其中的所有Bean(对象)进行创建。
// 缺点:占用系统资源(内存、CPU等)
// 优点:响应速度快
// 2)BeanFactory容器中的对象,在容器初始化时并不会被创建,而是在真正获取该对象时才被创建。
// 缺点:响应速度慢
// 优点:不多占用系统资源(内存、CPU等)
@Test
public void test04() { // 创建容器对象,加载Spring配置文件
BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml")); ISomeService service = (ISomeService) bf.getBean("myService");
service.doSomeThing();
}
五、获取对象的源码分析
点击ApplicationContext按 CTRL + T 可以看到下图,ApplicationContext对象实际上是一个接口,它有ClassPathXmlApplicationContext和FileSystemXmlApplicationContext两种实现方式。
1、首先进入ClassPathXmlApplicationContext对象,可以看到有很多的构造方法,实际上只要将applicationContext.xml配置文件放在项目中的src目录下,使用ClassPathXmlApplicationContext的任何一种构造方法都可以获取到容器。
比如:
@Test
public void test02() { // 创建容器对象,加载Spring配置文件
// ClassPathXmlApplicationContext会从类路径下查找配置文件
@SuppressWarnings("resource")
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// ApplicationContext ac = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml"}, true, null); ISomeService service = (ISomeService) ac.getBean("myService");
service.doSomeThing();
}
使用快捷键CTRL + O 可以查看类结构,下图是ClassPathXmlApplicationContext对象结构图:
2、之后再进入FileSystemXmlApplicationContext类查看类结构:
实际上会发现这两个类非常相似,不同的是,在使用ClassPathXmlApplicationContext类时,applicationContext.xml配置文件只能放在项目的src目录下,而在使用FileSystemXmlApplicationContext类时,只需要将类放在项目的根目录下,
或者放在当前系统指定的盘符下,程序都可以加载到容器。因为 FileSystemXmlApplicationContext首先会从项目的根下查找配置文件,或者从当前系统的盘盘符根目录下查找配置文件。
3、看看使用 BeanFactory bf = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));方式实现的原理:
首先进入ApplicationContext接口会发现这个接口实际继承了很多的其他类,其中有HierarchicalBeanFactory类,
public interface ApplicationContext extends EnvironmentCapable, ListableBeanFactory, HierarchicalBeanFactory,
MessageSource, ApplicationEventPublisher, ResourcePatternResolver {
进入HierarchicalBeanFactory接口,可以看到还有XmlBeanFactory的实现方式:
所以使用 XmlBeanFactory的构造函数也能够实现获取容器对象
public XmlBeanFactory(Resource resource) throws BeansException {
this(resource, null);
}
Spring框架第一篇之Spring的第一个程序的更多相关文章
- Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)
在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...
- Spring 框架的概述以及Spring中基于XML的IOC配置
Spring 框架的概述以及Spring中基于XML的IOC配置 一.简介 Spring的两大核心:IOC(DI)与AOP,IOC是反转控制,DI依赖注入 特点:轻量级.依赖注入.面向切面编程.容器. ...
- Spring之旅第一篇-初识Spring
一.概述 只要用框架开发java,一定躲不过spring,Spring是一个轻量级的Java开源框架,存在的目的是用于构建轻量级的J2EE应用.Spring的核心是控制反转(IOC)和面向切面编程(A ...
- 【Spring】Spring框架之Struts2和Spring的优点
Java Web开发使用Structs2和Spring框架的好处 今年我一直在思考web开发里的前后端分离的问题,到了现在也颇有点心得了,随着这个问题的深入,再加以现在公司很多web项目的控制层的技术 ...
- 手写Spring框架,加深对Spring工作机制的理解!
在我们的日常工作中,经常会用到Spring.Spring Boot.Spring Cloud.Struts.Mybatis.Hibernate等开源框架,有了这些框架的诞生,平时的开发工作量也是变得越 ...
- Spring框架系列(3) - 深入浅出Spring核心之控制反转(IOC)
在Spring基础 - Spring简单例子引入Spring的核心中向你展示了IoC的基础含义,同时以此发散了一些IoC相关知识点; 本节将在此基础上进一步解读IOC的含义以及IOC的使用方式.@pd ...
- Spring框架学习之--搭建spring框架
此文介绍搭建一个最最简单的spring框架的步骤 一.创建一个maven项目 二.在pom.xml文件中添加依赖导入spring框架运行需要的相关jar包 注意:在引入jar包之后会出现org.jun ...
- Spring框架[一]——spring概念和ioc入门(ioc操作xml配置文件)
Spring概念 spring是开源的轻量级框架(即不需要依赖其他东西,可用直接使用) spring核心主要两部分 aop:面向切面编程,扩展功能不是修改源代码来实现: ioc:控制反转,比如:有一个 ...
- Java开发工程师(Web方向) - 04.Spring框架 - 第1章.Spring概述
第1章.Spring概述 Spring概述 The Spring Framework is a lightweight solution and a potential one-stop-shop f ...
随机推荐
- 使用GitHub建立个人网站
使用GitHub建立个人网站 1 Git简介 2 为什么使用Github Pages 3 创建Github Pages 3.1 安装git工具. 3.2 两种pages模式 3.3 创建步骤 3.4 ...
- http常见的5个错误
1. HTTP 500错误(内部服务器错误)对对HTTP 500错误的定义已经充分证明了这是一个最常见的HTTP错误. 一般来说,HTTP 500 错误就是web服务器发生内部错误时返回的信息. 例如 ...
- java---servlet与filter的联系与区别
filter是一个可以复用的代码片段,可以用来转换HTTP请求.响应和头信息.Filter不像Servlet,它不能产生一个请求或者响应,它只是修改对某一资源的请求,或者修改从某一的响应. 最近使用插 ...
- PyQt的Layout的比例化分块。
一. QGridLayout: // 列比 第0列与第1列之比为 1:2 layout2p1 -> setColumnStretch(0, 1); layout2p1 -> setColu ...
- 如何过滤php中危险的HTML代码
用php过滤html里可能被利用来引入外部危险内容的代码.有些时候,需要让用户提交html内容,以便丰富用户发布的信息,当然,有些可能造成显示页面布局混乱的代码也在过滤范围内. 以下是引用片段: #用 ...
- Linux服务器的最大内存和CPU数
从网上查了很多资料.总算把linux下的内存和cpu个数搞清楚了.个人觉得使用linux系统的朋友都应该了解下.先公布如下,如有错误,请反馈给我.谢谢!! Linux系统/服务器能够支持的最大内存和C ...
- ssh证书登录(实例详解)
前言 本文基于实际Linux管理工作,实例讲解工作中使用ssh证书登录的实际流程,讲解ssh证书登录的配置原理,基于配置原理,解决实际工作中,windows下使用SecureCRT证书登录的各种问 ...
- 虚拟机中Lvs配置
参考:http://zh.linuxvirtualserver.org/node/272 环境,三台centos 5.2.基于ipvsadm的负载均衡,采用DR方式,负载均衡的服务是web. 内核版本 ...
- 工作表(Worksheet)基本操作应用示例
在编写代码时,经常要引用工作表的名字.知道工作表在工作簿中的位置.增加工作表.删除工作表.复制工作表.移动工作表.重命名工作表,等等.下面介绍与此有关及相关的一些属性和方法示例. [示例04-01]增 ...
- c#基础 第四讲
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...