欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Spring 簡單應用

系統 1611 0
下面使用的 spring-3.0版本來演示

目標任務
1.配置最簡單的,spring 應用?
2.使用spring管理ServiceBean
3.手動編寫代碼 模擬 spring的初始工作



1.首先來看 spring 應用中最基本的jar

Spring 簡單應用


2.spring的配置文件 (可以從spring的文檔中拷貝,或者 spring自帶的 案例中拷貝)
    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> 

</beans> 

  


3.就這樣 我們就可以 來初始化 spring 容器? 編寫單元測試
    
package junit.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringTest {

	@Test //測試 spring的 初始化
	public void init(){
		ApplicationContext context=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
	}
}

  


//上面 如果測試單元沒有問題 就說明 spring 框架 使用成功 !! 恭喜 !!

4.下面 來看 使用 spring容器來管理我們的 bean (首先定義bean--->然后配置)
    
package com.person.service;

public interface PersonService {
	public void save();
}

  


    
package com.person.service.impl;

import com.person.service.PersonService;

public class PersonServiceBean implements PersonService {
	public void save(){
		System.out.println("我就是save方法!");
	}
}

  


5.在spring配置文件中配置
    
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> 

	<!-- 這里配置后 就會有 spring來管理, 注意id 和name 的區別  -->
	<bean id="personService" class="com.person.service.impl.PersonServiceBean">
	</bean>
</beans>   
 

  


6.測試
    
package junit.test;

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

import com.person.service.PersonService;

public class SpringTest {

	@Test //測試 spring的 初始化
	public void init(){
		ApplicationContext ctx=new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		
		
		//方法一  獲取服務層對象
		PersonService personService=(PersonService)ctx.getBean("personService");
		//方法二
		PersonService personService1=ctx.getBean("personService",PersonService.class);
		
		personService.save();
		personService1.save();
		
	}
}

  


//如果能在控制臺 打印出? "我就是save方法!" 恭喜成功!

7.看了上面這么多? 那么spring到底怎么初始化 和 管理的 我們的bean呢? 我們自己來模擬寫一個 spring的 init 初始工作 (使用 dom4j 來讀取配置文件 -->使用 反射來創建對象)



8.首先加入? dom4j-1.6.1.jar 和? jaxen.jar(dom4j需要的一個jar)

9.定義隱射spring配置文件bean 的類
    

package junit.test;

/**
 * 定義  保存 spring配置文件中的bean對象
 * @author Bin
 */
public class BeanDefinition {
	
	private String id;  //對應 spring配置文件中的id
	private String className;   //對應 spring配置文件中的class 
	//get set
       // 構造方法 
}

  


10.定義 我們自己的 讀取spring配置文件中的 ClassPathXmlApplicationContext 類
    
package junit.test;


import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;

import com.sun.xml.internal.fastinfoset.stax.events.Util;


/*
 * 來模擬spring 的工作
 */
public class MySpringClassPathXmlApplicationContext {
	
	//保存 配置文件中的 bean 的信息  
	private List<BeanDefinition> beandefines=new ArrayList<BeanDefinition>();
	
	
	//保存初始化 后的對象
	private Map<String,Object> sigletons=new HashMap<String,Object>();
	
	
	public MySpringClassPathXmlApplicationContext(String fileName) {
		this.readXml(fileName);
		this.instanceBean();
	}

	/**==================
	 *   讀取xml配置文件
	 * ==================
	 * @author Bin
	 * @param fileName
	 */
	private void readXml(String fileName) {
		SAXReader saxReader=new SAXReader();
		Document doucment=null;
		try {
			URL xmlPath=this.getClass().getClassLoader().getResource(fileName);
			doucment=saxReader.read(xmlPath);
			Map<String,String> nsMap=new HashMap<String,String>();
			//給spring配置文件的命名空間  一個別名 ns
			nsMap.put("ns","http://www.springframework.org/schema/beans"); //加入命名空間  xmlns: spring配置文件中的
			XPath xsub=doucment.createXPath("http://ns:beans/ns:bean"); //創建 beans/bean 查詢路徑
			
			xsub.setNamespaceURIs(nsMap); //設置命名空間
			List<Element> beans=xsub.selectNodes(doucment); //獲取文檔下的所有bean節點
			
			for (Element element : beans) {
				String id=element.attributeValue("id"); //獲取 id的屬性值
				String clazz=element.attributeValue("class"); // 獲取 class屬性

				//類是 spring中的 : org.springframework.beans.factory.config.BeanDefinition;
				BeanDefinition beandefine=new BeanDefinition(id,clazz);
				beandefines.add(beandefine);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**===================================
	 *   完成Bean 的實例化
	 * ===================================
	 * @author Bin
	 */
	private void instanceBean() {
		for (BeanDefinition beandefine : beandefines) {
			try {
				if(!Util.isEmptyString(beandefine.getClassName()))
					sigletons.put(beandefine.getId(), Class.forName(beandefine.getClassName().trim()).newInstance());
			}catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**=======================
	 *    獲取bean 
	 * =====================
	 * @param beanName
	 * @return
	 */
	public Object getBean(String beanName){
		return this.sigletons.get(beanName);
	}
	
}

  


11.好啦 我們來測試吧
    

	@Test  //測試 自定義的spring 模擬器
	public void testMySpring(){
		MySpringClassPathXmlApplicationContext ctx=new MySpringClassPathXmlApplicationContext("applicationContext.xml");
		//方法一  獲取服務層對象
		PersonService personService=(PersonService)ctx.getBean("personService");
		personService.save();
	}

  



//如果? 同樣看到? "我就是save方法!"? 我想 你應該對spring有所了解了吧??? 嘻嘻
//睡覺啦 呵呵? 88

Spring 簡單應用


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 一区二区三区在线 | 久久69精品久久久久久国产越南 | 抱着cao才爽免费观看 | 97久久国语露脸精品对白 | 国产精品视频久久久 | 一区二区在线 | 91中文字幕在线观看 | 精品国产99 | 美女污视频网站 | 婷婷在线视频 | 欧美成人18 | 看个毛片| 国产精品吹潮在线观看中文 | www国产精品 | 成人国产在线观看 | 日韩精品 电影一区 亚洲 | 国产精品视频免费一区二区三区 | 欧美性爽xxxⅹbbbb | 亚洲一区中文字幕 | 日本久久网 | 神马久久久久久久久 | 精品国产一区二区亚洲人成毛片 | 欧洲色图亚洲色图 | 欧美在线小视频 | 亚洲av毛片久久久久 | 国产在线综合一区二区三区 | 2021国产精品自产拍在线 | 在线一级片 | 激情久久一区二区 | 特级毛片免费 | 好爽好大www视频在线播放 | 国产精品爱啪在线线免费观看 | 亚洲看看 | 中文字幕不卡在线观看 | 成人免费大片a毛片 | 欧美―第一页―浮力影院 | 久久第一页 | 午夜免费视频观看 | 日韩a在线看免费观看视频 五月天激情视频在线观看 成人97在线观看免费高清 | 成人国产在线观看 | 91福利一区二区在线观看 |