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

白话Spring(基础篇)---方法注入与方法替换

2016-03-01 20:07 645 查看
[一知半解,就是给自己挖坑]

本文我们将来介绍Spring中方法的注入与替换。按照惯例,首先我们来准备一下我们的开发环境:

a.操作系统:win7 x64

b.开发工具:eclipse mars j2ee版本,maven3.3.2,Spring 4,junit4.12

c.复制Spring05工程,重命名为Spring06工程。工程结构不变。

-------------------------------------------------------------------------------------------------------------------------------------------------------

正文开始:

一.方法注入

1.首先,我们先来修改单元测试方法的内容如下:

@org.junit.Test
public void test1() {
System.out.println("Start Test()");
Record customer1 = (Record)ac.getBean("record2");
Record customer2 = (Record)ac.getBean("record2");
System.out.println(customer1==customer2);
}
2.修改beans.xml配置文件,具体内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="Customer1" class="com.java.ingo.entity.Customer">
<property name="name" value="Tom"></property>
<property name="sex" value="male"></property>
<property name="age" value="22"></property>
</bean>
<bean id="record" class="com.java.ingo.entity.Record">
<property name="company" value="ABCD"></property>
<property name="position" value="Engineer"></property>
<property name="address" value="Beijing"></property>
</bean>
<bean id="record2" class="com.java.ingo.entity.Record">
<property name="company" value="ABCD"></property>
<property name="position" value="Engineer"></property>
<property name="address" value="Beijing"></property>
</bean>
</beans>
3.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。

运行之后,我们就会发现:Spring所管理的bean是单例模式的,即,前后两次取得的对象是完全相同的。

4.我们在id为record2的bean修改为如下内容:点击保存

<bean id="record2" class="com.java.ingo.entity.Record" scope="prototype">
<property name="company" value="ABCD"></property>
<property name="position" value="Engineer"></property>
<property name="address" value="Beijing"></property>
</bean>
5.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。

运行之后,我们发现:输出false。即两次取得的结果是不相同的。说明,每次获取的bean都是新生成的。

-------------------------

特别的:

6.我们修改id为Customer1的bean为如下内容:

<bean id="Customer1" class="com.java.ingo.entity.Customer" scope="prototype">
<property name="name" value="Tom"></property>
<property name="sex" value="male"></property>
<property name="age" value="22"></property>
<property name="record" ref="record"></property>
</bean>
7.修改测试方法为如下内容:

@org.junit.Test
public void test1() {
System.out.println("Start Test()");
Customer customer1 = (Customer)ac.getBean("Customer1");
Customer customer2 = (Customer)ac.getBean("Customer1");
System.out.println(customer1==customer2);
System.out.println(customer1.getRecord()==customer2.getRecord());
}
8.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。

运行之后,我们发现,第一行输出为false,但是第二行输出为true。说明Spring在底层,讲我们的一个record属性都注入两个Customer对象。

9.我们在将Customer1中的ref="record"修改为ref="record2",再运行单元测试方法。

运行之后,我们发现,第一行输出为false,第二行输出也是false。说明这一次两个对象都是每次新生成一个对象。

10.由此,我们得出下面的结论:Spring默认的为所有的bean设置为单例模式,只有当我们特别声明bean的模式时,Spring才会修改对应的bean设置,即使存在嵌套关系bean中,外层声明为prototype,其内部在未声明的情况下,仍然是单例的。

-------------------------------------------------------------------------------------------------------------------------------------------------------

除了上面介绍的声明类型的方式之外,我们还可以采用下面的方式来获取与prototype等同效果的bean。但,下面的方法仅作为了解知识。

-------------------------------------------------------------------------------------------------------------------------------------------------------

1.修改Customer.java文件为如下内容:

package com.java.ingo.entity;

/**
*@author 作者 E-mail:ingo
*@version 创建时间:2016年2月28日下午4:00:35
*类说明
*/
public abstract class Customer {
private String name;
private String sex;
private int age;
private Record record;

public Customer() {
super();
}

public Customer(String name, String sex, int age, Record record) {
super();
this.name = name;
this.sex = sex;
this.age = age;
this.record = record;
}

public Customer(Record record) {
super();
this.record = record;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public abstract Record getRecord() ;

public void setRecord(Record record) {
this.record = record;
}

@Override
public String toString() {
return "Customer [name=" + name + ", sex=" + sex + ", age=" + age + ", record=" + record.toString() + "]";
}
}


2.修改beans.xml为如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="Customer1" class="com.java.ingo.entity.Customer">
<property name="name" value="Tom"></property>
<property name="sex" value="male"></property>
<property name="age" value="22"></property>
<lookup-method name="getRecord" bean="record"></lookup-method>
</bean>
<bean id="record" class="com.java.ingo.entity.Record" scope="prototype">
<property name="company" value="ABCD"></property>
<property name="position" value="Engineer"></property>
<property name="address" value="Beijing"></property>
</bean>
</beans>
3.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。

运行之后,我们发现:这是及时两个Customer是相同的,但是这是的Record是不同的。作为扩展,读者也可以在id为Customer1的bean上加入prototype,运行之后再查看结果,对于与上文运行时的差异点。

---------------------------------------------------------------------------------------------------------------------------------------------------

---------------------------------------------------------------------------------------------------------------------------------------------------

二.方法替换

1.为了方便演示,我们这里直接固定Customer1对象的Record属性,具体方法如下:

package com.java.ingo.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
*@author 作者 E-mail:ingo
*@version 创建时间:2016年2月28日下午4:00:35
*类说明
*/
public class Customer {
private String name;
private String sex;
private int age;
private Record record;
<span style="white-space:pre">	</span>//构造函数,其他的set.get方法请读者自行填充
public Record getRecord() {
Record rcd = new Record();
rcd.setAddress("Shenzhen");
rcd.setCompany("Tencent");
rcd.setPosition("Engineer");
return rcd;
}
@Override
public String toString() {
return "Customer [name=" + name + ", sex=" + sex + ", age=" + age + ", record=" + record.toString() + "]";
}
}


2.创建Customer2.java文件,具体内容如下:

package com.java.ingo.entity;

import java.lang.reflect.Method;

import org.springframework.beans.factory.support.MethodReplacer;

/**
*@author 作者 E-mail:ingo
*@version 创建时间:2016年2月28日下午4:00:35
*类说明
*/
public class Customer2 implements MethodReplacer{

public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
Record rcd = new Record();
rcd.setAddress("Hangzhou");
rcd.setCompany("Alibaba");
rcd.setPosition("Engineer");
return rcd;
}
}


3.修改beans.xml文件为如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="Customer1" class="com.java.ingo.entity.Customer">
<property name="name" value="Tom"></property>
<property name="sex" value="male"></property>
<property name="age" value="22"></property>
<replaced-method name="getRecord" replacer="Customer2"></replaced-method>
</bean>
<bean id="Customer2" class="com.java.ingo.entity.Customer2"></bean>
</beans>
4.修改单元测试方法如下:

@org.junit.Test
public void test1() {
System.out.println("Start Test()");
Customer customer1 = (Customer)ac.getBean("Customer1");
System.out.println(customer1.getRecord());
}
5.测试方法:在test1方法上,右键run as--->junit test。观察控制台输出结果即可。

运行之后,我们发现,在Customer1中固定的Record已经被替换为Customer2中的值。

------------------------------------------------------------------------------------------------------------------------------------------------------

至此,白话Spring(基础篇)---方法注入与方法替换结束

参考资料:

Spring官网:http://spring.io/docs

其他博文:有点多,感谢各位开源世界的大神!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: