Spring學習手札(一)

 

Spring能作什麼前端

1. 能根據配置文件建立及組裝對象之間的依賴關係;java

2. 面向切面編程,能幫助咱們無耦合的實現日誌記錄,性能統計,安全控制等;web

3. 提供第三方數據訪問框架(如Hibernate),並且本身也提供了一套JDBC訪問模板方便訪問數據庫;spring

4. 很是簡單的管理數據庫事務;數據庫

5. 集成第三方Web框架(如Struts1/2),並且自己有Spring MVC方便搭建web層編程

6. 與Java EE整合,與更多技術整合(好比緩存)緩存

 

Spring的特點安全

1. 方便解耦,簡化開發前端框架

 Spring IoC完成對象建立,依賴關係維護等服務器

2. AOP的支持

 Spring提供面向切面編程,實現攔截,監控等

3. 聲明式事務管理

 只需經過配置完成對事務的管理,而無需手動操做

4. 方便測試

 支持Junit4,能夠經過註解方便測試

5. 方便集成其餘框架

 一直在提供並完善各類框架(如Struts,MyBatis,Hibernate等)

6. 下降開發難度,一站式開發項目

 提供各類優秀前端框架,以及後臺框架,一站式開發網站

 

Spring的定義

先看看百度百科的解釋

Spring框架是一個輕量級的DI/IoC和AOP容器的開源框架;

Spring爲了解決企業應用開發的複雜性而建立的;

Spring的用途不只限於服務器端的開發,只是爲了簡化Java開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用均可以從Spring中受益。Spring的核心是控制反轉(IoC)和麪向切面(AOP)簡單來講,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源框架。

Spring使用基本的JavaBean來完成開發。

 

先了解一下

Spring Boot  經過整合通用實踐,更加自動,智能的依賴管理,Spring Boot提供了各類典型應用系統的通用開發基礎,因此它是以應用爲中心的一個框架集合。

Spring Cloud  可認爲是在Spring Boot的升級版,提供了構建分佈式系統的通用模塊,包含服務發現和服務註冊,分佈式配置管理,負載均衡,分佈式診斷等各類子系統,是構建微服務的一把好手

還有針對特定領域的Spring Security,Spring Data等

 

Spring框架結構

 

  • Data Access/Integration層包含有JDBC、ORM、OXM、JMS和Transaction模塊。
  • Web層包含了Web、Web-Servlet、WebSocket、Web-Porlet模塊。
  • AOP模塊提供了一個符合AOP聯盟標準的面向切面編程的實現。
  • Core Container(核心容器):包含有Beans、Core、Context和SpEL模塊。
  • Test模塊支持使用JUnit和TestNG對Spring組件進行測試。

 

Bean規範

每個類實現了Bean的規範纔能有Spring來接管

    必須有個公有類

    有無參數構造函數

    用公共方法暴露內部成員屬性(getter,setter )

Bean的生命週期

   

Bean的做用域

Spring定義了多種Bean做用域,可基於這些做用域建立bean,包括:

    單例(Singleton):在整個應用中,只建立一個bean實例

    原型(Prototype):每次注入或者經過Spring應用上下文獲取的時候,都會建立一個新的bean實例

    會話(Session):在web應用中,爲每一個會話建立一個bean實例

請求(Request):在web應用中,爲每一個請求建立一個bean實例。默認狀況下,Spring應用上下文中全部bean都是做爲單例的形式建立。也就是說,無論既定的一個bean被注入到其餘bean多少次,每次所注入的都是同一個實例。

 

IoC瞭解一下

一種設計思想。就是將本來在程序中手動建立對象的控制權,交由Spring框架來管理。不用new對象,而直接從Spring那裏獲取對象。

 

開始Spring的Coding漫漫路 

1. 新建Java項目,命名 spring

2.新建lib目錄,並添加jar包,

 

3.開始編寫程序

  3.1 在src下新建bean包,而後新建User類

package bean;

public class User {
    private Integer id;
    private String name;
    private String gender;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

}
View Code

 

  3.2 在src下新建 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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean name="beanUser" class="bean.User">
        <property name="id" value="1001"></property>
        <property name="name" value="Jesse"></property>
        <property name="gender" value="male"></property>
    </bean>

</beans>
View Code

 

4.測試程序

  4.1 在src下新建test類

import bean.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class test {

    @Test
    public void demo1() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"applicationContext.xml"});
        User user = (User) context.getBean("beanUser");
        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getGender());
    }
}

 運行結果: 

 

DI:Dependency Injection (依賴注入)

Spring 在建立對象的過程當中,將對象依賴屬性(簡單值,集合,對象)經過配置設值給該對象。或者說拿到對象的屬性,已經被注入到了相關值了,能夠直接使用。

1. 在bean下新建 SaySomething類

package bean;

public class SaySomething {
    private User user = null;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public String sayHi() {
        return user.getId() + " " + user.getName() + " " + user.getGender();
    }
}
View Code

 

2.修改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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">


    <bean name="beanUser" class="bean.User">
        <property name="id" value="1001"></property>
        <property name="name" value="Jesse"></property>
        <property name="gender" value="male"></property>
    </bean>

    <bean name="saySomething" class="bean.SaySomething">
        <property name="user" ref="beanUser"></property>
    </bean>

</beans>
View Code

 

3.在test.java裏,添加新的方法,測試上面代碼

  @Test
    public void demo2() {
        ApplicationContext context = new ClassPathXmlApplicationContext(
                new String[]{"applicationContext.xml"});
        SaySomething saySomething = (SaySomething) context.getBean("saySomething");
        System.out.println(saySomething.sayHi());
    }

 運行結果: 

 

AOP Aspect Oriented Program 面向切面編程

Aspect Oriented Program面向切面編程,經過預編譯方式和運行期動態代理實現程序功能的統一維護的一種技術。AOP採起橫向抽取機制,取代了傳統縱向繼承體系重複性代碼

      核心業務,好比登錄,CUD

      周邊功能,統計,日誌,事務管理。在spring的面向切面編程AOP思想裏,即被定義爲切面

      在面向切面的思想裏,核心業務功能和切面功能單獨開發,而後把兩個組合在一塊兒,就是AOP 

目的:AOP 可以將那些與業務無關,卻爲業務模塊所共同調用的邏輯或責任(事務,日誌,權限等)封裝起來,便於減小系統的重複代碼,下降模塊間的耦合度,並有利於將來的可拓展性和可維護性。

AOP實現原理

    AOP底層將採用代理機制進行實現

    接口+實現類:spring採用jdk的動態代理Proxy

    實現類:spring採用cglib字節碼加強

相關文章
相關標籤/搜索