学习 Spring (一) Spring 介绍
Spring入门篇 学习笔记
Spring 是什么
Spring 是一个轻量级的 IoC (控制反转)和 AOP (面向切面)的容器框架

框架与类库的区别
- 框架一般是封装了逻辑、高内聚的,类库则是松散的工具组合
- 框架专注于某一领域,类库则是更通用的
IoC 与 DI 的关系
- IoC: 控制权的转移,应用程序本身不负责依赖对象的创建和维护,而是由外部容器负责创建和维护(获得依赖对象的过程被反转了)
- DI: 由 IoC 容器在运行期间,动态地将某种依赖关系注入到对象之中
IoC 是一种设计思想,DI 是这种思想的一种实现
示例
添加接口
public interface OneInterface {
String hello(String word);
}
实现接口
public class OneInterfaceImpl implements OneInterface {
public String hello(String word){
return "Word from interface \"OneInterface\": " + word;
}
}
添加配置文件
<?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 id="oneInterface" class="com.karonda.ioc.interfaces.OneInterfaceImpl"></bean>
</beans>
添加测试
@RunWith(BlockJUnit4ClassRunner.class)
public class TestOneInterface extends UnitTestBase {
public TestOneInterface(){
super("classpath*:spring-ioc.xml");
}
@Test
public void testHello(){
OneInterface oif = super.getBean("oneInterface");
System.out.println(oif.hello("world"));
}
}
其中 UnitTestBase 代码:
public class UnitTestBase {
private ClassPathXmlApplicationContext context;
private String springXmlpath;
public UnitTestBase() {}
public UnitTestBase(String springXmlpath) {
this.springXmlpath = springXmlpath;
}
@Before
public void before() {
if (StringUtils.isEmpty(springXmlpath)) {
springXmlpath = "classpath*:spring-*.xml";
}
try {
context = new ClassPathXmlApplicationContext(springXmlpath.split("[,\\s]+"));
context.start();
} catch (BeansException e) {
e.printStackTrace();
}
}
@After
public void after() {
context.destroy();
}
@SuppressWarnings("unchecked")
protected <T extends Object> T getBean(String beanId) {
try {
return (T)context.getBean(beanId);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
protected <T extends Object> T getBean(Class<T> clazz) {
try {
return context.getBean(clazz);
} catch (BeansException e) {
e.printStackTrace();
return null;
}
}
}
源码:learning-spring
Bean 容器初始化
两基础个包
- org.springframework.beans:BeanFactory 提供配置结构和基本功能,加载并初始化 Bean
- org.springframework.context:ApplicationContext 保存了 Bean 对象并在 Spring 中被广泛使用
初始化 ApplicationContext 方式:
- 本地文件
- Classpath
- Web 应用中依赖 servlet 或 Listener
备忘:在 IDEA 中构建 Maven Spring 项目
File --> New --> Project








在 pom.xml 中添加依赖包:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.18.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.18.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
学习 Spring (一) Spring 介绍的更多相关文章
- Spring Framework 官方文档学习(一)介绍
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#overview-maven-bom ...
- Spring第一篇【介绍Spring、引入Spring、Spring六大模块】
前言 前面已经学习了Struts2和Hibernate框架了.接下来学习的是Spring框架-本博文主要是引入Spring框架- Spring介绍 Spring诞生: 创建Spring的目的就是用来替 ...
- spring学习笔记(一) Spring概述
博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书. 强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...
- spring boot入门 -- 介绍和第一个例子
"越来越多的企业选择使用spring boot 开发系统,spring boot牛在什么地方?难不难学?心动不如行动,让我们一起开始学习吧!" 使用Spring boot ,可以轻 ...
- springboot:spring data jpa介绍
转载自:https://www.cnblogs.com/ityouknow/p/5891443.html 在上篇文章springboot(二):web综合开发中简单介绍了一下spring data j ...
- spring boot(五)Spring data jpa介绍
在上篇文章springboot(二):web综合开发中简单介绍了一下spring data jpa的基础性使用,这篇文章将更加全面的介绍spring data jpa 常见用法以及注意事项 使用spr ...
- Spring实战第八章学习笔记————使用Spring Web Flow
Spring实战第八章学习笔记----使用Spring Web Flow Spring Web Flow是一个Web框架,它适用于元素按规定流程运行的程序. 其实我们可以使用任何WEB框架写流程化的应 ...
- Spring实战第五章学习笔记————构建Spring Web应用程序
Spring实战第五章学习笔记----构建Spring Web应用程序 Spring MVC基于模型-视图-控制器(Model-View-Controller)模式实现,它能够构建像Spring框架那 ...
- Spring Framework简单介绍
Spring Framework 学习java编程不知不觉已经三年时间了,開始的时候,总是喜欢看着视频,然后按部就班的敲打着键盘,每当系统正常执行后.心里乐开了花.最開始的时候,所有的代 ...
- Java之Spring Cloud概念介绍(非原创)
文章大纲 一.理解微服务二.Spring Cloud知识介绍三.Spring Cloud全家桶四.参考资料下载五.参考文章 一.理解微服务 我们通过软件架构演进过程来理解什么是微服务,软件架构的发 ...
随机推荐
- 编程&学习总结格式
编程&学习总结格式 一.本周完成的作业: 题目1.A乘以B 题目内容描述:看我没骗你吧 -- 这是一道你可以在10秒内完成的题:给定两个绝对值不超过100的整数A和B,输出A乘以B的值. 1) ...
- 一:Neutron实现网络虚拟化
一 云计算时代数据中心物理网络的问题 数据中心虚拟化成为了趋势,最典型的场景莫过于:对数据中心的服务器进行虚拟化,来提高资源利用率,同时降低单位能耗. 但是,随着数据中心虚拟化程度的不断提高.虚拟化服 ...
- Java多线程学习(四)---控制线程
控制线程 摘要: Java的线程支持提供了一些便捷的工具方法,通过这些便捷的工具方法可以很好地控制线程的执行 1. join线程控制,让一个线程等待另一个线程完成的方法 2. 后台线程,又称为守护线程 ...
- 面试 10:玩转 Java 选择和插入排序,附冒泡最终源码
昨天给大家讲解了 Java 玩转冒泡排序,大家一定觉得并没有什么难度吧,不知道大佬们玩转了吗?不知道大家有没有多加思考,实际上在我们最后的一种思路上,还可以再继续改进. 我们先看看昨天最终版本的代码. ...
- python全栈开发慕课网
前端 web框架: flask:简单.轻量.灵活性大 (官网,stck overflowa); 目录结构:配置,发布,资源,日志,测试... 前后端协作:整体发布,前后端分离发布 django:简单, ...
- JS 作用域及作用域链
一.作用域 在 Javascript 中,作用域分为 全局作用域 和 函数作用域 全局作用域: 代码在程序的任何地方都能被访问,window 对象的内置属性都拥有全局作用域. 函数作用域: 在固定的代 ...
- c++入门之字符相关入门
先上代码: # include "iostream" # include "string" //# define BYTE char//注意,这里没有分号,且# ...
- Less or Equal CodeForces - 977C (sort+细节)
You are given a sequence of integers of length nn and integer number kk. You should print any intege ...
- WinForm 进度条
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- Django之事务
Django之事务 事务就是将一组操作捆绑在一起,只有当这一组操作全部都成功以后这个事务才算成功;当这组操作中有任何一个没有操作成功,则这个操作就会回滚,回到操作之前的状态. 其中牵扯到向数据库中写数 ...