Spring创建bean

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

1 实例化Spring容器

1.1 问题

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

1.2 方案

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


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

1.3 步骤

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

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

图-1

步骤二:导入Spring配置文件

导入applicationContext.xml,代码如下:


     
     
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6.     xmlns:jee="http://www.springframework.org/schema/jee"
  7.     xmlns:tx="http://www.springframework.org/schema/tx"
  8.     xmlns:aop="http://www.springframework.org/schema/aop"
  9.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  10.     xmlns:util="http://www.springframework.org/schema/util"
  11.     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  12.     xsi:schemaLocation="
  13.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  14.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  15.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  16.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  17.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  18.         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  19.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  20.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  21.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  22.     
  23. </beans>

步骤三:写测试代码

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


     
     
  1. package com.tarena.test;
  2. import java.sql.SQLException;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import org.junit.Test;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.AbstractApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import com.tarena.bean.Computer;
  10. import com.tarena.bean.ExampleBean;
  11. import com.tarena.bean.MobilePhone;
  12. import com.tarena.bean.Student;
  13. public class TestCase {
  14.     
  15.     /**
  16.      * 实例化Spring容器
  17.      */
  18.     @Test
  19.     public void test1() {
  20.         String cfg = "applicationContext.xml";
  21.         ApplicationContext ctx =
  22.             new ClassPathXmlApplicationContext(cfg);
  23.         System.out.println(ctx);
  24.     }
  25.     
  26. }

步骤四:执行测试

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

图-2

1.4 完整代码

applicationContext.xml完整代码如下:

TestCase完整代码如下:

2 使用Spring容器创建bean

2.1 问题

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

2.2 方案

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

2.3 步骤

步骤一:声明bean

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


     
     
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:context="http://www.springframework.org/schema/context"
  5.     xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  6.     xmlns:jee="http://www.springframework.org/schema/jee"
  7.     xmlns:tx="http://www.springframework.org/schema/tx"
  8.     xmlns:aop="http://www.springframework.org/schema/aop"
  9.     xmlns:mvc="http://www.springframework.org/schema/mvc"
  10.     xmlns:util="http://www.springframework.org/schema/util"
  11.     xmlns:jpa="http://www.springframework.org/schema/data/jpa"
  12.     xsi:schemaLocation="
  13.         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  14.         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
  15.         http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
  16.         http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
  17.         http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  18.         http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
  19.         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  20.         http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  21.         http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
  22.     
  23.     <!--通过构造器实例化bean -->
  24.     <bean id="obj1" class="java.util.GregorianCalendar"/>
  25.     
  26.     <!--通过静态工厂方法实例化bean -->
  27.     <bean id="obj2" class="java.util.Calendar" factory-method="getInstance"/>
  28.     
  29.     <!--通过实例工厂方法实例化bean -->
  30.     <bean id="obj3" class="java.util.GregorianCalendar"/>
  31.     <bean id="obj4" factory-bean="obj3" factory-method="getTime"/>
  32.     
  33. </beans>

步骤二:写测试代码

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


     
     
  1. package com.tarena.test;
  2. import java.sql.SQLException;
  3. import java.util.Calendar;
  4. import java.util.Date;
  5. import org.junit.Test;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.support.AbstractApplicationContext;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import com.tarena.bean.Computer;
  10. import com.tarena.bean.ExampleBean;
  11. import com.tarena.bean.MobilePhone;
  12. import com.tarena.bean.Student;
  13. public class TestCase {
  14.     //其他方法略
  15.     /**
  16.      * 3种实例化bean的方式
  17.      */
  18.     @Test
  19.     public void test2() {
  20.         String cfg = "applicationContext.xml";
  21.         ApplicationContext ctx =
  22.             new ClassPathXmlApplicationContext(cfg);
  23.         
  24.         Calendar cal1 = (Calendar) ctx.getBean("obj1");
  25.         System.out.println(cal1);
  26.         
  27.         Calendar cal2 = ctx.getBean("obj2", Calendar.class);
  28.         System.out.println(cal2);
  29.         
  30.         Date date = ctx.getBean("obj4", Date.class);
  31.         System.out.println(date);
  32.     }
  33. }

步骤三:执行测试

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

图-3

2.4 完整代码

applicationContext.xml完整代码如下:

TestCase完整代码如下:

3 bean的作用域

3.1 问题

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

3.2 方案

3.3 步骤

步骤一:声明bean

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


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

步骤二:写测试代码

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


     
     
  1.     /**
  2.      * bean的作用域
  3.      */
  4.     @Test
  5.     public void test3() {
  6.         String cfg = "applicationContext.xml";
  7.         ApplicationContext ctx =
  8.             new ClassPathXmlApplicationContext(cfg);
  9.         Calendar cal1 = (Calendar) ctx.getBean("obj5");
  10.         Calendar cal2 = (Calendar) ctx.getBean("obj5");
  11.         System.out.println(cal1==cal2);
  12.     }

步骤三:执行测试

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

图-4

步骤四:修改bean声明

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


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

步骤五:执行测试

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

图-5

3.4 完整代码

applicationContext.xml完整代码如下:

TestCase完整代码如下:

4 bean的生命周期

4.1 问题

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

4.2 方案

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

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

4.3 步骤

步骤一:创建bean

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


     
     
  1. package com.tarena.bean;
  2. import java.io.Serializable;
  3. public class ExampleBean implements Serializable {
  4.     
  5.     public ExampleBean() {
  6.         System.out.println("实例化ExampleBean:" + this);
  7.     }
  8.     
  9.     public void init() {
  10.         System.out.println("初始化ExampleBean");
  11.     }
  12.     
  13.     public void destroy() {
  14.         System.out.println("销毁ExampleBean");
  15.     }
  16.     
  17.     public void execute() {
  18.         System.out.println("执行execute方法");
  19.     }
  20.     
  21. }

