zoukankan      html  css  js  c++  java
  • Spring的lookup-method标签

    Spring的解析源码

        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的使用

    lookup-method实现方式说明:

    <bean class="beanClass">
        <lookup-method name="method" bean="non-singleton-bean"/>
    </bean>
    // 定义一个水果类
    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();
    }
    <!-- 这是2个非单例模式的bean -->
    <bean id="apple" class="Apple" scope="prototype"/>
    <bean id="bananer" class="Bananer " scope="prototype"/>
    <bean id="fruitPlate1" class="FruitPlate">
        <lookup-method name="getFruit" bean="apple"/>
    </bean>
    <bean id="fruitPlate2" class="FruitPlate">
        <lookup-method name="getFruit" bean="bananer"/>
    </bean>
    
    
    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 Fruit
    I got a fresh apple
    I got Fruit
    I got a fresh bananer
  • 相关阅读:
    十个html5代码片段,超实用,一定要收藏
    零基础学编程,我想给你这五条建议
    Java 程序员开发常用的工具(二)
    Java 程序员开发常用的工具(一)
    前端人才饱和?平均年薪25W难求优质程序员!
    java基础学习 了解这些很有必要
    初学HTML5技术开发笔记整理
    HTML5移动开发学习笔记之02-CH4-HTML5 Web表单
    web前端笔记之Web前端的安全与漏洞
    5.SSH 免密码登陆
  • 原文地址:https://www.cnblogs.com/wade-luffy/p/6068339.html
Copyright © 2011-2022 走看看