Spring相关知识点(全集)

1Spring概述

1.1Spring概述

1.spring是一个开源框架。

2.spring为简化企业级开发而生,使用spring,Javabean就可以实现很多以前要考EJB才能实现的功能。同样的功能,在EJB中要通过繁琐的配置和复杂的代码才能实现,而在spring中去表现的优雅和简介。

3.spring是一个IOC(DI)和AOP容器框架。

4.spring的优良特性

  1. 非侵入式:基于spring开发的应用中的对象可以不依赖与spring的API
  2. 依赖注入:DI--Dependency Injection ,控制反转IOC最简单的实现
  3. 面向切面编程:Aspect Oriented Programming --AOP
  4. 容器:spring是一个容器,因为它包含并且管理应用对象的生命周期
  5. 组件化:spring实现了使用简单的配置组件组成一个复杂的应用,在spring中可以使用XML方式和java注解组合这些对象
  6. 一站式:在IOC和AOP的基础上可以整合各种企业应用的开原框架和优秀的第三方类库(实际上spring自身也提供了表述层的springMVC和持久层的springJDBC)
  7. spring模块

 

1.2 安装 Spring 插件【了解】
1) 插件包: springsource-tool-suite-3.4.0.RELEASE-e4.3.1-updatesite.zip
1.3 搭建 Spring 运行时环境
1) 加入 JAR
① Spring 自身 JAR 包: spring-framework-4.0.0.RELEASE\libs 目录下
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE.jar
② commons-logging-1.1.1.jar
2) Spring Tool Suite 工具中通过如下步骤创建 Spring 的配置文件
① File->New->Spring Bean Configuration File
为文件取名字 例如: applicationContext.xml

1.4 HelloWorld
1)

 

目标:使用 Spring 创建对象,为属性赋值
2) 创建 Student
3) 创建 Spring 配置文件
<!-- 使用 bean 元素定义一个由 IOC 容器创建的对象 -->
<!-- class 属性指定用于创建 bean 的全类名 -->
<!-- id 属性指定用于引用 bean 实例的标识 -->
< bean id = "student" class = "com.atguigu.helloworld.bean.Student" >
<!-- 使用 property 子元素为 bean 的属性赋值 -->
< property name = "studentId" value = "1001" />
< property name = "stuName" value = "Tom2015" />
< property name = "age" value = "20" />
</ bean >
4) 测试:通过 Spring IOC 容器创建 Student 类实例
/1. 创建 IOC 容器对象
ApplicationContext iocContainer =
new ClassPathXmlApplicationContext( "helloworld.xml" );
//2. 根据 id 值获取 bean 实例对象
Student student = (Student) iocContainer.getBean( "student" )

2.1 IOC DI

2.1.1 IOC(Inversion of Control):反转控制

在应用程序中的组件需要获取资源时,传统的方式是组件主动的从容器中获取所需 要的资源,在这样的模式下开发人员往往需要知道在具体容器中特定资源的获取方式, 增加了学习成本,同时降低了开发效率。
反转控制的思想完全颠覆了应用程序组件获取资源的传统方式:反转了资源的获取 方向—— 改由容器主动的将资源推送给需要的组件,开发人员不需要知道容器是如何创 建资源对象的,只需要提供接收资源的方式即可,极大的降低了学习成本,提高了开发 的效率。这种行为也称为查找的 被动形式

2.1.2 DI(Dependency Injection):依赖注入

IOC 的另一种表述方式:即组件以一些预先定义好的方式 ( 例如: setter 方法 ) 接受
来自于容器的资源注入。相对于 IOC 而言,这种表述更直接。

2.1.3 IOC 容器在 Spring 中的实现

1 )在通过 IOC 容器读取 Bean 的实例之前,需要先将 IOC 容器本身实例化。
2 Spring 提供了 IOC 容器的两种实现方式
① BeanFactory IOC 容器的基本实现,是 Spring 内部的基础设施,是面向
Spring 本身的,不是提供给开发人员使用的。
② ApplicationContext BeanFactory 的子接口,提供了更多高级特性。面向
Spring 的 使 用 者 , 几 乎 所 有 场 合 都 使 用 ApplicationContext 而 不 是 底 层 的
BeanFactory

2.1.4 ApplicationContext 的主要实现类

1) ClassPathXmlApplicationContext :对应类路径下的 XML 格式的配置文件
2) FileSystemXmlApplicationContext :对应文件系统中的 XML 格式的配置文件
3) 在初始化时就创建单例的 bean ,也可以通过配置的方式指定创建的 Bean 是多实例的。

2.1.5 ConfigurableApplicationContext

1) ApplicationContext 的子接口,包含一些扩展方法
2) refresh() close() ApplicationContext 具有启动、关闭和刷新上下文的能力。

2.1.6 WebApplicationContext

1) 专门为 WEB 应用而准备的,它允许从相对于 WEB 根目录的路径中完成初始化工作

2.2 通过类型获取 bean

1) IOC 容器中获取 bean 时,除了通过 id 值获取,还可以通过 bean 的类型获取。但如
果同一个类型的 bean XML 文件中配置了多个,则获取时会抛出异常,所以同一个类
型的 bean 在容器中必须是唯一的

HelloWorld helloWorld = cxt.getBean(HelloWorld. class);

2)
或者可以使用另外一个重载的方法,同时指定 bean id 值和类型

HelloWorld helloWorld = cxt.getBean(“helloWorld”,HelloWorld. class)

2.3 bean 的属性赋值
2.3.1 依赖注入的方式
1. 通过 bean setXxx() 方法赋值
Hello World 中使用的就是这种方式
2. 通过 bean 的构造器赋值
1) Spring 自动匹配合适的构造器

 

< bean id = "book" class = "com.atguigu.spring.bean.Book" >
< constructor-arg value = "10010" />
< constructor-arg value = "Book01" />
< constructor-arg value = "Author01" />
< constructor-arg value = "20.2" />
</ bean >
2) 通过索引值指定参数位置
< bean id = "book" class = "com.atguigu.spring.bean.Book" >
< constructor-arg value = "10010" index = "0" />
< constructor-arg value = "Book01" index = "1" />
< constructor-arg value = "Author01" index = "2" />
< constructor-arg value = "20.2" index = "3" />
</ bean >
3) 通过类型区分重载的构造器
< bean id = "book" class = "com.atguigu.spring.bean.Book" >
< constructor-arg value = "10010" index = "0" type = "
java.lang.Integer" />
< constructor-arg value = "Book01" index = "1" type = "
java.lang.String" />
< constructor-arg value = "Author01" index = "2" type = "
java.lang.String" />
< constructor-arg value = "20.2" index = "3" type = "
java.lang.Double" />
</ bean >
2.3.2 p 名称空间
  • 2
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值