Spring入門詳細教程(一)

1、spring概述

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

2、Spring特色

一、方便解耦,簡化開發。spring

二、AOP編程的支持。編程

三、聲明式事務的支持。spring-mvc

四、方便程序的測試websocket

五、方便集成各類優秀框架session

3、spring下載

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

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

4、spring搭建入門案例

一、在eclipse中建立一個動態的web工程。框架

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

三、編寫一個實體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;
    }
}

四、建立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>

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

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

六、書寫測試代碼

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

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

com.jichi.entity.User@db5e9c

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

一、BeanFactory

spring原始接口,功能較爲單一,在從容器中得到對象的時候纔會建立對象。

二、ApplicationContext

每次啓動容器的時候,初始化容器中的全部對象並提供更多功能。

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

三、實現圖

四、結論

web開發中,使用applicationContext。

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

6、spring的bean元素屬性詳解

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

二、name:被容器管理對象的名稱,根據此名稱從容器中得到該對象,能夠重複,可使用特殊字符。

三、id:被容器管理對象的惟一標識,id不可重複,不可以使用特殊字符,做用與name相同,建議使用name。

四、scope:

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

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

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

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

五、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("銷燬");
    }
}

7、spring建立對象的方式

一、空參構造方法

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

二、靜態工廠實例化

(1)建立一個工廠類

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

(2)配置

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

三、實例工廠實例化

(1)建立一個工廠類

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

(2)配置

<bean name="user" factory-bean="userFactory" factory-method="createUser"></bean>
相關文章
相關標籤/搜索