@[toc]javascript
在學習SSM框架中,我建議初學者最好先學Spring框架,其次mybatis接着springMVC,先學mybatis固然也是能夠的,今天咱們就以絕對優雅的姿態闖進Spring世界,繫好安全帶,準備好了嗎,出發了哦!!!咳咳....平時開發接觸最多的估計就是IOC容器,它能夠裝載bean(所謂的bean也就是咱們java中的類,固然也包括servicedao裏面),有了這個機制,咱們就不用在每次使用這個類的時候爲它初始化,不多看到鍵字new。另外spring的aop,事務管理等等都是咱們常常用到的,可見Spring的尤其重要的做用Spring的核心是控制反轉(IoC)和麪向切面(AOP)html
確定有熊dei會問SE/EE開發的一站式框架所謂的一站式是什麼意思,(哼,人類,我早就猜到你會問了) 所謂一站式框架指的是有EE開發的每一層解決方案。java
WEB層 :SpringMVCweb
Service層 :Spring的Bean管理,Spring聲明式事務spring
DAO層 :Spring的Jdbc模板,Spring的ORM模塊設計模式
俗話說,人狠話很少(兄嘚看圖) 數組
Spring3.x、Spring4.x和Spring5.x安全
正所謂,人狠話很少(兄嘚看圖) session
一提及IOC我就想起了武哥對IOC的理解的幾個例子,可謂通俗易懂,很是適合剛入門Spring的兄嘚!有興趣的能夠去了解了解武哥,武哥博客:blog.csdn.net/eson_15mybatis
IOC(Inverse of Control):控制反轉,也能夠稱爲依賴倒置。 控制反轉:將對象的建立權反轉給(交給)Spring。
所謂依賴,從程序的角度看,就是好比A要調用B的方法,那麼A就依賴於B,反正A要用到B,則A依
賴於B。所謂倒置,你必須理解若是不倒置,會怎麼着,由於A必需要有B,才能夠調用B,若是不倒
置,意思就是A主動獲取B的實例:B b = new B(),這就是最簡單的獲取B實例的方法(固然還有各類
設計模式能夠幫助你去得到B的實例,好比工廠、Locator等等),而後你就能夠調用b對象了。所
以,不倒置,意味着A要主動獲取B,才能使用B;到了這裏,就應該明白了倒置的意思了。倒置就是
A要調用B的話,A並不須要主動獲取B,而是由其它人自動將B送上門來。
形象的舉例就是: 一般狀況下,假如你有一天在家裏口渴了,要喝水,那麼你能夠到你小區的小賣部去,告訴他們,你須要一瓶水,而後小賣部給你一瓶水!這原本沒有太大問題,關鍵是若是小賣部很遠,那麼你必須知道:從你家如何到小賣部;小賣部裏是否有你須要的水;你還要考慮是否開着車去;等等等等,也許有太多的問題要考慮了。也就是說,爲了一瓶水,你還可能須要依賴於車等等這些交通工具或別的工具,問題是否是變得複雜了?那麼如何解決這個問題呢? 解決這個問題的方法很簡單:小賣部提供送貨上門服務,凡是小賣部的會員,你只要告知小賣部你須要什麼,小賣部將主動把貨物給你送上門來!這樣一來,你只須要作兩件事情,你就能夠活得更加輕鬆自在: 第一:向小賣部註冊爲會員。 第二:告訴小賣部你須要什麼。
這和Spring的作法很相似!Spring就是小賣部,你就是A對象,水就是B對象 第一:在Spring中聲明一個類:A 第二:告訴Spring,A須要B
假設A是UserAction類,而B是UserService類
<bean id="userService" class="org.leadfar.service.UserService"/>
<bean id="documentService" class="org.leadfar.service.DocumentService"/>
<bean id="orgService" class="org.leadfar.service.OrgService"/>
<bean id="userAction" class="org.leadfar.web.UserAction">
<property name="userService" ref="userService"/>
</bean>
複製代碼
在Spring這個商店(工廠)中,有不少對象/服務:userService,documentService,orgService,也有不少會員:userAction等等,聲明userAction須要userService便可,Spring將經過你給它提供的通道主動把userService送上門來,所以UserAction的代碼示例相似以下所示:
package org.leadfar.web;
public class UserAction{
private UserService userService;
public String login(){
userService.valifyUser(xxx);
}
public void setUserService(UserService userService){
this.userService = userService;
}
}
複製代碼
在這段代碼裏面,你無需本身建立UserService對象(Spring做爲背後無形的手,把UserService對象經過你定義的setUserService()方法把它主動送給了你,這就叫依賴注入!),固然咯,咱們也可使用註解來注入。Spring依賴注入的實現技術是:動態代理
官網下載:spring.io/ 什麼?不會下載?what??? 好吧,已打包好了QAQ:pan.baidu.com/s/18wyE-5SR… 什麼?沒有網盤?what??? 有事請燒香謝謝...
解壓以後,文件說明: docs :Spring的開發規範和API libs :Spring的開發的jar和源碼 schema :Spring的配置文件的約束
建立普通接口,定義一個eat方法
package com.gx.sping;
public interface IUserDao {
public void eat();
}
複製代碼
建立普通實現類
package com.gx.sping;
public class UserDaoimpl implements IUserDao {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println(用戶eat了");
}
}
複製代碼
建立普通接口和類出現的問題: 若是底層的實現切換了,須要修改源代碼,能不能不修改程序源代碼對程序進行擴展? 重點來了,要想不改變源碼,Spring的IOC就能實現!以下圖:Spring的IOC底層實現
一、在classpath下(也就是src)建立一個XML文件
二、文件名最好統一叫applicationContext.xml
三、其xml文件的內容頭爲schema約束
四、約束文件位置在spring的解壓路徑下lspring-framework-4.2.4.RELEASE\docs\spring-framework-reference\html\xsd-configuration.htm
五、不要求xml文件的內容頭可以背出來,但要了解的是你要知道它是怎麼來的
六、xml文件的內容頭添加後,將實現類交給Spring管理
<?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"> <!-- 實現類UserDaoimpl交給Spring管理 --> <bean id="IuserDao" class="com.gx.Ioc.UserDaoimpl" ></bean> </beans>
複製代碼
package com.gx.Ioc;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpingDemo1 {
@Test
public void demo11() {
// 面向接口傳統方式
UserDaoimpl userdao = new UserDaoimpl();
userdao.eat();
}
//Spring的bean管理方式
@Test
public void demo22() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDao userdao = (IUserDao) applicationContext.getBean("IuserDao");
userdao.eat();
}
}
複製代碼
兄嘚,若是測試不成功最好看看兩者是否對應!!!
拉倒吧! 我都很差意思說了.(兄dei,我錯了,是我飄了,呀呀呀,兄dei別打臉鴨QAQ)
可是我依舊是阻止不了你驕傲的心.
那就頂我,讓我感覺感覺你的驕傲!哈哈哈QAQ
IOC不是什麼技術,而是一種設計思想,IOC能指導咱們如何設計出鬆耦合、更優良的程序。傳統應用程序都是由咱們在類內部主動建立依賴對象,從而致使類與類之間高耦合,難於測試;有了IoC容器後,把建立和查找依賴對象的控制權交給了Spring容器,由容器進行注入組合對象,因此對象與對象之間是鬆散耦合,這樣利於功能複用,更重要的是使得程序的整個體系結構變得很是靈活。 IOC:控制反轉,將對象的建立權反轉給了Spring。 DI:依賴注入,前提必須有IOC的環境,Spring管理這個類的時候將類的依賴的屬性注入(設置)進來。好比說下面講到的Spring的屬性注入其實就是典型的DI
所謂繼承:is a
Class A{
}
Class B extends A{
}
複製代碼
所謂依賴:
Class A{
}
Class B{
public void xxx(A a){
}
}
複製代碼
所謂聚合:has a
BeanFactory:調用getBean的時候,纔會生成類的實例。
ApplicationContext:加載配置文件的時候,就會將Spring管理的類都實例化。 ApplicationContext有兩個實現類 一、ClassPathXmlApplicationContext :加載類路徑下的配置文件 二、FileSystemXmlApplicationContext :加載文件系統下的配置文件
在XML文件中要使用各類標籤來給spring進行配置,博主我這佩奇腦殼怎麼可能記住spring中全部的標籤呢,不怕不怕,博主我會配置XML的提示配置QAQ,會了這一招就算兄dei你是喬治腦殼也不用擔憂(再說了我看兄dei各各英俊瀟灑,玉樹臨風,聰明絕頂...咳咳,暴露了暴露了)
id :使用了約束中的惟一約束。裏面不能出現特殊字符的。上面說起到了要與getbean參數值對應
name :沒有使用約束中的惟一約束(理論上能夠出現重複的,可是實際開發不能出現的)。裏面能夠出現特殊字符。
init-method :Bean被初始化的時候執行的方法 destroy-method :Bean被銷燬的時候執行的方法(Bean是單例建立,工廠關閉)
scope屬性 :Bean的做用範圍
scope屬性值以下(主要用的是前兩者) singleton :scope屬性的默認值,Spring會採用單例模式建立這個對象。 prototype :多例模式。(Struts2和Spring整合必定會用到) request :應用在web項目中,Spring建立這個類之後,將這個類存入到request範圍中。 session :應用在web項目中,Spring建立這個類之後,將這個類存入到session範圍中。 globalsession :應用在web項目中,必須在porlet環境下使用。可是若是沒有這種環境,相對於session。
首先,建立幾個普通類
com.gx.spring.demo.Car
public class Car {
private String name;
private Double price;
public Car(String name, Double price) {
super();
this.name = name;
this.price = price;
}
@Override
public String toString() {
return "Car [name=" + name + ", price=" + price + "]";
}
}
複製代碼
com.gx.spring.demo.Car2
/** * 用做set方法的屬性注入類 */
public class Car2 {
private String name;
private Double price;
public void setName(String name) {
this.name = name;
}
public void setPrice(Double price) {
this.price = price;
}
@Override
public String toString() {
return "Car2 [name=" + name + ", price=" + price + "]";
}
}
複製代碼
com.gx.spring.demo.Person
/** * 用做set方法的對象屬性注入類 */
public class Person {
private String name;
private Car2 car2;
public void setName(String name) {
this.name = name;
}
public void setCar2(Car2 car2) {
this.car2 = car2;
}
@Override
public String toString() {
return "Employee [name=" + name + ", car2=" + car2 + "]";
}
}
複製代碼
構造方法的屬性注入 constructor-arg 標籤用於配置構造方法的屬性注入 name :參數的名稱 value:設置普通數據 ref:引用數據,通常是另外一個bean id值
固然了,構造方法的方式的屬性注入也支持對象屬性的注入,標籤中對應屬性也是ref 若是隻有一個有參數的構造方法而且參數類型與注入的bean類型匹配,那就會注入到該構造方法中
applicationContext.xml中配置:
<!-- 構造方法的方式 -->
<bean id="car" class="com.gx.spring.demo.Car">
<constructor-arg name="name" value="瑪莎拉蒂"/>
<constructor-arg name="price" value="800000"/>
</bean>
複製代碼
測試方法:
/** * 構造方法方式的普通屬性注入方法 */
public void demo1(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car car = (Car) applicationContext.getBean("car");
System.out.println(car);
}
複製代碼
Set方法的普通屬性注入 property 標籤用於配置Set方法的屬性注入 name :參數的名稱 value:設置普通數據 ref:引用數據,通常是另外一個bean id值
applicationContext.xml中配置:
<!-- set方法的方式 -->
<bean id="car2" class="com.gx.spring.demo.Car2">
<property name="name" value="法拉利黃金跑車"/>
<property name="price" value="10000000"/>
</bean>
複製代碼
測試方法:
@Test
/** * set方法方式的屬性注入 */
public void demo2(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Car2 car2 = (Car2) applicationContext.getBean("car2");
System.out.println(car2);
}
複製代碼
Set方法設置對象類型的屬性 applicationContext.xml中配置:
<!-- set方法注入對象類型的屬性 -->
<bean id="Person" class="com.gx.spring.demo.Person">
<!-- value:設置普通類型的值,ref:設置其餘的類的id或name-->
<property name="name" value="濤哥"/>
<property name="car2" ref="car2"/>
</bean>
複製代碼
測試方法:
@Test
/** * set方法注入對象類型 */
public void demo3(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person= (Person) applicationContext.getBean("Person");
System.out.println(person);
}
複製代碼
@Component (做用在類上通用:組件) @Component(「userService」)至關於< bean id=」userService」 class=」...」>
衍生: @Controller Web層 @Service 業務層 @Repository 持久層 這三個註解是爲了讓標註類自己的用途清晰
屬性注入的註解 ( 能夠沒有set方法) 普通類型屬性:@Value
對象類型屬性:@Resource (對應bean中的id)= @Autowired(類型)+ @Qualifier(名稱)
額,初學框架,註解二字可能對於大部分熊dei來講,太過於陌生,註解其實就是在一個類、方法、屬性上,使用@註解名稱,就好比是咱們最熟悉的接實現口中的方法默認會有一個 @Override (熊dei,這樣理解能接受?)
Spring3.x註解的jar包 在Spring3.x的版本中,使用註解開發,只須要 spring核心基礎四包外 + log4j包 + 1個依賴包 便可
所謂約束就是就是就是約束啦(搽汗),其中bean約束是最基本的約束!(下圖也能夠看出)
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
</beans>
複製代碼
public interface UserDao {
public void sayHello();
}
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring...");
} }
複製代碼
Spring的註解開發:組件掃描(不使用類上註解的時候能夠不用組件掃描) 使用註解方式,須要開啓組件掃描< context:component-scan base-package=直接包名或者包名.類名/>,固然開發中通常都是base-package=包名,畢竟這樣能夠掃描整個包,方便開發 Spring 的註解開發:組件掃描(類上註解: 能夠直接使用屬性注入的註解)
<!-- Spring 的註解開發:組件掃描(類上註解: 能夠直接使用屬性注入的註解) -->
<context:component-scan base-package="com.gx.spring.demo1"/> 複製代碼
一、使用類上註解方式@Component(value=「userDao」),至關於< bean id="userDao class="com.gx.類名">< /bean> 固然value屬性名能夠省去直接@Component("userDao"),固然@Component(「value值任意寫建議取的要有意義」) 二、註解方式能夠沒有set方法
@Component(value="userDao") //至關於配置了<bean id="userDao" class="com.gx.UserDaoImpl "></bean>
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring Annotation...");
} }
複製代碼
@Test
public void demo3() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = (UserDao) applicationContext.getBean("userDao");
userDao.sayHello();
}
複製代碼
經過引入p名稱空間完成屬性的注入: 寫法: 普通屬性 p:屬性名=」值」 對象屬性 p:屬性名-ref=」值」
P名稱空間的約束引入
SpEL:Spring Expression Language,Spring的表達式語言。 語法: #{SpEL}
集合類型屬性配置: 集合的注入都是在< property>標籤中添加子標籤 數組:< array > List:< list > Set:< set > Map:< map > ,map存放k/v 鍵值對,使用描述 Properties:< props> < prop key="">< /prop>
普通數據:< value > 引用數據:< ref >
<!-- Spring的集合屬性的注入============================ -->
<!-- 注入數組類型 -->
<bean id="collectionBean" class="com.gx.spring.demo.CollectionBean">
<!-- 數組類型 -->
<property name="arrs">
<list>
<value>天才</value>
<value>王二</value>
<value>冠希</value>
</list>
</property>
<!-- 注入list集合 -->
<property name="list">
<list>
<value>李兵</value>
<value>趙如何</value>
<value>鄧鳳</value>
</list>
</property>
<!-- 注入set集合 -->
<property name="set">
<set>
<value>aaa</value>
<value>bbb</value>
<value>ccc</value>
</set>
</property>
<!-- 注入Map集合 -->
<property name="map">
<map>
<entry key="aaa" value="111"/>
<entry key="bbb" value="222"/>
<entry key="ccc" value="333"/>
</map>
</property>
</bean>
複製代碼
分模塊配置: 在加載配置文件的時候,加載多個,沒錯,這就是傳說中的騷操做,堪稱開掛級別的操做(固然,這是能夠的不是開掛)
【Spring框架學習二】Spring的AOP通俗理解以及AOP的入門開發(哎哎,別打..別打..別打臉....)
若是本文對你有一點點幫助,就請點個讚唄,手留餘香,謝謝!
最後,歡迎各位關注個人公衆號,一塊兒探討技術,嚮往技術,追求技術...