Spring入門詳細教程(一)


一、spring概述

Spring是一個開放源代碼的設計層面框架,他解決的是業務邏輯層和其他各層的松耦合問題,因此它將面向接口的編程思想貫穿整個系統應用。Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson創建。簡單來說,Spring是一個分層的JavaSE/EE full-stack(一站式) 輕量級開源框架。

二、Spring特點

1、方便解耦,簡化開發。

2、AOP編程的支持。

3、聲明式事務的支持。

4、方便程序的測試

5、方便集成各種優秀框架

三、spring下載

下載地址:https://repo.spring.io/libs-release-local/org/springframework/spring/

進入后可選擇下載版本,選擇版本后,進入目錄結構。其中dist是最終發布版本,包含開發所需lib和源碼。docs是開發文檔。schema是一些約束文件。

四、spring搭建入門案例

1、在eclipse中創建一個動態的web工程。

2、導入spring的基礎lib包到lib文件夾下。

3、編寫一個實體User類

package com.jichi.entity;

public class User {
    private String name;
    private Integer age ;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
}

4、創建applicationContext.xml文件

在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"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:jms="http://www.springframework.org/schema/jms"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:oxm="http://www.springframework.org/schema/oxm"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:tool="http://www.springframework.org/schema/tool"
    xmlns:websocket="http://www.springframework.org/schema/websocket"
    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.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tool http://www.springframework.org/schema/tool/spring-tool.xsd
        http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket.xsd">
        
</beans>

5、在beans內將user實體配置進去

<bean name="user" class="com.jichi.entity.User"></bean>

6、書寫測試代碼

public class TestDemo {
    @Test
    public void test1(){
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = (User) ac.getBean("user");
        System.out.println(user);
    }
}

7、結果顯示,將user對象交給spring容器管理成功

com.jichi.entity.User@db5e9c

五、spring中的工廠BeanFactory與ApplicationContext的區別

1、BeanFactory

spring原始接口,功能較為單一,在從容器中獲得對象的時候才會創建對象。

2、ApplicationContext

每次啟動容器的時候,初始化容器中的所有對象並提供更多功能。

其中的實現類ClassPathXmlApplicationContext是加載類路徑下的spring配置文件,FileSystemXmlApplicationContext是加載本地磁盤下spring的配置文件。

3、實現圖

4、結論

web開發中,使用applicationContext。

在資源匱乏的環境可以使用BeanFactory。

六、spring的bean元素屬性詳解

1、class:被容器管理對象的完整類名。

2、name:被容器管理對象的名稱,根據此名稱從容器中獲得該對象,可以重復,可以使用特殊字符。

3、id:被容器管理對象的唯一標識,id不可重復,不可使用特殊字符,作用與name相同,建議使用name。

4、scope:

  (1)singleton(默認):單例對象,該對象在spring容器中只會存在一個。

  (2)prototype:多例模式,配置為多例的對象不會在spring容器中創建,只有在從容器中要獲取該對象的時候,容器對該對象進行創建,每次創建都是一個新的對象。注意在與struts2配合使用的時候,struts2中的action對象必須要配置成prototype這種多例模式。

  (3)request:web項目中,創建一個對象,將其存放在request域中。

  (4)session:web項目中,創建一個對象,將其存放在session域中。

5、init-method與destory-method

init-method是對象創建的時候所執行的方法,destory-method是對象銷毀時所執行的方法。對象必須是單例的,同時容器關閉的時候,對象的銷毀方法才會執行。

<bean name="user" class="com.jichi.entity.User" init-method="init" destroy-method="destory"></bean>
public class User {
    private String name;
    private Integer age ;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getAge() {
        return age;
    }
    public void setAge(Integer age) {
        this.age = age;
    }
    public void init(){
        System.out.println("初始");
    }
    public void destory(){
        System.out.println("銷毀");
    }
}

七、spring創建對象的方式

1、空參構造方法

<bean name="user" class="com.jichi.entity.User" ></bean>

2、靜態工廠實例化

(1)創建一個工廠類

public class UserFactory {
    public static User createUser(){
        return new User();
    }
}

(2)配置

<bean name="user" class="com.jichi.factory.UserFactory" factory-method="createUser"></bean>

3、實例工廠實例化

(1)創建一個工廠類

public class UserFactory {
    public static User createUser(){
        return new User();
    }
}

(2)配置

<bean name="user" factory-bean="userFactory" factory-method="createUser"></bean>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM