Spring之Bean的创建和装配

来源:互联网 发布:linux mount nfs挂载 编辑:程序博客网 时间:2024/06/05 07:20

前言

最近在重温Spring,因此决定写一系列的关于Spring学习的博客,一是为了加深自己的理解,二来希望能帮助更多的朋友掌握Spring。我用到的Spring版本是3.1.1。

Spring创建Bean

Bean应该说是Spring中最基本的配置单位了,任何的操作都离不开Bean(好像是废话哈)。记得刚开始接触JavaEE的时候,看网上的教程通篇都是Bean啊Bean的,当时真是不知道Bean是何方神圣,英语不好还特意查了字典,发现Bean是豆子。。。这样就更难理解了。在我的理解中Bean就是一个类,在Spring中通过元素声明一个Bean,当合适的时候Spring容器来将这个Bean实例化,然后管理它的生命周期。

声明Bean

声明Bean很很简单,首先在命名空间中添加Bean的引用,一般类似下面这样:

<?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"></beans>

之后就可以在配置文件中自由的定义Bean了(友情提示:使用IntelliJ IDEA添加相应的命名空间后可以自动带出能出现的元素)。
为了下面讲解方便,我先写两个类–经典的员工Employee和部门Department:
Employee.java

public class Employee {    private String name;    private Date birthDate;    private int level;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Date getBirthDate() {        return birthDate;    }    public void setBirthDate(Date birthDate) {        this.birthDate = birthDate;    }    public int getLevel() {        return level;    }    public void setLevel(int level) {        this.level = level;    }}

Department .java

public class Department {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }}

上面已经添加过Bean的命名空间了,因此我们就可以定义一个Employee和Department了:

<bean id="employee" class="com.stream.spring.bean.entity.Employee" /><bean id="dept" class="com.stream.spring.bean.entity.Department" />

bean元素有几个属性值得注意:

  1. id或者name,指定Bean的名字。需要注意的是id是Bean的唯一标志,而name可以有多个,并且在定义name的时候可以用任意的空格,逗号或者分号做分割。
  2. scope指定Bean的作用域。scope有五个可选值:singleton,prototype,request,session,global session。singleton是Spring默认的值,使用这个作用域的Bean,在容器中只有一个实例,从容器启动,到第一次被请求而实例化开始,只要容器不销毁或退出,该实例就会一直存活。prototype是多实例,对于每个请求都会返回一个实例。剩下的三个只适用于Web程序。
  3. init-method和destroy-method分别指定初始化方法和销毁方法。全局的初始化方法和销毁方法在beans元素上定义default-init-method和default-destroy-method。
  4. factory-method通过工厂方法实例化Bean。
  5. autowire属性指定Bean中属性自动装配时的查找方式。

注入Bean的属性

Spring注入Bean属性有两种方式:构造器注入和setter方法注入。

构造器注入

构造器注入使用constructor-arg标签

<bean id="employee" class="com.stream.spring.bean.entity.Employee">    <constructor-arg name="name" value="stream" index="0"/></bean>

name是构造器方法的参数,index是参数的位置,当注入的是Bean时可以使用ref属性。当使用构造器方法注入的时候,constructor-arg列表个数要个构造方法的参数个数一致,如下:

    public Employee(String name,int level){        this.name = name;        this.level = level;        System.out.println("name,level");    }    /*public Employee(String name){        this.name = name;        System.out.println("name");    }*/    public Employee(int level){        this.level = level;    }

在Employee的构造方法中没有对应的只有一个参数的构造方法,这样在实例化Bean的时候会报错:

Unsatisfied dependency expressed through constructor argument with index 1 of type [int]: Ambiguous constructor argument types - did you specify the correct bean references as constructor arguments?

另外需要注意的是如果没有无参构造方法,那么在装配Bean属性的时候必须要有构造器注入,否则会报错。

setter方法注入

setter方法注入使用property标签:

<bean id="employee" class="com.stream.spring.bean.entity.Employee">    <property name="name" value="Stream" /></bean>

和构造器注入类似,name是Bean的属性名,当需要注入Bean时可以用ref属性。
setter方法只要保证在java类中有对应的setter方法即可,对顺序和个数没有限制。

装配集合

当装配的属性是基本数据类型时可以直接使用value属性,当引用其它Bean时使用ref属性,当属性是集合时Spring也提供了对应的方法。
Spring提供了4种类型的集合配置元素:

元素名称 用途 声明方式 list 装配list类型的值,允许重复 list set 装配set类型的值,不允许重复 set map 装配map类型的值,key和value可以是任意类型 map props 装配properties类型的值,key和value必须都是String类型 props

装配空值

可以使用标签来显式的指定空值。

1 0
原创粉丝点击