preface

Text has been included to my lot warehouse, welcome Star:https://github.com/bin392328206 plant a tree is the best time ten years ago, followed by now

six-finger-web

The wheels of a Web backend framework roll themselves (easy) from handling Http requests [Netty request-level Web servers] to MVC [interface encapsulating and forwarding], to IOC [dependency injection], to AOP [aspect], to RPC [remote procedure calls] and finally to ORM [database operations].

github

Why the wheel

Actually is such, small six six oneself peacetime? Sometimes like to look at the source code such as Spring, but the level may not how, every time dazed at all, and then feel the inside of the details too difficult, then I can only view the overall thought, then I think if I can according to each elder some thought, rolled out a simple wheel, Is it easier for me to understand the author’s ideas? So six-Finger-Web came out, and it was really a learning process for me, and THEN I opened it up to help those of you who are having trouble learning the source code. In addition, we can exercise our coding ability, because we usually use Java API for CRUD. As time goes by, we are not familiar with many framework API classes, so we take this opportunity to exercise.

The characteristics of

  • Built-in HTTP server written by Netty, without additional dependence on Web services such as Tomcat.
  • Code is simple to understand (small 66 can not write their own framework big guy that kind of high clustering, low coupling code), ability a little bit stronger to see the code can understand, weakness also does not matter, small 66 has a supporting from 0 build tutorial.
  • Support MVC-related annotations to ensure that they are used similarly to SpringMVC
  • Support for Spring IOC and Aop related features
  • Support similar to Mybatis related functions
  • Supports RPC-related functionality similar to Dubbo
  • For data returns, only the Json format is supported

omg

The front is already written chapters, I will give you one by one to go through the construction process

  • Suitable for beginners and intermediate Java programmer training manual to build the entire Web project from 0 (A)
  • Build the whole Web project from 0 (2)
  • Build the whole Web project from 0 (3)

Last time we completed ioc injection based on Spring annotations, today we will look at the simple IMPLEMENTATION of XML, to learn about the XML process, just the most simple understanding of the XML process, and no more in-depth to write the details inside!

First let’s look at the package structure, let’s look at what’s new


In fact, this section is very simple, is to add a few classes to parse XML files, I also use someone else’s package, dom4j this specialized parsing XML. So in fact, there is not much we write, we just understand his process, and then we can better understand our Spring source code,

Let’s look at the effects and test classes


It’s really simple, I just add this class to the request, and then I print out this class, and I’m done

Background printing of tests


Take a look at the transformation process.

DefaultApplicationContext

DefaultApplicationContext->refresh() This is actually where the bean loaded the XML

XmlBeanDefinitionReader

package com.xiaoliuliu.six.finger.web.spring.ioc.beans.support;

import com.xiaoliuliu.six.finger.web.spring.ioc.beans.BeanDefinition;
import com.xiaoliuliu.six.finger.web.spring.ioc.exception.BeanDefinitionStoreException;
import com.xiaoliuliu.six.finger.web.spring.ioc.io.Resource;
import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader;  import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.List;  / * ** @author little six six* @ version 1.0 * @date 2020/10/21 15:11 * Parse the XML file into a data structure* /public class XmlBeanDefinitionReader {  public static final String ID_ATTRIBUTE = "id";   public static final String CLASS_ATTRIBUTE = "class";    public List<BeanDefinition> loadBeanDefinitions(Resource resource) {  InputStream is = null;  List<BeanDefinition> result = new ArrayList<>();  try {  is = resource.getInputStream();  SAXReader reader = new SAXReader();  Document doc = reader.read(is);   Element root = doc.getRootElement(); //<beans>  Iterator<Element> iter = root.elementIterator();  while (iter.hasNext()) {  Element ele = (Element) iter.next();  String id = ele.attributeValue(ID_ATTRIBUTE);  String beanClassName = ele.attributeValue(CLASS_ATTRIBUTE);  BeanDefinition bd = new BeanDefinition(beanClassName, id);  result.add(bd);  }  return result;  } catch (Exception e) {  throw new BeanDefinitionStoreException("IOException parsing XML document from " + resource.getDescription(), e);  } finally {  if(is ! = null) { try {  is.close();  } catch (IOException e) {  e.printStackTrace();  }  }  }   }   }  Copy the code

Resource

package com.xiaoliuliu.six.finger.web.spring.ioc.io;

import java.io.IOException;
import java.io.InputStream;

/ * ** @author little six six* @ version 1.0 * @date 2020/10/21 15:18 * /public interface Resource {  public InputStream getInputStream() throws IOException;  public String getDescription(); }   Copy the code

ClassPathResource

package com.xiaoliuliu.six.finger.web.spring.ioc.io;

import com.xiaoliuliu.six.finger.web.spring.ioc.util.ClassUtils;

import java.io.FileNotFoundException;
import java.io.IOException; import java.io.InputStream;  / * ** @author little six six* @ version 1.0 * @date 2020/10/21 15:32 * /public class ClassPathResource implements Resource {   private String path;  private ClassLoader classLoader;   public ClassPathResource(String path) {  this(path, (ClassLoader) null);  }  public ClassPathResource(String path, ClassLoader classLoader) {  this.path = path; this.classLoader = (classLoader ! = null ? classLoader : ClassUtils.getDefaultClassLoader()); }   @Override  public InputStream getInputStream() throws IOException {  InputStream is = this.classLoader.getResourceAsStream(this.path);   if (is == null) {  throw new FileNotFoundException(path + " cannot be opened");  }  return is;   }  @Override  public String getDescription() { return this.path;  }  }  Copy the code

At the end

The end result is the same whether it’s annotation-based or xmL-based. Let’s take a look at AOP in the next chapter. Fighting!!!!

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest power of my creation, our next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!