---
title: "Sping中Bean的简单认识（一）"
author: "GoodWell"
date: "2011-04-18T19:52:00Z"
tags: []
slug: "sping-bean"
draft: false
lastmod: "2011-04-18T19:52:00Z"
id: 1007381
url: "https://goodwell.iteye.com/blog/1007381"
---

Bean的简单认识：

在spring配置文件中，使用bean元素来装配一个Bean，它的常用属性有一下九种

1、 id：指它该Bean的唯一标识。

2、 class：指定该Bean的全限定名。

3、 name：为该Bean指定一到多个别名。多个别名可以用空格、”，”或”；”分隔。

4、 autowire：指定该Bean的属性的装配方式（后面会详细介绍）

5、 scope：指定该Bean的存在范围（同上）

6、 init-method：指定该Bean的初始化方法

7、 destroy-method：指定该Bean的销毁方法

8、 abstract：指定该Bean是否为抽象的，如果是抽象的，则spring不在为其创建实例。

9、 parent：指定该Bean的父类标识或别名。

装配Bean的各种类型的属性值

1、 简单类型属性值的装配

Bean中通常有一些简单的类型属性，比如String和int这些简单的类型都可以通过bean元素的子元素property来设置

比如：

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;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-2.5.xsd"*&gt;

&lt;bean id=*"hello"* class=*"cn.csdn.service.GreetingServiceImpl"* init-method=*"init"*&gt;

&lt;property name=*"say"* value=*"hello"*/&gt;

&lt;/bean&gt;

&lt;bean id=*"test"* class=*"cn.csdn.service.Test"*&gt;&lt;/bean&gt;

&lt;/beans&gt;

2、 引用其他Bean的装配

如果Bean的某个属性是复杂的类型，既它将引用自其他的Bean。那么在Spring配置文件中可以使用property元素的ref属性来引用

比如：

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;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-2.0.xsd"*&gt;

&lt;!-- 创建一个DAO的bean id是唯一的标识 class指明类路径 property是bean的属性 --&gt;

&lt;bean id=*"greetingDAOImpl"* class=*"cn.csdn.dao.GreetingDAOImpl"*&gt;

&lt;property name=*"say"*&gt;

&lt;value&gt;hello&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;!-- 创建一个Service的bean ref引用对象--&gt;

&lt;bean id=*"greetingServiceImpl"* class=*"cn.csdn.service.GreetingServiceImpl"*&gt;

&lt;property name=*"greetingDAOImpl"* ref=*"greetingDAOImpl"*&gt;&lt;/property&gt;

&lt;/bean&gt;

&lt;/beans&gt;

还有一种不常用的方法是在property元素中嵌入一个bean元素来指定所引用的Bean

比如：

&lt;?xml version=*"1.0"* encoding=*"UTF-8"*?&gt;

&lt;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-2.0.xsd"*&gt;

&lt;!-- 创建一个DAO的bean id是唯一的标识 class指明类路径 property是bean的属性 --&gt;

&lt;bean id=*"greetingDAOImpl"* class=*"cn.csdn.dao.GreetingDAOImpl"*&gt;

&lt;property name=*"say"*&gt;

&lt;value&gt;hello&lt;/value&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;!-- 创建一个Service的bean ref引用对象--&gt;

&lt;bean id=*"greetingServiceImpl"* class=*"cn.csdn.service.GreetingServiceImpl"*&gt;

&lt;property name=*"greetingDAOImpl"*&gt;

&lt;bean class=”*cn.csdn.dao.GreetingDAOImpl*”&gt;&lt;/bean&gt;

&lt;/property&gt;

&lt;/bean&gt;

&lt;/beans&gt;

这种装配方式的确定是用户无法在其他地方重用这个内嵌的Bean

未完待续……………………………………（请继续关注GoodWell的东东）