Top

JAVA Spring MyBatis DAY01

  1. 实例化Spring容器
  2. 使用Spring容器创建bean
  3. bean的作用域
  4. bean的生命周期
  5. Setter注入
  6. 构造器注入
  7. 自动装配

1 实例化Spring容器

1.1 问题

使用ApplicationContext的方式实例化Spring容器。

1.2 方案

使用ApplicationContext的方式实例化Spring容器的核心代码如下:

String conf = "applicationContext.xml";
ApplicationContext ac = 
new ClassPathXmlApplicationContext(conf);

1.3 步骤

步骤一:创建项目,导入jar包

创建项目Spring01,导入Spring开发包,如下图所示:

图-1

步骤二:导入Spring配置文件

导入applicationContext.xml,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
</beans>

步骤三:写测试代码

创建测试类TestCase,并增加测试方法test1,代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
}

步骤四:执行测试

运行测试方法test1,效果如下图所示:

图-2

1.4 完整代码

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
}

2 使用Spring容器创建bean

2.1 问题

演示如何使用Spring容器创建bean。

2.2 方案

使用Spring容器创建bean有3种方式:

2.3 步骤

步骤一:声明bean

在applicationContext.xml中,通过<bean>声明bean,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!--通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!--通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!--通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
</beans>

步骤二:写测试代码

在TestCase中增加测试方法test2,在此方法中通过Spring容器来创建声明的bean,代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {

	//其他方法略
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}

}

步骤三:执行测试

运行测试方法test2,效果如下图所示:

图-3

2.4 完整代码

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!-- 通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!-- 通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}
	
}

3 bean的作用域

3.1 问题

本案例探讨通过Spring容器创建bean时,bean的作用域。

3.2 方案

3.3 步骤

步骤一:声明bean

在applicationContext.xml中,新声明一个bean,追加的代码如下:

	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" />

步骤二:写测试代码

在TestCase中追加测试方法test3,测试新声明的bean是否单例,追加代码如下:

	/**
	 * bean的作用域
	 */
	@Test
	public void test3() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Calendar cal1 = (Calendar) ctx.getBean("obj5");
		Calendar cal2 = (Calendar) ctx.getBean("obj5");
		System.out.println(cal1==cal2);
	}

步骤三:执行测试

执行测试方法test3,效果如下图所示:

图-4

步骤四:修改bean声明

修改applicationContext.xml中新声明的bean,增加scope=”prototype”,修改代码如下:

	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>

步骤五:执行测试

执行测试方法test3,效果如下图所示:

图-5

3.4 完整代码

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!-- 通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!-- 通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>
	
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}
	
		/**
		 * bean的作用域
		 */
		@Test
		public void test3() {
			String cfg = "applicationContext.xml";
			ApplicationContext ctx = 
				new ClassPathXmlApplicationContext(cfg);
			Calendar cal1 = (Calendar) ctx.getBean("obj5");
			Calendar cal2 = (Calendar) ctx.getBean("obj5");
			System.out.println(cal1==cal2);
		}
	
}

4 bean的生命周期

4.1 问题

本案例探讨bean的生命周期,以及bean的延迟实例化。

4.2 方案

Spring容器在创建bean时,可以帮忙管理bean的生命周期,即管理bean的初始化及销毁的方法。

默认情况下Spring容器创建时,会创建单例的对象,也可以让Spring容器延迟实例化这些单例的对象。

4.3 步骤

步骤一:创建bean

创建一个类ExampleBean,代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class ExampleBean implements Serializable {
	
	public ExampleBean() {
		System.out.println("实例化ExampleBean:" + this);
	}
	
	public void init() {
		System.out.println("初始化ExampleBean");
	}
	
	public void destroy() {
		System.out.println("销毁ExampleBean");
	}
	
	public void execute() {
		System.out.println("执行execute方法");
	}
	
}

步骤二:声明bean

在applicationContext.xml中声明这个bean,追加代码如下:

	<!-- bean的生命周期 -->
	<!-- bean的延迟实例化 -->
	<bean id="exampleBean" class="com.tarena.bean.ExampleBean"
		init-method="init" destroy-method="destroy" />

