1. 程式人生 > >Spring入門詳細教程(一)

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容器管理成功

[email protected]

五、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>