目標(biÄo)任務(wù)
1.é…置最簡單的,spring 應(yÄ«ng)用?
2.使用spring管ç†ServiceBean
3.手動編寫代碼 模擬 springçš„åˆå§‹å·¥ä½œ
1.首先來看 spring 應(yÄ«ng)用ä¸æœ€åŸºæœ¬çš„jar

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 çš„å€(qÅ«)別 --> <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"); //方法一 ç²å–æœå‹™(wù)層å°è±¡ PersonService personService=(PersonService)ctx.getBean("personService"); //方法二 PersonService personService1=ctx.getBean("personService",PersonService.class); personService.save(); personService1.save(); } }
//如果能在控制臺 打å°å‡º? "我就是save方法ï¼" æå–œæˆåŠŸ!
7.看了上é¢é€™ä¹ˆå¤š? 那么spring到底怎么åˆå§‹åŒ– å’Œ 管ç†çš„ 我們的beanå‘¢? 我們自己來模擬寫一個 springçš„ init åˆå§‹å·¥ä½œ (使用 dom4j 來讀å–é…置文件 -->使用 å射來創(chuà ng)建å°è±¡)
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; //å°æ‡‰(yÄ«ng) springé…置文件ä¸çš„id private String className; //å°æ‡‰(yÄ«ng) springé…置文件ä¸çš„class //get set // 構(gòu)é€ æ–¹æ³• }
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"); //創(chuà ng)建 beans/bean 查詢路徑 xsub.setNamespaceURIs(nsMap); //è¨(shè)置命å空間 List<Element> beans=xsub.selectNodes(doucment); //ç²å–文檔下的所有bean節(jié)點 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"); //方法一 ç²å–æœå‹™(wù)層å°è±¡ PersonService personService=(PersonService)ctx.getBean("personService"); personService.save(); }
//如果? åŒæ¨£çœ‹åˆ°? "我就是save方法ï¼"? 我想 ä½ æ‡‰(yÄ«ng)該å°spring有所了解了å§??? 嘻嘻
//ç¡è¦ºå•¦ 呵呵? 88
æ›´å¤šæ–‡ç« ã€æŠ€è¡“(shù)交æµã€å•†å‹™(wù)åˆä½œã€è¯(lián)ç³»åšä¸»
微信掃碼或æœç´¢ï¼šz360901061

å¾®ä¿¡æŽƒä¸€æŽƒåŠ æˆ‘ç‚ºå¥½å‹
QQ號è¯(lián)系: 360901061
您的支æŒæ˜¯åšä¸»å¯«ä½œæœ€å¤§çš„動力,如果您喜æ¡æˆ‘çš„æ–‡ç« ï¼Œæ„Ÿè¦ºæˆ‘çš„æ–‡ç« å°æ‚¨æœ‰å¹«åŠ©ï¼Œè«‹ç”¨å¾®ä¿¡æŽƒæ下é¢äºŒç¶ç¢¼æ”¯æŒåšä¸»2å…ƒã€5å…ƒã€10å…ƒã€20å…ƒç‰æ‚¨æƒ³æ的金é¡å§ï¼Œç‹ ç‹ é»žæ“Šä¸‹é¢çµ¦é»žæ”¯æŒå§ï¼Œç«™é•·éžå¸¸æ„Ÿæ¿€æ‚¨ï¼æ‰‹æ©Ÿå¾®ä¿¡é•·æŒ‰ä¸èƒ½æ”¯ä»˜è§£æ±ºè¾¦æ³•ï¼šè«‹å°‡å¾®ä¿¡æ”¯ä»˜äºŒç¶ç¢¼ä¿å˜åˆ°ç›¸å†Šï¼Œåˆ‡æ›åˆ°å¾®ä¿¡ï¼Œç„¶åŽé»žæ“Šå¾®ä¿¡å³ä¸Šè§’掃一掃功能,é¸æ“‡æ”¯ä»˜äºŒç¶ç¢¼å®Œæˆæ”¯ä»˜ã€‚
ã€æœ¬æ–‡å°æ‚¨æœ‰å¹«åŠ©å°±å¥½ã€‘å…ƒ
