Spring annotation 之lookup (方法注入)
来源:互联网 发布:在淘宝上买steam游戏 编辑:程序博客网 时间:2024/05/16 04:02
Spring lookup-method Example
When a bean has dependency on another bean, we inject the bean using the setter property or through the constructor.
The getter method will return us the reference that is been set but suppose you want a new instance of the dependent bean each time you invoke the getter method, then you will probably have to follow a different approach.
In this article, we will see an example of method injection using
attribute.Dependencies
Add the following dependencies:
pom.xml:
Method Injection Approaches
How do we get a new instance each time we invoke the getter method? One approach would be to define the dependent bean as prototype and then implement the getter method to return us a new instance calling
Issue with this approach is that now your dependent on the .
Second approach is let container manage the method injection. The getter method can be abstract, let spring dynamically sub-class the class containing the getter method and implement it to return the configured bean. This way, we can use the same base class and deploy it in different ways to returns us different beans without the need of changing the code.
Method injection using lookup-method
The method in question doesn’t need to be a getter method but it should be a method that returns something. In our example,
is an abstract class and has two method and which returns us the veggie .PizzaShop:
As you can see our example is very simple.
has a static variable which gets incremented as we create a new instance. It has a boolean member which will be true if the pizza is vegetarian.Pizza:
We will configure
and as s. We have configures a normal pizza and a veggie pizza beans. Each abstract method will have one <lookup-method element. The attribute will be the method name and the bean will point to the bean configured. Here we have configured both and as prototype beans.applicationContext.xml:
Let’ snow test it. We will first load the context and get the
bean. Next, we will make calls and .SpringLookupMethodExample:
Each time we invoke the method, it create a new
bean, we can see the count getting incremented.Output:
Download the source code
This was an example about spring method injection using springLookupMethodExample.zip
http://www.javacodegeeks.com/2015/05/spring-lookup-method-example.html attribute. You can download the source code here:Example in my project
<bean id="resourceGroupResource"class="com.polycom.cloudAxis.rest.services.ResourceGroupResource" /><bean id="resourceGroupCollectionResource"class="com.polycom.cloudAxis.rest.services.ResourceGroupCollectionResource"><lookup-method name="createNewResourceGroupResource"bean="resourceGroupResource" /><aop:scoped-proxy proxy-target-class="true" /></bean>
- Spring annotation 之lookup (方法注入)
- spring Lookup方法注入
- spring 的 lookup方法注入
- spring 方法注入 lookup方法注入
- 码农小汪-spring框架学习之3-spring Method injection(方法注入 ) Lookup method injection
- Spring的方法注入lookup-method(转)
- 快速上手Spring--9. Lookup方法注入
- 快速上手Spring--9. Lookup方法注入
- Spring内核研究-Lookup方法注入
- 快速上手Spring--9. Lookup方法注入
- 快速上手Spring--9. Lookup方法注入
- 快速上手Spring--9.Lookup方法注入
- Spring的方法注入lookup-method
- Spring-方法注入lookup、方法替换MethodReplacer接口
- lookup方法注入
- 使用Spring(八) 延迟初始化bean、Lookup方法注入
- Spring(07)——单例注入多例之lookup-method
- Spring基础之三:annotation配置注入
- hdu 5493
- 一道小学生题目:红星+星红+星红=爱星星,问:爱、红、星各代表数字几?
- dede 列表页 上一页下一页 只要链接
- 如何删除微软拼音输入法2007
- 在fragment中添加上下文菜单
- Spring annotation 之lookup (方法注入)
- MFC消息机制逆向追踪
- 用jquery动画实现的简单的图片轮播
- textview属性
- Python DeprecationWarning 类型错误
- DriverStudio工具包介绍
- Xilinx实现基于BlockRAM的高效移位寄存器
- State Threads 回调终结者
- jsp与servlet的联系