步骤三:写测试代码

在TestCase中增加测试方法test4,追加代码如下:

	/**
	 * 1.bean的生命周期;
	 * 2.bean的延迟实例化;
	 */
	@Test
	public void test4() {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println("----------------");
		ExampleBean bean = 
			ctx.getBean("exampleBean", ExampleBean.class);
		bean.execute();
		ctx.close();
	}

步骤四:执行测试

执行测试方法test4,效果如下图:

图-6

步骤五:修改声明的bean

修改applicationContext.xml中新声明的bean,修改代码如下:

	<!-- bean的生命周期 -->
	<!-- bean的延迟实例化 -->
	<bean id="exampleBean" class="com.tarena.bean.ExampleBean"
		init-method="init" destroy-method="destroy" lazy-init="true"/>

步骤六:执行测试

再次执行test4方法,效果如下图所示:

图-7

4.4 完整代码

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!-- 通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!-- 通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>
	
	<!-- bean的生命周期 -->
	<!-- bean的延迟实例化 -->
	<bean id="exampleBean" class="com.tarena.bean.ExampleBean"
		init-method="init" destroy-method="destroy" lazy-init="true"/>
		
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}
	
		/**
		 * bean的作用域
		 */
		@Test
		public void test3() {
			String cfg = "applicationContext.xml";
			ApplicationContext ctx = 
				new ClassPathXmlApplicationContext(cfg);
			Calendar cal1 = (Calendar) ctx.getBean("obj5");
			Calendar cal2 = (Calendar) ctx.getBean("obj5");
			System.out.println(cal1==cal2);
		}
	
	/**
	 * 1.bean的生命周期;
	 * 2.bean的延迟实例化;
	 */
	@Test
	public void test4() {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println("----------------");
		ExampleBean bean = 
			ctx.getBean("exampleBean", ExampleBean.class);
		bean.execute();
		ctx.close();
	}
	
}

5 Setter注入

5.1 问题

本案例探讨如何使用setter注入的方式为bean注入值。

5.2 方案

通过setter注入的方式,为bean注入值。

5.3 步骤

步骤一:创建bean

创建计算机类Computer,代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class Computer implements Serializable {

	private String mainboard; // 主板
	private String hdd; // 硬盘
	private String ram; // 内存

	public String getMainboard() {
		return mainboard;
	}

	public void setMainboard(String mainboard) {
		this.mainboard = mainboard;
	}

	public String getHdd() {
		return hdd;
	}

	public void setHdd(String hdd) {
		this.hdd = hdd;
	}

	public String getRam() {
		return ram;
	}

	public void setRam(String ram) {
		this.ram = ram;
	}

}

步骤二:声明bean

在applicationContext.xml中声明这个bean,追加代码如下:

	<!-- setter注入 -->
	<bean id="computer" class="com.tarena.bean.Computer">
		<property name="mainboard" value="技嘉"/>
		<property name="hdd" value="希捷"/>
		<property name="ram" value="金士顿"/>
	</bean>

步骤三:写测试代码

在TestCase中增加测试方法test5,追加代码如下:

	/**
	 * setter注入
	 */
	@Test
	public void test5() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Computer computer = 
			ctx.getBean("computer", Computer.class);
		System.out.println(computer.getMainboard());
		System.out.println(computer.getHdd());
		System.out.println(computer.getRam());
	}

步骤四:执行测试

执行测试方法test5,效果如下图所示:

图-8

5.4 完整代码

