Spring4.0 學習筆記(一)
阿新 • • 發佈:2018-09-30
接受 主動 public sql drive 對象 ans article 種類 版權聲明:本文為博主原創文章,未經博主允許不得轉載。 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="name"