本文共 1714 字,大约阅读时间需要 5 分钟。
public void parseLookupOverrideSubElements(Element beanEle, MethodOverrides overrides) { NodeList nl = beanEle.getChildNodes(); for (int i = 0; i < nl.getLength(); i++) { Node node = nl.item(i); //仅当在Spring默认bean的子元素下且为 if (isCandidateElement(node) && nodeNameEquals(node, LOOKUP_METHOD_ELEMENT)) { Element ele = (Element) node; //获取要修饰的方法 String methodName = ele.getAttribute(NAME_ATTRIBUTE); //获取配置返回的bean String beanRef = ele.getAttribute(BEAN_ELEMENT); LookupOverride override = new LookupOverride(methodName, beanRef); override.setSource(extractSource(ele)); overrides.addOverride(override); } } }
lookup-method实现方式说明:
// 定义一个水果类public class Fruit { public Fruit() { System.out.println("I got Fruit"); }}// 苹果public class Apple extends Fruit { public Apple() { System.out.println("I got a fresh apple"); }}// 香蕉public class Bananer extends Fruit { public Bananer () { System.out.println("I got a fresh bananer"); }}// 水果盘,可以拿到水果public abstract class FruitPlate{ // 抽象方法获取新鲜水果 protected abstract Fruit getFruit();}public static void main(String[] args) { ApplicationContext app = new ClassPathXmlApplicationContext("classpath:resource/applicationContext.xml"); FruitPlate fp1= (FruitPlate)app.getBean("fruitPlate1"); FruitPlate fp2 = (FruitPlate)app.getBean("fruitPlate2"); fp1.getFruit(); fp2.getFruit();}测试结果:I got FruitI got a fresh appleI got FruitI got a fresh bananer
转载地址:http://lczeo.baihongyu.com/