Spring開發步驟

來源:互聯網
上載者:User

標籤:ica   new   www   return   cat   ase   ring   const   細節   

1) 源碼, jar檔案:此處用的是spring-framework-3.2.5.RELEASE

commons-logging-1.1.3.jar                 日誌

spring-beans-3.2.5.RELEASE.jar           bean節點

spring-context-3.2.5.RELEASE.jar         spring上下文節點

spring-core-3.2.5.RELEASE.jar              spring核心功能

spring-expression-3.2.5.RELEASE.jar    spring運算式相關表

2) 核心設定檔: applicationContext.xml 

      Spring設定檔:applicationContext.xml / bean.xml

約束參考:

spring-framework-3.2.5.RELEASE\docs\spring-framework-reference\htmlsingle\index.html

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

 (4)、建立一個實體類對象,如下:

package com.gqx.hello;public class User {private int id;private String name;public User() {// TODO Auto-generated constructor stubSystem.out.println("----------User對象建立成功-------------");}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public void init_user() {// TODO Auto-generated method stubSystem.out.println("對象建立之後初始化 !");}public void destory_user() {// TODO Auto-generated method stubSystem.out.println("IOC容器銷毀,user對象回收!");}}

 (5)、在applicationContext.xml中加入

<!-- IOC容器的配置:要建立的所有對象都配置在這裡 --><bean id="user" class="com.gqx.hello.User" scope="singleton" lazy-init="default" init-method="init_user" destroy-method="destory_user"></bean>

 (6)、測試類別

public class App {//1、通過工廠類得到IOC容器建立的對象public static void main(String[] args) {// TODO Auto-generated method stub//建立對象//User user=new User();//現在,把對象的建立交給spring的IOC容器Resource resource=new ClassPathResource("com/gqx/hello/applicationContext.xml");//建立容器物件(Bean的工廠) IOC容器=工廠類+applicationContext.xmlBeanFactory factory=new XmlBeanFactory(resource);//得到容器建立的對象User user=(User)factory.getBean("user");System.out.println(user.getId());}}

 這個時候會看到控制台會列印出相對應的對象。

(7)、bean對象建立的細節

編寫測試類別

//2、直接得到IOC容器物件(方便)/* * (1)、對象的建立:單例/多例 * 預設是單例 * scope="singleton" 預設值,即預設是單例 【service/dao/工具類】 * scope="prototype" 多例 【Action對象】 *   * (2)、什麼時候建立 * scope="prototype"  在用到對象的時候,才建立 * ----------容器建立------------ * ---------User對象建立------ * ---------User對象建立------ *  * scope="singleton" 在啟動(容器初始化之前),就已經建立了bean,且整個應用只有一個 * ----------User對象建立------------- * ---------容器建立------------ *  * (3)、是否延遲建立(只對scope="singleton"單例有效) * lazy-init="default" 預設為false 不延遲建立,即在啟動的時候建立 * lazy-init="true" 延遲初始化,在用到對象的時候,才建立 *  * (4)、建立對象之後,初始化/銷毀 * init-method="init_user"  對應對象的init_user方法,在對象建立之後執行 * destroy-method="destory_user"  調用容器物件的destory方法時執行(容器用實作類別) */@Testpublic void test(){//得到IOC容器物件(要銷毀,必須用實作類別)ClassPathXmlApplicationContext ac=new ClassPathXmlApplicationContext("com/gqx/hello/applicationContext.xml");System.out.println("----------容器建立------------");//從容器中取得beanUser user1=(User)ac.getBean("user");User user2=(User)ac.getBean("user");System.out.println(user1);System.out.println(user2);System.err.println(user1==user2);//銷毀容器物件ac.destroy();}

 

Spring開發步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.