本篇文章为大家展示了如何进行spring中的默认标签源码解析,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
try { String parent = null; if (ele.hasAttribute(PARENT_ATTRIBUTE)) {//判断是否有parent标签 parent = ele.getAttribute(PARENT_ATTRIBUTE); } AbstractBeanDefinition bd = 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)); return bd; }
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>ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); Child parent = applicationContext.getBean(Child.class); System.out.println("name="+parent.getName()); System.out.println("age="+parent.getAge()); 打印结果: name=ouyangfeng age=18
createBeanDefinition方法:创建 GenericBeanDefinition 实例
parseBeanDefinitionAttributes 方法:解析下列属性
singleton属性<bean class="com.demo.dao.UserDao" id="userDao" scope="singleton"/>//单例模式只实例化一次scope属性<bean class="com.demo.dao.UserDao" id="userDao" scope="request"/>//作用域request <bean class="com.demo.dao.UserDao" id="userDao" scope="session"/>//作用域session <bean class="com.demo.dao.UserDao" id="userDao" scope="globalsession"/>//作用域globalsession <bean class="com.demo.dao.UserDao" id="userDao" scope="prototype"/>//每次请求重新实例化一个新的对象abstract属性<bean id="person" class="spring.extend.Person" abstract="true">//定义抽象类spring容器不会为该类创建对象。lazy-init属性<bean id="testBean" calss="cn.itcast.test.TestBean" lazy-init="true" />//延迟加载,启动时不进行加载,getBean时再加载autowire属性:自动装配 <bean id="testBean" calss="cn.itcast.test.TestBean" autowire="byType" />No: 不启动自动装配默认值 byName:在spring IOC中通过属性的名字的方式查找javaBean依赖的对象并为其注入; byType:在spring IOC中通过属性的类型的方式查找javaBean依赖的对象并为其注入 default:由上级标签<beans>的default-autowire属性确定 constructor:跟buType一样区别在于不是使用Seter方法注入,而是通过构造函数注入dependsOn属性:bean的依赖关系,被依赖的bean启动时先实例化,关闭时后销毁<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao"> beanOne依赖manager和accountDaoautowire-candidate属性:是否自动注入<bean class="shangbo.spring.example38.MessageServiceDBImpl" autowire-candidate="false" /><bean class="shangbo.spring.example38.MessageServiceFileImpl"/> <bean class="shangbo.spring.example38.MessagePrinter" autowire="byType"/>public class MessagePrinter { private MessageService service; public void setService(MessageService service) { this.service = service; } }MessageServiceDBImpl和MessageServiceFileImpl都是MessageService的实现类, 实例化MessagePrinter时只自动注入MessageServiceFileImpl类primary属性:首选<bean class="shangbo.spring.example38.MessageServiceFileImpl" primary="true"/>当容器类有多个同一类型的bean改类为首选项init-method属性:初始化类时执行指定方法<bean id="testInitMethod" class="com.TestInitMethod" init-method="testInit"></bean> destroy-method属性:销毁时执行指定方法factory-method属性:调用工厂类方法<bean id="bmwCar" class="com.home.factoryMethod.CarStaticFactory" factory-method="getCar"/> ApplicationContext.getBean("bmwCar"); //获取bean时调用CarStaticFactory类里的getCar方法factory-bean属性: 实例化工厂类<bean id="carFactory" class="com.baobaotao.ditype.CarFactory" /> <bean id="car5" factory-bean="carFactory" factory-method="createCar">parseMetaElements方法
解析meta元素 mete元素的使用<bean id="demo" class="com.yhl.myspring.demo.bean.MyBeanDemo"> <property name="beanName" value="bean demo1"/> <meta key="demo" value="demo"/> </bean> BeanDefinition bd; String beanCategoriesExpression = (String)bd.getAttribute(CATEGORY_META_KEY);parseLookupOverrideSubElements方法:解析 lookup-method 标签
public abstract class GetBeanCase { public void showMe(){ this.getBean().showMe(); } public abstract User getBean(); public class Teacher extends User { public void showMe(){ System.out.println("i is Teacher"); } public class Person extends User { public void showMe(){ System.out.println("i is Person"); } <bean id="getBeanCase" class="com.sgcc.bean.GetBeanCase"> <lookup-method name="getBean" bean="person"/> </bean> <bean id="teacher" class="com.sgcc.bean.Teacher"/> <bean id="person" class="com.sgcc.bean.Person"/> ApplicationContext context = new ClassPathXmlApplicationContext("appContext.xml"); GetBeanCase getBeanCase = (GetBeanCase) context.getBean("getBeanCase"); getBeanCase.showMe(); 打印 i is PersonparseReplacedMethodSubElements方法:解析replaced-method标签
ublic class MyBean { public void disPlay(){ System.out.println("me is原来的method"); } public class MyBeanReplacer implements MethodReplacer { public Object reimplement(Object obj, Method method, Object[] args) throws Throwable { System.out.println("我替换了原来的method!"); return null; } <bean id="myBean" class="com.sgcc.bean.MyBean"> <replaced-method name="disPlay" replacer="replacer"/> </bean> <bean id="replacer" class="com.sgcc.bean.MyBeanReplacer"/> </beans> ApplicationContext context = new ClassPathXmlApplicationContext("replaceMethod.xml"); MyBean myBean = (MyBean) context.getBean("myBean"); myBean.disPlay(); 打印 我替换了原来的method!
上述内容就是如何进行spring中的默认标签源码解析,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。