Spring4.0 学习笔记(一)
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baidu_37181928/article/details/80020702
Spring 是什么
- Spring 是一个开源框架.
- Spring 为简化企业级应用开发而生. 使用 Spring 可以使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.
- Spring 是一个 IOC(DI) 和 AOP 容器框架.
具体描述Spring
- 轻量级:Spring 是非侵入性的 - 基于 Spring 开发的应用中的对象可以不依赖于 Spring 的 API
- 依赖注入(DI — dependency injection、IOC)
- 面向切面编程(AOP — aspect oriented programming)
- 容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期
- 框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在 Spring 中可以使用 XML 和 Java 注解组合这些对象
- 一站式:在 IOC 和 AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库 (实际上 Spring 自身也提供了展现层的
SpringMVC 和 持久层的 Spring JDBC)
IOC 和 DI
- IOC(Inversion of Control):其思想是反转资源获取的方向. 传统的资源查找方式要求组件向容器发起请求查找资源.作为回应, 容器适时的返回资源. 而应用了 IOC 之后, 则是容器主动地将资源推送给它所管理的组件,组件所要做的仅是选择一种合适的方式来接受资源. 这种行为也被称为查找的被动形式
- DI(Dependency Injection) — IOC 的另一种表述方式:即组件以一些预先定义好的方式(例如: setter 方法)接受来自如容器的资源注入. 相对于 IOC 而言,这种表述更直接
IOC 容器
- 在 Spring IOC 容器读取 Bean 配置创建 Bean 实例之前, 必须对它进行实例化. 只有在容器实例化后, 才可以从 IOC 容器里获取 Bean 实例并使用.
- Spring 提供了两种类型的 IOC 容器实现.
- BeanFactory: IOC 容器的基本实现.
- ApplicationContext: 提供了更多的高级特性. 是 BeanFactory 的子接口.
- BeanFactory 是 Spring 框架的基础设施,面向 Spring 本身;ApplicationContext 面向使用 Spring 框架的开发者,几乎所有的应用场合都直接使用 ApplicationContext 而非底层的 BeanFactory
- 无论使用何种方式, 配置文件是相同的.
开发环境的搭建
需要 jar 包
commons-logging-1.1.1.jar
spring-beans-4.0.2.RELEASE.jar
spring-context-4.0.2.RELEASE.jar
spring-core-4.0.2.RELEASE.jar
spring-expression-4.0.2.RELEASE.jar
Spring bean 的配置细节
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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<!--
配置 bean
class: bean 的全类名, 通过反射的方法在 IOC 容器中创建 Bean, 使用必须要求有无参构造函数
id: 标识容器中的 bean, id 唯一
-->
<bean id="hellospring" class="com.anqi.hello.HelloSpring">
<property name="name" value="anqi"></property>
</bean>
<!-- 通过构造方法来配置 bean 的属性 -->
<bean id="car" class="com.anqi.hello.Car">
<constructor-arg value="Audi" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="300000" index="2"></constructor-arg>
</bean>
<!-- 使用构造器注入属性值可以指定参数的位置和参数的类型! 以区分重载的构造器! -->
<bean id="car2" class="com.anqi.hello.Car">
<constructor-arg value="Baoma" index="0"></constructor-arg>
<constructor-arg value="Shanghai" index="1"></constructor-arg>
<constructor-arg value="240" type="int"></constructor-arg>
</bean>
<bean id="car3" class="com.anqi.hello.Car">
<constructor-arg value="Baoma3" index="0"></constructor-arg>
<!-- 如果字面值包含特殊字符 可以用<![CDATA[]]> 包裹起来 -->
<!-- 字面值就是基本数据类型及其包装类, 以及 String 类 -->
<!-- 属性值也可以使用 value 节点来进行配置 -->
<constructor-arg index="1">
<value><![CDATA[<shanghai3]]></value>
</constructor-arg>
<constructor-arg value="2403" type="int"></constructor-arg>
</bean>
<bean id = "person" class = "com.anqi.hello.Person">
<property name="name" value="Anqi"></property>
<property name="age">
<value>25</value>
</property>
<!-- <property name="car" ref="car2"></property> 与下面代码效果一致-->
<!--
<property name="car">
<ref bean="car2"/>
</property>
-->
<!-- 内部 bean 不能被外部引用, 只能在内部使用-->
<property name="car">
<bean class="com.anqi.hello.Car">
<constructor-arg value="fute"></constructor-arg>
<constructor-arg value="Beijing"></constructor-arg>
<constructor-arg value="28000" type="double"></constructor-arg>
</bean>
</property>
</bean>
<bean id="person2" class="com.anqi.hello.Person">
<constructor-arg value="LiChen"></constructor-arg>
<constructor-arg value="18"></constructor-arg>
<!-- 测试赋值 NULL 值 -->
<!--<constructor-arg><null/></constructor-arg> -->
<constructor-arg name="car" ref="car2"></constructor-arg>
<!-- 为级联属性赋值,必须有对应的 setter 方法 -->
<!-- 注意 : 属性需要先初始化后才可以为级联属性赋值, 否则会有异常 -->
<property name="car.maxSpeed" value="220"></property>
</bean>
<bean id="person3" class="com.anqi.hello.collections.Person">
<property name=