Spring Core Learning (4) reading from XML beandefinition-code into configuration

Source: Internet
Author: User
Tags acer

Leading: Start learning Spring core ideas and learn with a cottage Lite spring code.


Content: 1. The beandefinitionreader-configures the reader. 2. xmlbeandefinitionreader-reads the configuration from the XML. 3. resource-locate the resource file. This time the bean configuration information is put into the XML, so there will be an XML file read, we have xmlbeandefinitionreader the information in the XML into a map beandefinition, This way we can actually assemble the bean as spring does by configuring the file.


Duplicate code is no longer listed

Beandefinition:

public class Beandefinition {private Object bean;private class Beanclass;private String beanclassname;private Propertyvalues propertyvalues = new Propertyvalues ();p ublic beandefinition () {}public Object Getbean () {return bean;} public void Setbean (Object bean) {This.bean = bean;} Public Class Getbeanclass () {return beanclass;} public void Setbeanclass (Class beanclass) {this.beanclass = Beanclass;} Public String Getbeanclassname () {return beanclassname;} public void Setbeanclassname (String beanclassname) {this.beanclassname = beanclassname;try {This.beanclass = Class.forName (beanclassname);} catch (ClassNotFoundException e) {e.printstacktrace ();}} Public propertyvalues getpropertyvalues () {return propertyvalues;} public void Setpropertyvalues (Propertyvalues propertyvalues) {this.propertyvalues = propertyvalues;}}

Resource:

/** * Resource is the spring internal location resource interface * @author Acer */public interface Resource {InputStream getinputstream () throws IOException; }
Resourceloader:

public class Resourceloader {public Resource getresource (String location) {URL Resource = This.getclass (). getClassLoader (). getresource (location); return new Urlresource (Resource);}
Urlresource:

public class Urlresource implements Resource{private final URL url;public urlresource (url url) {this.url = URL;} @Overridepublic InputStream getInputStream () throws IOException {URLConnection urlconnection = url.openconnection (); Urlconnection.connect (); return Urlconnection.getinputstream ();}}
Beandefinitionreader:

/** * Read from configuration Beandefinitionreader * @author acer * */public interface Beandefinitionreader {void Loadbeandefinitions (Strin G location) throws Exception;}
Abstractbeandefinitionreader:

/** * Read from configuration Beandefinitionreader * @author Acer * */public abstract class Abstractbeandefinitionreader implements Beandefi Nitionreader{private map<string, beandefinition> registry;private resourceloader resourceLoader;protected Abstractbeandefinitionreader (Resourceloader resourceloader) {this.registry = new hashmap<string, BeanDefinition > (); this.resourceloader = Resourceloader;} Public map<string, Beandefinition> GetRegistry () {return this.registry;} Public Resourceloader Getresourceloader () {return resourceloader;}}

Xmlbeandefinitionreader:

public class Xmlbeandefinitionreader extends Abstractbeandefinitionreader{public Xmlbeandefinitionreader ( Resourceloader Resourceloader) {super (resourceloader);} @Overridepublic void Loadbeandefinitions (String location) throws Exception {InputStream InputStream = Getresourceloader (). getresource (location). getInputStream ();d oloadbeandefinitions (InputStream);} protected void Doloadbeandefinitions (InputStream inputstream) throws Exception {Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();D ocumentbuilder docbuilder = Factory.newdocumentbuilder ();D ocument doc = Docbuilder.parse (InputStream);//Parse Beanregisterbeandefinitions (DOC); Inputstream.close ();} public void Registerbeandefinitions (Document doc) {Element root = doc.getdocumentelement ();p arsebeandefinitions (Root) ;} protected void Parsebeandefinitions (Element root) {NodeList nl = root.getchildnodes (); for (int i = 0; i < nl.getlength ( ); i++) {Node node = Nl.item (i), if (node instanceof Element) {element ele = (element) node;Processbeandefinition (Ele);}}} protected void Processbeandefinition (Element ele) {String name = Ele.getattribute ("name"); String className = Ele.getattribute ("class"); Beandefinition beandefinition = new Beandefinition ();p rocessproperty (Ele, beandefinition); Beandefinition.setbeanclassname (ClassName); GetRegistry (). Put (name, beandefinition);} private void Processproperty (Element ele, beandefinition beandefinition) {NodeList Propertynode = Ele.getelementsbytagname ("Property"), for (int i = 0; i < propertynode.getlength (); i++) {Node node = Propertynode.item ( i); if (node instanceof Element) {element Propertyele = (element) node; String name = Propertyele.getattribute ("name"); String value = Propertyele.getattribute ("value"); Beandefinition.getpropertyvalues (). Addpropertyvalue (New PropertyValue (name, value));}}}}

public class Beanfactorytest {@Testpublic void Test () throws exception{//1. Read Configuration Xmlbeandefinitionreader Xmlbeandefinitionreader = new Xmlbeandefinitionreader (new Resourceloader ()); Xmlbeandefinitionreader.loadbeandefinitions ("Tinyioc.xml");//2. Initialize beanfactory and register beanbeanfactory beanFactory = new Autowirecapablebeanfactory (); for (map.entry<string, beandefinition> beandefinitionentry: Xmlbeandefinitionreader.getregistry (). EntrySet ()) Beanfactory.registerbeandefinition (BeanDefinitionEntry.getKey (), Beandefinitionentry.getvalue ());//3. Get Beanhelloworldservice HelloWorldService = (helloworldservice) beanfactory.getbean ("HelloWorldService"); Helloworldservice.helloworld ();}}

Tinyioc.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:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ SPRING-BEANS-2.5.XSDHTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-2.5.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-2.5.xsd "> <bean name=" helloworldservice "class=" Step4.test.HelloWorldService "> <prope Rty name= "text" value= "Hello world! From step4! " ></property> </bean></beans>



Spring Core Learning (4) reading from XML beandefinition-code into configuration

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.