Computer完整代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class Computer implements Serializable {

	private String mainboard; // 主板
	private String hdd; // 硬盘
	private String ram; // 内存

	public String getMainboard() {
		return mainboard;
	}

	public void setMainboard(String mainboard) {
		this.mainboard = mainboard;
	}

	public String getHdd() {
		return hdd;
	}

	public void setHdd(String hdd) {
		this.hdd = hdd;
	}

	public String getRam() {
		return ram;
	}

	public void setRam(String ram) {
		this.ram = ram;
	}

}

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!-- 通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!-- 通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>
	
	<!-- bean的生命周期 -->
	<!-- bean的延迟实例化 -->
	<bean id="exampleBean" class="com.tarena.bean.ExampleBean"
		init-method="init" destroy-method="destroy" lazy-init="true"/>
		
	<!-- setter注入 -->
	<bean id="computer" class="com.tarena.bean.Computer">
		<property name="mainboard" value="技嘉"/>
		<property name="hdd" value="希捷"/>
		<property name="ram" value="金士顿"/>
	</bean>
	
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}
	
		/**
		 * bean的作用域
		 */
		@Test
		public void test3() {
			String cfg = "applicationContext.xml";
			ApplicationContext ctx = 
				new ClassPathXmlApplicationContext(cfg);
			Calendar cal1 = (Calendar) ctx.getBean("obj5");
			Calendar cal2 = (Calendar) ctx.getBean("obj5");
			System.out.println(cal1==cal2);
		}
	
	/**
	 * 1.bean的生命周期;
	 * 2.bean的延迟实例化;
	 */
	@Test
	public void test4() {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println("----------------");
		ExampleBean bean = 
			ctx.getBean("exampleBean", ExampleBean.class);
		bean.execute();
		ctx.close();
	}
	
	/**
	 * setter注入
	 */
	@Test
	public void test5() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Computer computer = 
			ctx.getBean("computer", Computer.class);
		System.out.println(computer.getMainboard());
		System.out.println(computer.getHdd());
		System.out.println(computer.getRam());
	}
	
}

6 构造器注入

6.1 问题

本案例探讨如何使用构造器注入的方式为bean注入值。

6.2 方案

通过构造器注入的方式,为bean注入值

6.3 步骤

步骤一:创建bean

创建一个手机类MobilePhone,代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class MobilePhone implements Serializable {

	private String cpu;
	private String ram;

	public MobilePhone(String cpu, String ram) {
		this.cpu = cpu;
		this.ram = ram;
	}

	public String getCpu() {
		return cpu;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public String getRam() {
		return ram;
	}

	public void setRam(String ram) {
		this.ram = ram;
	}

}

步骤二:声明bean

在applicationContext.xml中声明这个bean,追加代码如下:

	<!--构造器注入 -->
	<bean id="phone" class="com.tarena.bean.MobilePhone">
		<constructor-arg index="0" value="ARM"/>
		<constructor-arg index="1" value="2G"/>
	</bean>

步骤三:写测试代码

在TestCase中增加测试方法test6,追加代码如下:

	/**
	 * 构造器注入
	 */
	@Test
	public void test6() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		MobilePhone phone = 
			ctx.getBean("phone", MobilePhone.class);
		System.out.println(phone.getCpu());
		System.out.println(phone.getRam());
	}

步骤四:执行测试

执行测试方法test6,效果如下图:

图-9

6.4 完整代码

