如何进行spring中的默认标签源码解析

如何进行spring中的默认标签源码解析

本篇文章为大家展示了如何进行spring中的默认标签源码解析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

默认标签解析

try{Stringparent=null;if(ele.hasAttribute(PARENT_ATTRIBUTE)){//判断是否有parent标签parent=ele.getAttribute(PARENT_ATTRIBUTE);}AbstractBeanDefinitionbd=createBeanDefinition(className,parent);parseBeanDefinitionAttributes(ele,beanName,containingBean,bd);bd.setDescription(DomUtils.getChildElementValueByTagName(ele,DESCRIPTION_ELEMENT));parseMetaElements(ele,bd);parseLookupOverrideSubElements(ele,bd.getMethodOverrides());parseReplacedMethodSubElements(ele,bd.getMethodOverrides());parseConstructorArgElements(ele,bd);parsePropertyElements(ele,bd);parseQualifierElements(ele,bd);bd.setResource(this.readerContext.getResource());bd.setSource(extractSource(ele));returnbd;}

parent标签

某个<bean>的父类。这个类用parent的属性,
<bean id="parent" class="com.whj.test.parent.Parent">
<property name="name" value="ouyangfeng"/>
</bean>
<!--下面的parent表示这个child的bean的父亲是id=parent的这个类-->
<bean id="child" class="com.whj.test.parent.Child" parent="parent">
<property name="age" value="18"/>
</bean>

如何进行spring中的默认标签源码解析

ClassPathXmlApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext.xml");Childparent=applicationContext.getBean(Child.class);System.out.println("name="+parent.getName());System.out.println("age="+parent.getAge());打印结果:name=ouyangfengage=18

createBeanDefinition方法:创建 GenericBeanDefinition 实例

parseBeanDefinitionAttributes 方法:解析下列属性

singleton属性<beanclass="com.demo.dao.UserDao"id="userDao"scope="singleton"/>//单例模式只实例化一次scope属性<beanclass="com.demo.dao.UserDao"id="userDao"scope="request"/>//作用域request<beanclass="com.demo.dao.UserDao"id="userDao"scope="session"/>//作用域session<beanclass="com.demo.dao.UserDao"id="userDao"scope="globalsession"/>//作用域globalsession<beanclass="com.demo.dao.UserDao"id="userDao"scope="prototype"/>//每次请求重新实例化一个新的对象abstract属性<beanid="person"class="spring.extend.Person"abstract="true">//定义抽象类spring容器不会为该类创建对象。lazy-init属性<beanid="testBean"calss="cn.itcast.test.TestBean"lazy-init="true"/>//延迟加载,启动时不进行加载,getBean时再加载autowire属性:自动装配<beanid="testBean"calss="cn.itcast.test.TestBean"autowire="byType"/>No:不启动自动装配默认值byName:在springIOC中通过属性的名字的方式查找javaBean依赖的对象并为其注入;byType:在springIOC中通过属性的类型的方式查找javaBean依赖的对象并为其注入default:由上级标签<beans>的default-autowire属性确定constructor:跟buType一样区别在于不是使用Seter方法注入,而是通过构造函数注入dependsOn属性:bean的依赖关系,被依赖的bean启动时先实例化,关闭时后销毁<beanid="beanOne"class="ExampleBean"depends-on="manager,accountDao">beanOne依赖manager和accountDaoautowire-candidate属性:是否自动注入<beanclass="shangbo.spring.example38.MessageServiceDBImpl"autowire-candidate="false"/><beanclass="shangbo.spring.example38.MessageServiceFileImpl"/><beanclass="shangbo.spring.example38.MessagePrinter"autowire="byType"/>

publicclassMessagePrinter{privateMessageServiceservice;publicvoidsetService(MessageServiceservice){this.service=service;}}

MessageServiceDBImpl和MessageServiceFileImpl都是MessageService的实现类,实例化MessagePrinter时只自动注入MessageServiceFileImpl类primary属性:首选<beanclass="shangbo.spring.example38.MessageServiceFileImpl"primary="true"/>当容器类有多个同一类型的bean改类为首选项init-method属性:初始化类时执行指定方法<beanid="testInitMethod"class="com.TestInitMethod"init-method="testInit"></bean>destroy-method属性:销毁时执行指定方法factory-method属性:调用工厂类方法<beanid="bmwCar"class="com.home.factoryMethod.CarStaticFactory"factory-method="getCar"/>ApplicationContext.getBean("bmwCar");//获取bean时调用CarStaticFactory类里的getCar方法factory-bean属性:实例化工厂类<beanid="carFactory"class="com.baobaotao.ditype.CarFactory"/><beanid="car5"factory-bean="carFactory"factory-method="createCar">

parseMetaElements方法

解析meta元素mete元素的使用<beanid="demo"class="com.yhl.myspring.demo.bean.MyBeanDemo"><propertyname="beanName"value="beandemo1"/><metakey="demo"value="demo"/></bean>BeanDefinitionbd;StringbeanCategoriesExpression=(String)bd.getAttribute(CATEGORY_META_KEY);

parseLookupOverrideSubElements方法:解析 lookup-method 标签

publicabstractclassGetBeanCase{publicvoidshowMe(){this.getBean().showMe();}publicabstractUsergetBean();publicclassTeacherextendsUser{publicvoidshowMe(){System.out.println("iisTeacher");}publicclassPersonextendsUser{publicvoidshowMe(){System.out.println("iisPerson");}<beanid="getBeanCase"class="com.sgcc.bean.GetBeanCase"><lookup-methodname="getBean"bean="person"/></bean><beanid="teacher"class="com.sgcc.bean.Teacher"/><beanid="person"class="com.sgcc.bean.Person"/>ApplicationContextcontext=newClassPathXmlApplicationContext("appContext.xml");GetBeanCasegetBeanCase=(GetBeanCase)context.getBean("getBeanCase");getBeanCase.showMe();打印iisPerson

parseReplacedMethodSubElements方法:解析replaced-method标签

ublicclassMyBean{publicvoiddisPlay(){System.out.println("meis原来的method");}publicclassMyBeanReplacerimplementsMethodReplacer{publicObjectreimplement(Objectobj,Methodmethod,Object[]args)throwsThrowable{System.out.println("我替换了原来的method!");returnnull;}<beanid="myBean"class="com.sgcc.bean.MyBean"><replaced-methodname="disPlay"replacer="replacer"/></bean><beanid="replacer"class="com.sgcc.bean.MyBeanReplacer"/></beans>ApplicationContextcontext=newClassPathXmlApplicationContext("replaceMethod.xml");MyBeanmyBean=(MyBean)context.getBean("myBean");myBean.disPlay();打印我替换了原来的method!

上述内容就是如何进行spring中的默认标签源码解析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注恰卡编程网行业资讯频道。

发布于 2021-12-28 22:16:27
收藏
分享
海报
0 条评论
54
上一篇:如何进行flowable工作流引擎Table 'flowable.act_ge_property' doesn't exist 问题分析 下一篇:xml解析失败的示例分析
目录

    0 条评论

    本站已关闭游客评论,请登录或者注册后再评论吧~

    忘记密码?

    图形验证码