步骤二:声明bean

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


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

步骤三:写测试代码

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


     
     
  1.     /**
  2.      * 1.bean的生命周期;
  3.      * 2.bean的延迟实例化;
  4.      */
  5.     @Test
  6.     public void test4() {
  7.         String cfg = "applicationContext.xml";
  8.         AbstractApplicationContext ctx =
  9.             new ClassPathXmlApplicationContext(cfg);
  10.         System.out.println("----------------");
  11.         ExampleBean bean =
  12.             ctx.getBean("exampleBean", ExampleBean.class);
  13.         bean.execute();
  14.         ctx.close();
  15.     }

步骤四:执行测试

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

图-6

步骤五:修改声明的bean

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


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

步骤六:执行测试

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

图-7

4.4 完整代码

applicationContext.xml完整代码如下:

TestCase完整代码如下:

5 Setter注入

5.1 问题

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

5.2 方案

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

5.3 步骤

步骤一:创建bean

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


     
     
  1. package com.tarena.bean;
  2. import java.io.Serializable;
  3. public class Computer implements Serializable {
  4.     private String mainboard; // 主板
  5.     private String hdd; // 硬盘
  6.     private String ram; // 内存
  7.     public String getMainboard() {
  8.         return mainboard;
  9.     }
  10.     public void setMainboard(String mainboard) {
  11.         this.mainboard = mainboard;
  12.     }
  13.     public String getHdd() {
  14.         return hdd;
  15.     }
  16.     public void setHdd(String hdd) {
  17.         this.hdd = hdd;
  18.     }
  19.     public String getRam() {
  20.         return ram;
  21.     }
  22.     public void setRam(String ram) {
  23.         this.ram = ram;
  24.     }
  25. }

步骤二:声明bean

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


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

步骤三:写测试代码

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


     
     
  1.     /**
  2.      * setter注入
  3.      */
  4.     @Test
  5.     public void test5() throws SQLException {
  6.         String cfg = "applicationContext.xml";
  7.         AbstractApplicationContext ctx =
  8.             new ClassPathXmlApplicationContext(cfg);
  9.         Computer computer =
  10.             ctx.getBean("computer", Computer.class);
  11.         System.out.println(computer.getMainboard());
  12.         System.out.println(computer.getHdd());
  13.         System.out.println(computer.getRam());
  14.     }

步骤四:执行测试

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

图-8

5.4 完整代码

Computer完整代码如下:

applicationContext.xml完整代码如下:

TestCase完整代码如下:

6 构造器注入

6.1 问题

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

6.2 方案

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

6.3 步骤

步骤一:创建bean

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


     
     
  1. package com.tarena.bean;
  2. import java.io.Serializable;
  3. public class MobilePhone implements Serializable {
  4.     private String cpu;
  5.     private String ram;
  6.     public MobilePhone(String cpu, String ram) {
  7.         this.cpu = cpu;
  8.         this.ram = ram;
  9.     }
  10.     public String getCpu() {
  11.         return cpu;
  12.     }
  13.     public void setCpu(String cpu) {
  14.         this.cpu = cpu;
  15.     }
  16.     public String getRam() {
  17.         return ram;
  18.     }
  19.     public void setRam(String ram) {
  20.         this.ram = ram;
  21.     }
  22. }

步骤二:声明bean

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


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

步骤三:写测试代码

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


     
     
  1.     /**
  2.      * 构造器注入
  3.      */
  4.     @Test
  5.     public void test6() throws SQLException {
  6.         String cfg = "applicationContext.xml";
  7.         AbstractApplicationContext ctx =
  8.             new ClassPathXmlApplicationContext(cfg);
  9.         MobilePhone phone =
  10.             ctx.getBean("phone", MobilePhone.class);
  11.         System.out.println(phone.getCpu());
  12.         System.out.println(phone.getRam());
  13.     }

步骤四:执行测试

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

图-9

6.4 完整代码

MobilePhone完整代码如下:

applicationContext.xml完整代码如下:

TestCase完整代码如下:

7 自动装配

7.1 问题

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

7.2 方案

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

7.3 步骤

步骤一:创建bean

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


     
     
  1. package com.tarena.bean;
  2. import java.io.Serializable;
  3. public class Student implements Serializable {
  4.     private Computer computer;
  5.     private MobilePhone mobilePhone;
  6.     public Computer getComputer() {
  7.         return computer;
  8.     }
  9.     public void setComputer(Computer computer) {
  10.         this.computer = computer;
  11.     }
  12.     public MobilePhone getMobilePhone() {
  13.         return mobilePhone;
  14.     }
  15.     public void setMobilePhone(MobilePhone mobilePhone) {
  16.         this.mobilePhone = mobilePhone;
  17.     }
  18. }

步骤二:声明bean

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


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

步骤三:写测试代码

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


     
     
  1.     /**
  2.      * 自动装配
  3.      */
  4.     @Test
  5.     public void test7() throws SQLException {
  6.         String cfg = "applicationContext.xml";
  7.         AbstractApplicationContext ctx =
  8.             new ClassPathXmlApplicationContext(cfg);
  9.         Student student =
  10.             ctx.getBean("student", Student.class);
  11.         System.out.println(student.getComputer());
  12.         System.out.println(student.getMobilePhone());
  13.     }

步骤四:执行测试

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

图-10

7.4 完整代码

Student完整代码如下:

applicationContext.xml完整代码如下:

TestCase完整代码如下:

猜你喜欢

转载自blog.csdn.net/magicianjun/article/details/78770879