Spring annotation 之lookup (方法注入)
2015-09-29 18:17
453 查看
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
Add the following dependencies:
pom.xml:
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.
The method in question doesn’t need to be a getter method but it should be a method that returns something. In our example,
an abstract class and has two method
returns us the veggie
PizzaShop:
As you can see our example is very simple.
a static
which gets incremented as we create a new instance. It has a boolean member
will be true if the pizza is vegetarian.
Pizza:
We will configure
We have configures a normal pizza and a veggie pizza beans. Each abstract method will have one <lookup-method element. The
will be the method name and the bean will point to the bean configured. Here we have configured both
prototype beans.
applicationContext.xml:
Let’ snow test it. We will first load the context and get the
Next, we will make calls
SpringLookupMethodExample:
Each time we invoke the method, it create a new
we can see the count getting incremented.
Output:
view
sourceprint?
This was an example about spring method injection using
You can download the source code here:springLookupMethodExample.zip http://www.javacodegeeks.com/2015/05/spring-lookup-method-example.html
Example in my project
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
lookup-methodattribute.
Dependencies
Add the following dependencies:spring-core
spring-context
spring-beans
pom.xml:
01 | < project xmlns = "http://maven.apache.org/POM/4.0.0" xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
02 | xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/maven-4.0.0.xsd" > |
03 | < modelVersion >4.0.0</ modelVersion > |
04 | < groupId >com.javarticles.spring</ groupId > |
05 | < artifactId >springLookupMethodExample</ artifactId > |
06 | < version >0.0.1-SNAPSHOT</ version > |
07 |
08 | < dependencies > |
09 | < dependency > |
10 | < groupId >org.springframework</ groupId > |
11 | < artifactId >spring-core</ artifactId > |
12 | < version >${spring.version}</ version > |
13 | </ dependency > |
14 | < dependency > |
15 | < groupId >org.springframework</ groupId > |
16 | < artifactId >spring-context</ artifactId > |
17 | < version >${spring.version}</ version > |
18 | </ dependency > |
19 | < dependency > |
20 | < groupId >org.springframework</ groupId > |
21 | < artifactId >spring-beans</ artifactId > |
22 | < version >${spring.version}</ version > |
23 | </ dependency > |
24 | </ dependencies > |
25 |
26 | < properties > |
27 | < spring.version >3.2.3.RELEASE</ spring.version > |
28 | </ properties > |
29 |
30 | </ project > |
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 callingapplicationContext.getBean(beanId).
Issue with this approach is that now your dependent on the
applicationContext.
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, PizzaShopis
an abstract class and has two method
makePizzaand
makeVeggiesPizza()which
returns us the veggie
Pizza.
PizzaShop:
1 | package com.javarticles.spring; |
2 |
3 | public abstract class PizzaShop { |
4 | public abstract Pizza makePizza(); |
5 | public abstract Pizza makeVeggiePizza(); |
6 | } |
Pizzahas
a static
countvariable
which gets incremented as we create a new instance. It has a boolean member
isVegwhich
will be true if the pizza is vegetarian.
Pizza:
01 | package com.javarticles.spring; |
02 |
03 | import java.util.concurrent.atomic.AtomicLong; |
04 |
05 | public class Pizza { |
06 | private static AtomicLong count = new AtomicLong( 0 ); |
07 | private boolean isVeg; |
08 |
09 | public Pizza() { |
10 | count.incrementAndGet(); |
11 | } |
12 | public String toString() { |
13 | return "A new " + (isVeg ? "veggie" : "" ) + "Pizza, count(" + count.get() + ")" ; |
14 | } |
15 | public void setIsVeg( boolean veg) { |
16 | isVeg = veg; |
17 | } |
18 | } |
makePizzaand
makeVeggiePizzaas
lookup-methods.
We have configures a normal pizza and a veggie pizza beans. Each abstract method will have one <lookup-method element. The
nameattribute
will be the method name and the bean will point to the bean configured. Here we have configured both
pizzaand
veggiePizzaas
prototype beans.
applicationContext.xml:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
03 | < beans xmlns = "http://www.springframework.org/schema/beans" |
04 | xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:schemaLocation = "http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd" > |
06 |
07 | < bean id = "pizzaShop" class = "com.javarticles.spring.PizzaShop" > |
08 | < lookup-method name = "makePizza" bean = "pizza" /> |
09 | < lookup-method name = "makeVeggiePizza" bean = "veggiePizza" /> |
10 | </ bean > |
11 |
12 | < bean id = "pizza" class = "com.javarticles.spring.Pizza" scope = "prototype" /> |
13 |
14 | < bean id = "veggiePizza" class = "com.javarticles.spring.Pizza" scope = "prototype" > |
15 | < property name = "isVeg" value = "true" /> |
16 | </ bean > |
17 |
18 | </ beans > |
PizzaShopbean.
Next, we will make calls
pizzaShop.makePizza()and
pizzaShop.makeVeggiePizza().
SpringLookupMethodExample:
01 | package com.javarticles.spring; |
02 |
03 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
04 |
05 | public class SpringLookupMethodExample { |
06 | public static void main(String[] args) { |
07 | ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext( |
08 | "applicationContext.xml" ); |
09 | try { |
10 | PizzaShop pizzaShop = (PizzaShop) context.getBean( "pizzaShop" ); |
11 | Pizza firstPizza = pizzaShop.makePizza(); |
12 | System.out.println( "First + firstPizza); |
13 |
14 | Pizza secondPizza = pizzaShop.makePizza(); |
15 | System.out.println( "Second + secondPizza); |
16 |
17 | Pizza veggiePizza = pizzaShop.makeVeggiePizza(); |
18 | System.out.println( "Veggie + veggiePizza); |
19 | } finally { |
20 | context.close(); |
21 | } |
22 | } |
23 | } |
Pizzabean,
we can see the count getting incremented.
Output:
view
sourceprint?
1 | First |
2 | Second |
3 | Veggie |
Download the source code
This was an example about spring method injection using lookup-methodattribute.
You can download the source code here:springLookupMethodExample.zip http://www.javacodegeeks.com/2015/05/spring-lookup-method-example.html
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>
相关文章推荐
- Java连接SQLServer的相关配置
- java基本类型和包装器类【转】
- 详解Java的文件与目录管理以及输入输出相关操作
- Struts-自定义的MyStruts框架
- Java Web程序工作原理
- Java中的“虚函数”,抽象类的抽象函数
- MyEclipse连接MySQL数据库中文乱码问题
- java 替换反斜杠 Unexpected internal error near index 1
- java7:数组
- MyEclipse在导入已有包时报红色下划线
- Struts 2 validator param list
- Spring整合Quartz,并持久化Job到MySQL
- WWW-SSH
- Java____内部类学习整理
- Java中字符流与字节流的区别
- Java修改数组长度
- Spring MVC入门
- java 字节流和字符流的区别
- Java中基本类型数组默认值
- 经典算法之动态规划(一):入门级动态规划