您好,登錄后才能下訂單哦!
本篇文章為大家展示了如何進行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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。