1、Spring簡介
•Spring 是一個 IOC(DI) 和 AOP 容器框架
Spring的模塊:
2、Eclipse安裝Spring插件
Eclipse工具想要更好更快的開發Spring,需要安裝一個插件:SPRING TOOL SUITE ,利用該插件可以更方便的在 Eclipse 平臺上開發基於 Spring 的應用。
本次我使用的是InteliJ IDEA2019版作爲開發工具,所以跳過了上述插件安裝。
3、入門程序HelloWorld
3.1 環境準備
1、新建一個普通的Java工程:
File——》new ——》Project——》Java——》JavaEE,然後一路next知道完成。
2、引入依賴的jar包
本次需要用到的jar包:spring-core-4.0.0.RELEASE.jar,spring-beans-4.0.0.RELEASE.jar,spring-context-4.0.0.RELEASE.jar,spring-expression-4.0.0.RELEASE.jar和commons-logging-1.1.1.jar。
我們首先要把這幾個包放到同一個目錄下面,然後在IDEA中操作:
File——》Project Structure...——》Libraries——》點擊+號,選擇Java——》然後選擇我們剛纔存放jar包的目錄即可。
我們就可以在工程的 External Libraries下面看到我們剛纔引的jar包的目錄,裏面有我們需要的jar包了。
3、創建資源文件夾
在工程的根目錄下面,創建一個conf的資源文件夾,用來存放一些Spring的配置文件,以及其他配置文件(比如日誌文件)。
右鍵工程名——》new——》Directory——》文件名稱爲:conf
右鍵conf目錄——》Mark Directory as——》Resources Root,那麼這個conf就變成了資源目錄了。
在conf目錄下面創建一個spring目錄,用來存放Spring的主配置文件:applicationContext.xml
3.2 代碼實現
3.2.1 創建HelloWorld
在src下面創建包:com.spring.beans,然後創建HelloWorld:
package com.spring.beans;
public class HelloWorld {
private String name;
public void setName(String name) {
this.name = name;
}
public void sayHello(){
System.out.println("Hello "+name);
}
}
3.2.2 配置文件applicationContext.xml
在conf/spring下面創建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:util="http://www.springframework.org/schema/util"
xmlns:p="http://www.springframework.org/schema/p"
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-4.0.xsd">
<!--告訴Spring去創建一個變量爲helloWorld的對象-->
<bean id="helloWorld" class="com.spring.beans.HelloWorld">
<!--調用HelloWorld的setName方法給name屬性賦值-->
<!--這兩個操作都在Spring初始化的時候進行-->
<property name="name" value="Spring"></property>
</bean>
</beans>
3.2.3 測試
package com.spring.test;
import com.spring.beans.HelloWorld;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
public static void main(String[] args) {
//通過配置文件創建Spring的IOC容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
//從IOC容器中,得到helloWorld對象
HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");
helloWorld.sayHello();//調用方法
}
}
打印結果:Hello Spring
通過入門程序,我們發現,之前我們需要創建對象,給對象的屬性賦值,現在的這些操作都是Spring幫我們做了,在通過配置文件創建Spring容器的時候,就會初始化配置文件裏面所有的實例對象,並且賦值。這就是SPring的IOC和DI。
4、Spring中Bean的配置
4.1 IOC和DI概述
以前我們要創建一個對象,都是通過new 來獲取,而現在,只要在配置文件配置了類全名,Spring就會通過發射幫我們把這個對象創建出來,存儲到IOC容器中,到時候我們直接從容器裏面取,其實這就是IOC;
同樣,以前的時候,對象裏面的屬性賦值,需要提高set方法,我們手動的調用set方法給對象屬性賦值,但是現在IOC容器可以把我們需要的值按照配置信息,自動注入到我們的對象中。這就是DI。
4.2 Bean配置概述
4.3 Spring容器
4.3.1 ApplicationContext
4.4 依賴注入
Spring支持3中注入方式:
屬性注入(settre方法);
構造器注入;
工廠方法注入(很少用,不推薦)。
4.4.1 屬性注入
<!--告訴Spring去創建一個變量爲helloWorld的對象-->
<bean id="helloWorld" class="com.spring.beans.HelloWorld">
<!--調用HelloWorld的setName方法給name屬性賦值-->
<!--這兩個操作都在Spring初始化的時候進行-->
<property name="name" value="Spring"></property>
</bean>
4.4.2 構造器注入
4.5 不同類型的值的注入
4.5.1 字面值
4.5.2 引入其他Bean
<!-- 聲明使用內部 bean -->
<bean id="service2" class="com.atguigu.spring.ref.Service">
<property name="dao">
<!-- 內部 bean, 類似於匿名內部類對象. 不能被外部的 bean 來引用, 也沒有必要設置 id 屬性 -->
<bean class="com.atguigu.spring.ref.Dao">
<property name="dataSource" value="c3p0"></property>
</bean>
</property>
</bean>
4.5.3 集合屬性注入
<!-- 聲明集合類型的 bean -->
<util:list id="cars">
<ref bean="car"/>
<ref bean="car2"/>
</util:list>
<bean id="user2" class="com.atguigu.spring.helloworld.User">
<property name="userName" value="Rose"></property>
<!-- 引用外部聲明的 list -->
<property name="cars" ref="cars"></property>
</bean>
4.6 XML 配置裏的 Bean 自動裝配
缺點:
4.7 Spring Bean的作用域
prototype:原型的, 每次調用 getBean 方法都會返回一個新的 bean. 且在第一次調用 getBean 方法時才創建實例
singleton:單例的, 每次調用 getBean 方法都會返回同一個 bean. 且在 IOC 容器初始化時即創建 bean 的實例. 默認值
4.8 使用外部配置文件
需要到context命名空間,在配置文件中引入如下配置:
<!-- 導入外部的資源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
<property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
</bean>
外部文件配置:db.properties
jdbc.user=root
jdbc.password=1230
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///test
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
4.9 Spring表達式語言:SPEL
<!-- 測試 SpEL: 可以爲屬性進行動態的賦值(瞭解) -->
<bean id="girl" class="com.atguigu.spring.helloworld.User">
<property name="userName" value="周迅"></property>
</bean>
<bean id="boy" class="com.atguigu.spring.helloworld.User" init-method="init" destroy-method="destroy">
<property name="userName" value="高勝遠"></property>
<property name="wifeName" value="#{girl.userName}"></property>
</bean>
4.10 Spring Bean的生命週期
在 Bean 的聲明裏設置 init-method 和 destroy-method 屬性, 爲 Bean 指定初始化和銷燬方法
4.10.1 創建 Bean 後置處理器
@Override
//bean:Bean實例本身
//beanName:IOC容器配置的bean的名字
//返回值:是實際上返回給用戶的那個bean,注意:可以在下面的兩個方法中修改返回的bean,甚至返回一個新的bean。
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return null;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return null;
}
添加 Bean 後置處理器後 Bean 的生命週期:
後置處理器的配置:
<!-- 配置 bean 後置處理器: 不需要配置 id 屬性, IOC 容器會識別到他是一個 bean 後置處理器, 並調用其方法 -->
<bean class="com.atguigu.spring.ref.MyBeanPostProcessor"></bean>
4.11 實現 FactoryBean 接口在 Spring IOC 容器中配置 Bean
package org.springframework.beans.factory;
public interface FactoryBean<T> {
T getObject() throws Exception;
Class<?> getObjectType();
boolean isSingleton();
}
5、Spring註解使用
5.1 基於註解的方式配置Bean
<!-- 配置自動掃描的包: 需要加入 aop 對應的 jar 包 -->
<context:component-scan base-package="com.atguigu.spring.annotation"
resource-pattern="repository/*.class"
></context:component-scan>
–<context:include-filter> 子節點表示要包含的目標類
5.2 註解來進行組件裝配
<context:component-scan> 元素還會自動註冊 AutowiredAnnotationBeanPostProcessor (Bean的後置處理器)實例, 該實例可以自動裝配具有 @Autowired 和 @Resource 、@Inject註解的屬性
5.2.1 @Autowired 自動裝配 Bean
5.2.2 @Resource 或 @Inject 自動裝配 Bean