MobilePhone完整代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class MobilePhone implements Serializable {

	private String cpu;
	private String ram;

	public MobilePhone(String cpu, String ram) {
		this.cpu = cpu;
		this.ram = ram;
	}

	public String getCpu() {
		return cpu;
	}

	public void setCpu(String cpu) {
		this.cpu = cpu;
	}

	public String getRam() {
		return ram;
	}

	public void setRam(String ram) {
		this.ram = ram;
	}

}

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!-- 通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!-- 通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>
	
	<!-- bean的生命周期 -->
	<!-- bean的延迟实例化 -->
	<bean id="exampleBean" class="com.tarena.bean.ExampleBean"
		init-method="init" destroy-method="destroy" lazy-init="true"/>
		
	<!-- setter注入 -->
	<bean id="computer" class="com.tarena.bean.Computer">
		<property name="mainboard" value="技嘉"/>
		<property name="hdd" value="希捷"/>
		<property name="ram" value="金士顿"/>
	</bean>
	
	<!-- 构造器注入 -->
	<bean id="phone" class="com.tarena.bean.MobilePhone">
		<constructor-arg index="0" value="ARM"/>
		<constructor-arg index="1" value="2G"/>
	</bean>
	
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}
	
		/**
		 * bean的作用域
		 */
		@Test
		public void test3() {
			String cfg = "applicationContext.xml";
			ApplicationContext ctx = 
				new ClassPathXmlApplicationContext(cfg);
			Calendar cal1 = (Calendar) ctx.getBean("obj5");
			Calendar cal2 = (Calendar) ctx.getBean("obj5");
			System.out.println(cal1==cal2);
		}
	
	/**
	 * 1.bean的生命周期;
	 * 2.bean的延迟实例化;
	 */
	@Test
	public void test4() {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println("----------------");
		ExampleBean bean = 
			ctx.getBean("exampleBean", ExampleBean.class);
		bean.execute();
		ctx.close();
	}
	
	/**
	 * setter注入
	 */
	@Test
	public void test5() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Computer computer = 
			ctx.getBean("computer", Computer.class);
		System.out.println(computer.getMainboard());
		System.out.println(computer.getHdd());
		System.out.println(computer.getRam());
	}
	
	/**
	 * 构造器注入
	 */
	@Test
	public void test6() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		MobilePhone phone = 
			ctx.getBean("phone", MobilePhone.class);
		System.out.println(phone.getCpu());
		System.out.println(phone.getRam());
	}
	
}

7 自动装配

7.1 问题

通过Spring自动装配机制,自动为一个bean装配其关联的bean。

7.2 方案

采用autowire=”byType”,即按照bean的类型进行自动装配。

7.3 步骤

步骤一:创建bean

创建一个学生类Student,该类中有计算机、手机属性,即学生关联了计算机和手机,代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class Student implements Serializable {

	private Computer computer;
	private MobilePhone mobilePhone;

	public Computer getComputer() {
		return computer;
	}

	public void setComputer(Computer computer) {
		this.computer = computer;
	}

	public MobilePhone getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(MobilePhone mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

}

步骤二:声明bean

在applicationContext.xml中声明这个bean,并声明按类型自动装配其关联属性,追加代码如下:

	<!--自动装配 -->
	<bean id="student" class="com.tarena.bean.Student" autowire="byType">
	</bean>

步骤三:写测试代码

在TestCase中增加测试方法test7,追加代码如下:

	/**
	 * 自动装配
	 */
	@Test
	public void test7() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Student student = 
			ctx.getBean("student", Student.class);
		System.out.println(student.getComputer());
		System.out.println(student.getMobilePhone());
	}

步骤四:执行测试

执行测试方法test7,效果如下图:

图-10

7.4 完整代码

Student完整代码如下:

package com.tarena.bean;

import java.io.Serializable;

public class Student implements Serializable {

	private Computer computer;
	private MobilePhone mobilePhone;

	public Computer getComputer() {
		return computer;
	}

	public void setComputer(Computer computer) {
		this.computer = computer;
	}

	public MobilePhone getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(MobilePhone mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

}

applicationContext.xml完整代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/www.springframework.org/schema/beans" 
	xmlns:xsi="/www.w3.org/2001/XMLSchema-instance"
	xmlns:context="/www.springframework.org/schema/context" 
	xmlns:jdbc="/www.springframework.org/schema/jdbc"  
	xmlns:jee="/www.springframework.org/schema/jee" 
	xmlns:tx="/www.springframework.org/schema/tx"
	xmlns:aop="/www.springframework.org/schema/aop" 
	xmlns:mvc="/www.springframework.org/schema/mvc"
	xmlns:util="/www.springframework.org/schema/util"
	xmlns:jpa="/www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		/www.springframework.org/schema/beans /www.springframework.org/schema/beans/spring-beans-3.2.xsd
		/www.springframework.org/schema/context /www.springframework.org/schema/context/spring-context-3.2.xsd
		/www.springframework.org/schema/jdbc /www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		/www.springframework.org/schema/jee /www.springframework.org/schema/jee/spring-jee-3.2.xsd
		/www.springframework.org/schema/tx /www.springframework.org/schema/tx/spring-tx-3.2.xsd
		/www.springframework.org/schema/data/jpa /www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		/www.springframework.org/schema/aop /www.springframework.org/schema/aop/spring-aop-3.2.xsd
		/www.springframework.org/schema/mvc /www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		/www.springframework.org/schema/util /www.springframework.org/schema/util/spring-util-3.2.xsd">
	
