您的位置:首页 > 编程语言 > Java开发

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
lookup-method
attribute.


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 calling
applicationContext.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,
PizzaShop
is
an abstract class and has two method
makePizza
and
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
}
As you can see our example is very simple.
Pizza
has
a static
count
variable
which gets incremented as we create a new instance. It has a boolean member
isVeg
which
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
}
We will configure
makePizza
and
makeVeggiePizza
as
lookup-method
s.
We have configures a normal pizza and a veggie pizza beans. Each abstract method will have one <lookup-method element. The
name
attribute
will be the method name and the bean will point to the bean configured. Here we have configured both
pizza
and
veggiePizza
as
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
>
Let’ snow test it. We will first load the context and get the
PizzaShop
bean.
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
Pizza:"
+
firstPizza);
13
14
Pizza
secondPizza = pizzaShop.makePizza();
15
System.out.println(
"Second
Pizza:"
+
secondPizza);
16
 
17
Pizza
veggiePizza = pizzaShop.makeVeggiePizza();
18
System.out.println(
"Veggie
Pizza:"
+
veggiePizza);
19
}
finally
{
20
context.close();
21
}
22
}
23
}
Each time we invoke the method, it create a new
Pizza
bean,
we can see the count getting incremented.

Output:

view
sourceprint?

1
First
Pizza:A new Pizza, count(1)
2
Second
Pizza:A new Pizza, count(2)
3
Veggie
Pizza:A new veggiePizza, count(3)


Download the source code

This was an example about spring method injection using
lookup-method
attribute.
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: