天天看點

SSM-Spring-01:Spring的概念+入門案例

------------吾亦無他,唯手熟爾,謙卑若愚,好學若饑-------------      

Spring

提起Spring,就會想到企業級架構這個詞

  企業級系統:

    1.大規模:使用者數量多,資料規模龐大,資料衆多

    2.性能和安全性要求更高

    3.業務複雜

    4.靈活應變

--------------------------------------------------------------------------------------------------------------------------------------------------

我覺得先了解一下Spring的地位和他的作者比較好

  Spring:java中的核心架構,沒有它就沒有現在的java的地位,如果Spring挂掉了,3-5年内java必挂

  Spring 的作者:Rod Johnson

    

SSM-Spring-01:Spring的概念+入門案例

    他是SpringFramework創始人,interface21 CEO

    豐富的C/C++背景,豐富的金融行業背景

    1996年開始關注java伺服器端技術

    2002年著寫《Expoert one-on-oneJ2EE設計與開發》,改變了Java世界

    技術主張以實用為本,音樂學博士

  接下來講講Spring的内容,放倆張圖檔

SSM-Spring-01:Spring的概念+入門案例

    Data/Access:資料通路

    Web:網絡

    AOP:面向切面程式設計

    Instrumentatio:插樁

    Core Container:核心(下面的一張圖有他的核心的内容,先了解這張)

    Test:測試,單測

SSM-Spring-01:Spring的概念+入門案例

    官網:Spring.io        拿的圖

  Spring的核心IOC和AOP(本處詳解IOC)

    IOC:控制反轉:(Inverse Of Control)

      控制反轉(Inversion of Control),是一個重要的面向對象程式設計的法則來削減計算機程式的耦合問題,也是輕量級的Spring架構的核心,beans。

      了解一:将元件對象(業務對象)的控制權從代碼本身轉移到外部容器()

      了解二:IOC控制反轉:說的是建立對象執行個體的控制權從代碼控制剝離到IOC容器(spring容器)控制,實際就是你在xml檔案控制,側重于原理。

    AOP:面向切面程式設計;  (Aspect Oritend Programming)

    提及一下對象間的關系把

SSM-Spring-01:Spring的概念+入門案例

第一個案例:

    1.下載下傳jar包:(我提供節點)

<!--單元測試的依賴  ctrl+shif+/-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.2.0.RELEASE</version>
        </dependency>

        <!--aop使用的jar-->
        <dependency>
            <groupId> org.aspectj</groupId >
            <artifactId> aspectjweaver</artifactId >
            <version> 1.8.7</version>
        </dependency>          

    2.一個普通類

package cn.dawn.day01.service;

/**
 * Created by Dawn on 2018/3/3.
 */
public class DawnService {

    private String workInfo;
    private Integer age;
    public void work(){
        System.out.println("info"+workInfo);
    }

    @Override
    public String toString() {
        return "DawnService{" +
                "workInfo='" + workInfo + '\'' +
                ", age=" + age +
                '}';
    }

    public String getWorkInfo() {
        return workInfo;
    }

    public void setWorkInfo(String workInfo) {
        this.workInfo = workInfo;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}      

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

    <bean id="service" class="cn.dawn.day01.service.DawnService">
        <property name="workInfo" value="第一個Spring程式"></property>
        <property name="age" value="12"></property>
    </bean>
</beans>      

    4.單測

package cn.dawn.day01;

import cn.dawn.day01.service.DawnService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * Created by Dawn on 2018/3/3.
 */
public class test20180303 {
    @Test
    /*入門案例*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");
        DawnService service = (DawnService) context.getBean("service");
        System.out.println(service);
    }
}      

    在沒有new 的情況下,就拿到了他的實作

    ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");

    這行如果不了解我就放一張圖

SSM-Spring-01:Spring的概念+入門案例

     看到ClassPathXmlApplicationContext前面的C嗎?在結合這個,表示他是ApplicationContext的實作類,裡氏替換,為什麼不可以用

    還記得我提了的IOC嗎?我不是說他把建立對象的控制權交給了Spring容器嘛,那麼容器在什麼時候建立對象呢,是getBean的時候嗎?還是。。。。(小實驗)

    在剛才的那個普通類中,添加一個構造,如下

public DawnService(){
        System.out.println("========================DawnService建立=======================");
    }      

     單測方法如下

@Test
    /*入門案例*/
    public void t01(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext01.xml");
        /*DawnService service = (DawnService) context.getBean("service");
        System.out.println(service);*/
    }      

    運作結果:

SSM-Spring-01:Spring的概念+入門案例

     結論就是Spring容器初始化的時候就把bean中的對象執行個體化了