	<!-- 通过构造器实例化bean -->
	<bean id="obj1" class="java.util.GregorianCalendar"/>
	
	<!-- 通过静态工厂方法实例化bean -->
	<bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
	
	<!-- 通过实例工厂方法实例化bean -->
	<bean id="obj3" class="java.util.GregorianCalendar"/>
	<bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
	
	<!-- bean的作用域 -->
	<bean id="obj5" class="java.util.GregorianCalendar" scope="prototype"/>
	
	<!-- bean的生命周期 -->
	<!-- bean的延迟实例化 -->
	<bean id="exampleBean" class="com.tarena.bean.ExampleBean"
		init-method="init" destroy-method="destroy" lazy-init="true"/>
		
	<!-- setter注入 -->
	<bean id="computer" class="com.tarena.bean.Computer">
		<property name="mainboard" value="技嘉"/>
		<property name="hdd" value="希捷"/>
		<property name="ram" value="金士顿"/>
	</bean>
	
	<!-- 构造器注入 -->
	<bean id="phone" class="com.tarena.bean.MobilePhone">
		<constructor-arg index="0" value="ARM"/>
		<constructor-arg index="1" value="2G"/>
	</bean>
	
	<!-- 自动装配 -->
	<bean id="student" class="com.tarena.bean.Student" autowire="byType">
	</bean>
	
</beans>

TestCase完整代码如下:

package com.tarena.test;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.tarena.bean.Computer;
import com.tarena.bean.ExampleBean;
import com.tarena.bean.MobilePhone;
import com.tarena.bean.Student;

public class TestCase {
	
	/**
	 * 实例化Spring容器
	 */
	@Test
	public void test1() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println(ctx);
	}
	
	/**
	 * 3种实例化bean的方式
	 */
	@Test
	public void test2() {
		String cfg = "applicationContext.xml";
		ApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		
		Calendar cal1 = (Calendar) ctx.getBean("obj1");
		System.out.println(cal1);
		
		Calendar cal2 = ctx.getBean("obj2", Calendar.class);
		System.out.println(cal2);
		
		Date date = ctx.getBean("obj4", Date.class);
		System.out.println(date);
	}
	
		/**
		 * bean的作用域
		 */
		@Test
		public void test3() {
			String cfg = "applicationContext.xml";
			ApplicationContext ctx = 
				new ClassPathXmlApplicationContext(cfg);
			Calendar cal1 = (Calendar) ctx.getBean("obj5");
			Calendar cal2 = (Calendar) ctx.getBean("obj5");
			System.out.println(cal1==cal2);
		}
	
	/**
	 * 1.bean的生命周期;
	 * 2.bean的延迟实例化;
	 */
	@Test
	public void test4() {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		System.out.println("----------------");
		ExampleBean bean = 
			ctx.getBean("exampleBean", ExampleBean.class);
		bean.execute();
		ctx.close();
	}
	
	/**
	 * setter注入
	 */
	@Test
	public void test5() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Computer computer = 
			ctx.getBean("computer", Computer.class);
		System.out.println(computer.getMainboard());
		System.out.println(computer.getHdd());
		System.out.println(computer.getRam());
	}
	
	/**
	 * 构造器注入
	 */
	@Test
	public void test6() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		MobilePhone phone = 
			ctx.getBean("phone", MobilePhone.class);
		System.out.println(phone.getCpu());
		System.out.println(phone.getRam());
	}
	
	/**
	 * 自动装配
	 */
	@Test
	public void test7() throws SQLException {
		String cfg = "applicationContext.xml";
		AbstractApplicationContext ctx = 
			new ClassPathXmlApplicationContext(cfg);
		Student student = 
			ctx.getBean("student", Student.class);
		System.out.println(student.getComputer());
		System.out.println(student.getMobilePhone());
	}
	
}