`
zhangwei_david
  • 浏览: 468870 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring lookup-method,replace-method,factory-method使用示例

 
阅读更多

    在这里会介绍如下知识点:

  1. factory-method
  2. factory-bean
  3. lookup-method
  4. replace-method

 

 factory-method

    在Spring中如果对一个bean的创建使用工厂方法模式就可以使用factory-method

/**
 * factory-method  demo
 * @author zhangwei_david
 * @version $Id: FactoryMethodDemo.java, v 0.1 2015年7月8日 下午11:03:58 zhangwei_david Exp $
 */
public class FactoryMethodDemo {

    public static Person getPerson() {
        return new Person() {

            @Override
            public String getSex() {
                return "male";
            }
        };
    }
}

 

<bean id="male" class="com.cathy.demo.spring.FactoryMethodDemo"
		factory-method="getPerson" />

 

 

 factory-bean

 

   在上一个示例中class指向的是一个工厂方法类,如果这个工厂方法类对多个bean中都有使用,可以通过factory-bean 进行优化处理

/**
 *
 * @author zhangwei_david
 * @version $Id: FactoryBeanDemo.java, v 0.1 2015年7月8日 下午10:08:58 zhangwei_david Exp $
 */
public class FactoryBeanDemo {

    public Man getMan() {
        return new Man();
    }

    public Person getPerson() {
        System.out.println("getPerson");
        return new Person() {

            @Override
            public String getSex() {
                return "femal";
            }
        };
    }
}

 

<bean id="factoryBeanDemo" class="com.cathy.demo.spring.FactoryBeanDemo" />
	<bean id="person" factory-bean="factoryBeanDemo" factory-method="getPerson" />

 

 

 lookup-method

              在Spring中多数的bean都是单例的,但是如果有一个单例bean需要引用一个prototype bean怎么处理了,不可能每次都需要查询加载bean吧。在这样的场景下lookup-method可以很好的解决。

 

/**
 *  lookup-method使用示例
 *  
 * @author zhangwei_david
 * @version $Id: LookupMethodDemo.java, v 0.1 2015年7月8日 下午2:55:38 zhangwei_david Exp $
 */
public abstract class LookupMethodDemo {
    
    
    protected Man man = getMan();

    /**抽象方法**/
    public abstract Man getMan();

    public int getId() {
        return man.getId();
    }

}

 

/**
 *
 * @author zhangwei_david
 * @version $Id: Man.java, v 0.1 2015年6月7日 下午6:46:30 zhangwei_david Exp $
 */
public class Man extends Person implements InitializingBean {

    private int id = (int) (Math.random() * 100);

    /**
     * @see com.cathy.demo.spring.Person#getSex()
     */
    @Override
    public String getSex() {
        return "Male";
    }

    /**
     * Getter method for property <tt>id</tt>.
     *
     * @return property value of id
     */
    public int getId() {
        return id;
    }

    /**
     * Setter method for property <tt>id</tt>.
     *
     * @param id value to be assigned to property id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
     */
    public void afterPropertiesSet() throws Exception {
        System.out.println(" 初始化 Man [id=" + getId() + "]");
    }

}

 

<bean id="lookupMethodDemo" class="com.cathy.demo.spring.LookupMethodDemo">
		<lookup-method name="getMan" bean="man" />
	</bean>
	<bean id="man" class="com.cathy.demo.spring.Man" scope="prototype" />

 

/**
 *
 * @author zhangwei_david
 * @version $Id: MySpringTest.java, v 0.1 2015年6月7日 下午5:50:50 zhangwei_david Exp $
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:META-INF/spring/test-beans.xml")
public class MySpringTest {

    @Autowired
    private LookupMethodDemo lookupMethodDemo;

    @Autowired
    private Man              man;


    @Test
    public void test() {
        System.out.println(man.getId());
        System.out.println(lookupMethodDemo.getId());

    }

}

 

 

测试的结果是:

 

 初始化 Man [id=4]
 初始化 Man [id=13]
 初始化 Man [id=48]
48
13

 

 

  replace-method

     我们知道在Spring中可以使用AOP增强一个方法也可以替换一个方法的功能,可是有不少人却不知道在Spring中还有更简单的方式,就是使用replece-method替换一个方法。

 

/**
 *  一个方法替换的简单示例
 * @author zhangwei_david
 * @version $Id: ReplacMethodDemo.java, v 0.1 2015年7月8日 下午10:26:22 zhangwei_david Exp $
 */
public class ReplaceMethodDemo {

    public String getName() {
        return "original";
    }

}

 

/**
 *
 * @author zhangwei_david
 * @version $Id: Replacer.java, v 0.1 2015年7月8日 下午10:27:18 zhangwei_david Exp $
 */
public class Replacer implements MethodReplacer {

    /**
     * @see org.springframework.beans.factory.support.MethodReplacer#reimplement(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
     */
    public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
        return "replacer";

    }

}

 

<bean id="replacer" class="com.cathy.demo.spring.Replacer" />
	<!-- 方法替换的demo -->
	<bean id="replaceMethodDemo" class="com.cathy.demo.spring.ReplaceMethodDemo">
		<replaced-method name="getName" replacer="replacer" />
	</bean>

  通过调用replceMethodDemo.getName()的结果是:replacer

 

3
1
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics