spring源码学习一
个人的学习总结如下:
1.系统环境变量验证,判断环境是否包含某个变量,如果没有,则启动不了
继承ClassPathXmlApplicationContext,重写initPropertySources方法(由于AbstractApplicationContext的refresh方法中,prepareRefresh中initPropertySources为空,方便后续扩展)
public class MyClassPathXmlApplicationContent extends ClassPathXmlApplicationContext { public MyClassPathXmlApplicationContent(String configLocations) { super(configLocations); } @Override protected void initPropertySources() { System.out.println(this.getEnvironment().getRequiredProperty("abc")); } }
2.自定义标签
自定义handler,handler找到parser,parser填充实体
自定义一个实体类
ublic class Jsuper { private String id; private String username; private Integer height; private Integer age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Integer getHeight() { return height; } public void setHeight(Integer height) { this.height = height; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
自定义parser,继承AbstractSingleBeanDefinitionParser,重写getBeanClass,重写doParse方法
public class JsuperBeanDefinitionParser extends AbstractSingleBeanDefinitionParser { @Override protected Class<?> getBeanClass(Element element) { return Jsuper.class; } @Override protected void doParse(Element element, BeanDefinitionBuilder builder) { if(element.hasAttribute("id")){ builder.addPropertyValue("id",element.getAttribute("id")); } if(element.hasAttribute("username")){ builder.addPropertyValue("username",element.getAttribute("username")); } if(element.hasAttribute("height")){ builder.addPropertyValue("height",element.getAttribute("height")); } if(element.hasAttribute("age")){ builder.addPropertyValue("age",element.getAttribute("age")); } } }
自定义handler,继承NameSpaceHandlerSupport,重写init方法
public class JsuperNamespaceHandler extends NamespaceHandlerSupport { @Override public void init() { registerBeanDefinitionParser("user",new JsuperBeanDefinitionParser()); } }
resources下新增spring.handlers,spring.schemas
spring.handlers http\://www.jsuper.com/schema/user=com.jsuper.test.custombean.JsuperNamespaceHandler spring.schemas http\://www.jsuper.com/schema/user.xsd=META-INF/user.xsd
新增user.xsd
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.jsuper.com/schema/user" xmlns:tns="http://www.jsuper.com/schema/user" elementFormDefault="qualified"> <element name="user"> <complexType> <attribute name="id" type="string"></attribute> <attribute name="username" type="string"></attribute> <attribute name="height" type="integer"></attribute> <attribute name="age" type="integer"></attribute> </complexType> </element> </schema>
在xml中新增
xmlns:jsuper="http://www.jsuper.com/schema/user" http://www.jsuper.com/schema/user http://www.jsuper.com/schema/user.xsd <jsuper:user id="aaa" username="zs" age="18" height="180"></jsuper:user>
可以从BeanFactory中获取aaa,这里有一个需要注意的,xml中的bean必须有id