1. 程式人生 > >Spring學習一(spring概念和ioc入門)

Spring學習一(spring概念和ioc入門)

 

Spring概念

    Spring是一個開源輕量級框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是為了解決企業應用開發的複雜性而建立的。框架的主要優勢之一就是其分層架構,分層架構允許使用者選擇使用哪一個元件,同時為 J2EE 應用程式開發提供整合的框架。Spring使用基本的JavaBean來完成以前只可能由EJB完成的事情。然而,Spring的用途不僅限於伺服器端的開發。從簡單性、可測試性和鬆耦合

的角度而言,任何Java應用都可以從Spring中受益。Spring的核心是控制反轉(IoC)和麵向切面(AOP)。

 spring核心主要兩部分

(1)aop:面向切面程式設計,擴充套件功能不是修改原始碼實現

(2)ioc:控制反轉,

- 比如有一個類,在類裡面有方法(不是靜態的方法),呼叫類裡面的方法,建立類的物件,使用物件呼叫方法,建立類物件的過程,需要new出來物件

- 把物件的建立不是通過new方式實現,而是交給spring配置建立類物件

IOC底層原理

1 ioc底層原理使用技術

(1)xml配置檔案

(2)dom4j解決xml

(3)工廠設計模式

(4)反射

2.實現過程

   通過xml實現通過map的鍵值對放入 鍵class 值完整的類全名(指向相關聯的類),併為其設定   鍵id 值(隨意不過一般是類名) 。

 

  通過dom解析將得到類全名

 在通過類全名使用反射建立類物件

圖片解釋 

 IOC入門案例

所需jar包(https://download.csdn.net/download/qq_35654259/10618401

                           

配置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: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>

在xml檔案中配製物件建立

<bean id="user3" class="com.yc.test.bbs.bean.User3">
    
     </bean>

最終的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: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">
     
     
     <bean id="user3" class="com.yc.test.bbs.bean.User3">
    
     </bean>
  </beans>

建立所對應的User3.java檔案(注意包名必須與配置的class的值一致)

package com.yc.test.bbs.bean;

import java.sql.Timestamp;
import java.util.List;
import java.util.Properties;

/**
 * 使用者實體類
 */
public class User3 {

	/**
	 * 使用者ID
	 */
	private Integer uid;
	/**
	 * 使用者名稱
	 */
	private String uname;
	/**
	 * 使用者密碼
	 */
	private String upass; // 加密後的長度比較長
	/**
	 * 使用者頭像
	 */
	private String head; // 頭像的圖片路徑
	/**
	 * 註冊時間
	 */
	private Timestamp regtime; // 註冊時間     Timestamp 帶時分秒的日期
	/**
	 * 使用者性別:1男,0女
	 */
	private Integer gender; // 性別   1男,0女
	
	/**
	 * 使用者發帖集合
	 */
	private List<Topic> topicList;
	private Properties pros;
	
	

	public Properties getPros() {
		return pros;
	}

	public void setPros(Properties pros) {
		this.pros = pros;
	}

	public Integer getUid() {
		return uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}

	public String getUpass() {
		return upass;
	}

	public void setUpass(String upass) {
		this.upass = upass;
	}

	public String getHead() {
		return head;
	}

	public void setHead(String head) {
		this.head = head;
	}

	public Timestamp getRegtime() {
		return regtime;
	}

	public void setRegtime(Timestamp regtime) {
		this.regtime = regtime;
	}

	public Integer getGender() {
		return gender;
	}

	public void setGender(Integer gender) {
		this.gender = gender;
	}

	public List<Topic> getTopicList() {
		return topicList;
	}

	public void setTopicList(List<Topic> topicList) {
		this.topicList = topicList;
	}

}

IOC案例的測試

package com.yc.test;

import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.yc.test.bbs.bean.User3;

public class User3Test {
	
protected ApplicationContext context;
	
	@Before
	public void before() throws Exception{
		//載入Spring配置檔案,根據檔案建立物件
		context = new ClassPathXmlApplicationContext("User3.xml");
		
	}
	
	@Test
	public void TestUser3(){
		//得到通過反射建立的物件
		User3 user = (User3) context.getBean("user3");
		User3 user1 = (User3) context.getBean("user3");
		System.out.println(user);//(若物件能建立則會輸出物件的地址)[email protected]
		System.out.println(user1);//(若物件能建立則會輸出物件的地址)[email protected]
		System.out.println(user == user1);//true
	}
}

問題 

  最後給大家提一個問題為啥最後的user和user1是同一個物件(可以去了解di  依賴注入)