学习 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全家桶四.参考资料下载五.参考文章 一.理解微服务 我们通过软件架构演进过程来理解什么是微服务,软件架构的发 ...
随机推荐
- CF617E XOR and Favorite Number
CF617E XOR and Favorite Number 已知一个序列 \(a_1,\ a_2,\ \cdots,\ a_n\) 和 \(k\) ,\(m\) 次询问给出 \(l,\ r\) ,求 ...
- glance系列二:glance部署及操作
一 简单架构图示参考 更新中... 二 部署glance yum install memcached python-memcachedsystemctl enable memcached.servic ...
- Unity编辑器:基于NGUI的引用检测工具
这里共享一个基于NGUI的引用检测工具.工具包括几个部分:Atlas/Sprite的引用查找:字库引用查找:UITexture引用查找:Component查找: 代码就不多介绍了,文章底部提供源码下载 ...
- CF1103D Professional layer 状压DP
传送门 首先对于所有数求gcd并求出这个gcd含有的质因子,那么在所有数中,只有这一些质因子会对答案产生影响,而且对于所有的数,每一个质因子只会在一个数中被删去. 质因子数量不会超过\(11\),所以 ...
- Python股票分析系列——自动获取标普500股票列表.p5
该系列视频已经搬运至bilibili: 点击查看 欢迎来到Python for Finance教程系列的第5部分.在本教程和接下来的几节中,我们将着手研究如何为更多公司提供大量的定价信息,以及如何一次 ...
- [2017BUAA软工助教]个人项目准备工作
BUAA软工个人项目准备工作 零.注册Github个人账号(你不会没有吧..) 这是Git的使用教程: http://www.cnblogs.com/schaepher/p/5561193.html ...
- JS实用小函数 数据是否合法或存在 获取当前日期时间
1.判断数据是否合法或存在 //判断数据是否合法或存在 function isNotNull(data) { if(data === "" || data === undefine ...
- 学习笔记:filter_var()函数
PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤 filter_var() 函数通过指定的过滤器过滤变量. 如果成功,则返回已过滤的数据,如果失败,则返回 false. 语法 f ...
- Oracle循环
--无条件循环 declare v_num ; begin loop dbms_output.put_line(v_num); v_num:; ; end loop; end; --有条件循环 dec ...
- mybatis事务管理机制详解
1.mybatis事务的配置和使用 mybatis事务有两种使用方式: (a):使用JDBC的事务管理机制:即使用java.Sql.Connection对象完成对事务的提交,回滚和关闭操作. (b): ...