Spring入門(通俗易懂)

前言:網上的框架學習教程千千萬,所以我通過查看別人理解的,以及一些教程總結了我所掌握的spring的知識點,總的來說就是三個知識點,IOC、DI和AOP的使用,所以並沒有那麼複雜的理論知識點,如果有錯的地方接受大佬們指點!
首先:簡單瞭解一下Spring是個什麼東西!
Spring是一個輕量級控制反轉(IOC)和麪向切面(AOP)的容器框架,主要是爲了解決企業應用開發的複雜性而誕生的。
現在我們來簡單實現一下通過Spring輸出HelloWorld:
1、新建一個項目,導入Spring的核心依賴包
2、創建HelloWorld.java文件

package com.java.bean;
public class HelloWorld {

   public void hellowrld(){
      System.out.println("HelloWorld!");
   }

}

3、創建Spring的配置文件,程序就是通過配置文件作爲耦合合 bean 的耦合類,這是一個xml文件,取名爲spring.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"     xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 配置service
<bean> 配置需要創建的對象
id :用於之後從spring容器獲得實例時使用的
class :需要創建實例的全限定類名-->
<bean id="hello" class="com.java.bean.HelloWorld"></bean>
</beans>

3、創建測試類

package com.java.test;
public class TestHelloWorld {
 
public static void main(String[] args) {
	//加載spring配置文件
	ApplicationContext con = new ClassPathXmlApplicationContext("spring.xml");
	//通過bean的id獲取需要創建的對象
	HelloWorld test= (HelloWorld)con.getBean("hello");
	test.hellowrld();
 
}
}

一個簡單的spring實例就這樣完成了,但是這樣還沒能完全體現出來他的優點,所以我們接下來需要做的就是Spring的第一個概念,ICO容器。

IOC:控制反轉,把對象的創建以及各個對象間的關係還有對象的銷燬交給Spring容器去管理,在實際代碼中無需去手動new 對象,只要從spring容器中拿對象來使用。
我們就先膚淺的理解爲不用去new一個對象。

Spring 提供了以下兩種不同類型的容器:
Spring BeanFactory 容器:它是最簡單的容器,給 DI 提供了基本的支持
Spring ApplicationContext 容器:該容器添加了更多的企業特定的功能,當然我一般都是用這個。

IOC的實例,其實就是剛剛HelloWorld的實例,IOC就是通過配置文件,把我要需要創建的對象,也就是new的對象放到IOC容器裏面,至於怎麼放,就是我們在配置文件裏面寫的

<bean id="hello" class="com.java.bean.HelloWorld"></bean>

然後怎麼從這個容器裏面拿出來,就是通過ApplicationContext 的getBean通過bean的id獲取到這個對象。

當然Spring實例化bean一共有三種方式:
第一種就是上面寫的通過class實例化,也就是使用類構造器實例化(默認無參數)。
第二種就是使用靜態工廠方法實例化(簡單工廠模式)。

//factory-method指的是方法名
<bean id="hello" class="com.java.bean.HelloWorld" factory-method="hellowrld">

第三種就是使用實例工廠方法實例化(工廠方法模式)。
這種方式需要結合第一種,寫兩個bean,並且多添加一個工廠類,在工廠類中返回對象(HelloWorld)的實例

package com.java.bean;
public class HelloWorldFactory {
    public HelloWorld getHello() {
        return new HelloWorld ();
    }
}

第一個bean指向工廠類,獲取工廠類的實例,第二個bean則根據第一個bean的工廠類實例,指向factory-method的方法名

<bean class="com.java.HelloWorldFactory " id="HelloWorldFactory "/>
<bean id="helloworld" factory-bean="HelloWorldFactory " factory-method="getHello">

DI:依賴注入,引入IOC容器,利用依賴關係注入的方式,實現對象之間的解耦。簡單來講就是在Spring的配置文件編寫bean的時候添加 property把屬性注入
我們將第一個例子的對象修改一下:

public class HelloWorld {

   private String message;
   public void setMessage(String message){
      this.message  = message;
   }
   public void hellowrld(){
      System.out.println("HelloWorld!"+message);
   }

在配置文件bean中間添加property屬性

 <property name="message" value="你好,世界!"/>  

這是通過屬性的set方法注入,相應的還有按照類型和索引注入,大家瞭解一下就行。
類型注入

<constructor-arg type="int" value=""></constructor-arg>
<constructor-arg type="String" value=""></constructor-arg>

索引注入

<constructor-arg index="0" value=""></constructor-arg>
<constructor-arg index="1" value=""></constructor-arg>

接下來就是AOP:面向切片編程,面向切面編程的目標就是分離關注點,什麼意思呢,就是我們執行一個程序,做一件事,這件事作爲一個切點,這件事的前後就是切面,也就是服務,用來服務這個切點事件的服務。例如:開車出門這件事,就是一個切點,而我們要執行這件事需要的條件就是開車前車庫的門(我們指自動門)開啓,開車出去後車庫門關閉,車庫門的開啓和關閉服務了我們開車出門這件事。
1、定義切點

public class Aop01 {
 
	public void car() {
		System.out.println("開車");
	}
}

2、定義切面

public class Aop02 {
 
	public void befor() {
		System.out.println("車庫開門----這是切點前,調用切點時自動在切點前執行服務");
	}
	public void after() {
		System.out.println("車庫關門----這是切點後,調用切點時自動在切點後執行服務");
	}
}

3、配置文件

<?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:aop="http://www.springframework.org/schema/aop"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
 <!-- 目標對象 (切點)-->
 <bean id="aop01" class="com.spring.aop.Aop01"/>
 <!-- 切面bean(服務) -->
 <bean id="aop02" class="com.spring.aop.Aop02"/>
 <!-- 面向切面編程 -->
 <aop:config>
 <aop:aspect ref="aop02">
  <!-- 定義切點 -->
  <aop:pointcut expression="execution(* *.cat(..))" id="embark"/>
  <!-- 聲明前置通知 (在切點方法被執行前調用)-->
  <aop:before method="befor" pointcut-ref="embark"/>
  <!-- 聲明後置通知 (在切點方法被執行後調用)-->
  <aop:after method="after" pointcut-ref="embark"/>
 </aop:aspect>
 </aop:config>
</beans>

4、測試

public class TestAop {
 
	public static void main(String[] args) {
		ApplicationContext con = new ClassPathXmlApplicationContext("spring.xml");
		Aop01 aop = (Aop01)con.getBean("aop01");
		aop.cat();
	}
}

以上就是Spring入門的一些基礎知識,至於源碼那些什麼的都不是咱現在這個級別接觸的,先掌握使用,然後知道爲什麼,再去考慮源碼實現原理這些。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章