Spring 的注入方式有很多种,方法的注入方式更加体现了Spring容器覆盖容器管理的bean上的方法的能力,方法注入的方式也是多种,本例以Lookup方式,采用xml配置和注解两种不同的方式。

使用Lookup方法注入需要注意:
1.为了使这个动态子类化工作,Spring bean容器将子类化的类不能是final,并且要重写的方法也不能是final。
2.注入的方法需要有返回值。
3.方法必须是public或protected修饰
4.必须是无参数的方法
方法格式如下:

1
<public|protected> [abstract] <return-type> theMethodName(no-arguments);

使用的技术及工具:
1.eclipse 4.4.2
2.Maven 3.1
3.JDK 1.7
4.Spring 4.3.5
5.Junit 4.12

XML配置的方式:

注:实例一和二都是使用相同的配置

1.pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.devnp</groupId>
<artifactId>Spring4Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Spring4Demo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.5.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

2.Java 代码

HelloService.java

1
2
3
4
5
6
7
8
package com.devnp.SpringDemo;

public class HelloService {

public void print(){
System.out.println("Hello World.");
}
}

MethodDIDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.devnp.SpringDemo.method.DI;

import com.devnp.SpringDemo.HelloService;

public abstract class MethodDIDemo {

public void process(){
HelloService helloService = createHelloService();

helloService.print();
}

protected abstract HelloService createHelloService();
}

3.xml 配置

1
2
3
4
5
<bean id="helloService" class="com.devnp.SpringDemo.HelloService"></bean>

<bean id="methodDIDemo" class="com.devnp.SpringDemo.method.DI.MethodDIDemo">
<lookup-method name="createHelloService" bean="helloService" />
</bean>

4.Junit 测试

MethodDIDemoTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.devnp.SpringDemo.method.DI;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class MethodDIDemoTest {

@Autowired
private MethodDIDemo methodDIDemo ;

@Test
public void processTest(){
methodDIDemo.process();
}

}

5.结果

Hello World.

实例二 采用注解的形式

@Lookup(“helloService”)

1.java 代码

MethodDIDemo2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.devnp.SpringDemo.method.DI;

import org.springframework.beans.factory.annotation.Lookup;

import com.devnp.SpringDemo.HelloService;

public abstract class MethodDIDemo2 {

public void process(){
HelloService helloService = createHelloService();

helloService.print();
}

@Lookup("helloService")
protected abstract HelloService createHelloService();
}

2.xml 配置

1
<bean id="methodDIDemo2" class="com.devnp.SpringDemo.method.DI.MethodDIDemo2"/>

3.Junit 测试

MethodDIDemo2Test.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.devnp.SpringDemo.method.DI;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class MethodDIDemo2Test {

@Autowired
private MethodDIDemo2 methodDIDemo2 ;

@Test
public void processTest(){
methodDIDemo2.process();
}

}

4.运行结果

Hello World.

代码下载:

Spring4Demo.zip