一个完整的Bean的配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD//BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--定义一个bean,id是这个bean的唯一标识,class指出这个bean的来源,singleton指定这个bean是否是单例模式,
depends-on指定这个bean初始化前,强制初始化date-->
<bean id="HelloWorld" class="com.gc.action.HelloWorld" singleton="true" depends-on="date">
<!--配置bean的属性-->
<property name="msg">
<value>HelloWorld!</value>
</property>
<!--指定bean的一个依赖-->
<property name="date">
<ref bean="date"/>
</property>
</bean> <bean id="date" class="java.util.Date"/>
</beans>

Spring中,Bean的使用有3种方式:

第一种:使用BeanWrapper

HelloWorld helloWorld = new HelloWorld();
BeanWrapper bw = new BeanWrapperlmpl(helloWorld);
bw.setPropertyValue("msg", "HelloWorld");
System.out.println(bw.getPropertyValue("msg"));

第二种,使用BeanFactory

InputStream is = new FileInputStream("config.xml");
XmlBeanFactory factory = new XmlBeanFactory(is);
HelloWorld helloWorld = (HelloWorld)factory.getBean("HelloWorld");
System.out.println(helloWorld.getMsg());

第三种,使用ApplicationContext

ApplicationContext actx = new FileSystemXmlApplicationContext("config.xml");
HelloWorld helloWorld = (HelloWorld)actx.getBean("HelloWorld");
System.out.println(helloWorld.getMsg());

spring:bean的定义的更多相关文章

  1. Spring Bean的定义及作用域

    目录: 了解Spring的基本概念 Spring简单的示例 Bean的定义 简单地说Bean是被Spring容器管理的Java对象,Spring容器会自动完成对Bean的实例化. 那么什么是容器呢?如 ...

  2. Spring Bean的生命周期、后置处理器、定义继承

    目录: 了解Spring的基本概念 Spring简单的示例 Spring Bean的定义及作用域 1.Bean的生命周期 Bean的生命周期可以简单的理解为:Bean的定义——Bean的初始化——Be ...

  3. Spring Bean 定义

    Bean 定义 被称作 bean 的对象是构成应用程序的支柱.也是由 Spring IoC 容器管理的. bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象. 这些 bea ...

  4. Spring学习二:Spring Bean 定义

    Bean 定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象.这些 bean 是 ...

  5. 一个 Spring Bean 定义 包含什么?

    一个Spring Bean 的定义包含容器必知的所有配置元数据,包括如何创建一个bean,它的生命周期详情及它的依赖.

  6. 【Spring IoC】Spring Bean(三)

    一.Spring Bean的定义 被称作 bean 的对象是构成应用程序的支柱也是由 Spring IoC 容器管理的.bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象. ...

  7. [spring] -- bean作用域跟生命周期篇

    作用域 singleton : 唯一 bean 实例,Spring 中的 bean 默认都是单例的. prototype : 每次请求都会创建一个新的 bean 实例. request : 每一次HT ...

  8. Spring 学习笔记(2) Spring Bean

    一.IoC 容器 IoC 容器是 Spring 的核心,Spring 通过 IoC 容器来管理对象的实例化和初始化(这些对象就是 Spring Bean),以及对象从创建到销毁的整个生命周期.也就是管 ...

  9. 大厂高频面试题Spring Bean生命周期最详解

    Spring作为当前Java最流行.最强大的轻量级框架.Spring Bean的生命周期也是面试高频题,了解Spring Bean周期也能更好地帮助我们解决日常开发中的问题.程序员应该都知道Sprin ...

  10. Spring-Context之四:Spring容器及bean的定义

    Spring框架的核心功能之一就是控制反转(Inversion of Control, IoC),也叫做依赖注入(dependency injection, DI).关于依赖注入的具体内容可以参见Ma ...

随机推荐

  1. Home not found. Define system property "openfireHome" or create and add the openfire_init.xml file to the classpath

    启动openfire后出现这个错误,貌似什么配置没对吧.网上搜索了下,找到解决办法, $ vi /etc/profile在里面加入:export openfireHome=/opt/openfire ...

  2. 百度地图API示例之根据城市名设置地图中心点

    代码: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" con ...

  3. 在配置dubbo框架的时候出现dubbo:application标签无法识别问题。

    这原因是因为目前 http://code.alibabatech.com/schema/dubbo/dubbo.xsd标签库打不开.所以我们需要在dubbo的jar包里面引入一个dubbo.xsd 解 ...

  4. MySQL大数据分页的优化思路和索引延迟关联

    之前上次在部门的分享会上,听了关于MySQL大数据的分页,即怎样使用limit offset,N来进行大数据的分页,现在做一个记录: 首先我们知道,limit offset,N的时候,MySQL的查询 ...

  5. C#DataGridView中的常用技巧

    0(最基本的技巧). 获取某列中的某行(某单元格)中的内容  this.currentposition = this.dataGridView1.BindingContext  [this.dataG ...

  6. windows下apache+php+mysql 环境配置方法

    一 准备 1 下载apache http://httpd.apache.org/download.cgi#apache24 httpd-2.2.22-win32-x86-openssl-0.9.8t. ...

  7. 使用Trello实现敏捷项目管理

    使用Trello实现敏捷项目管理 作者                     侯伯薇        发布于    五月 24, 2012     |     1         讨论 新浪微博腾讯微 ...

  8. Java设计模式——装饰者模式

    JAVA 设计模式 装饰者模式 用途 装饰者模式 (Decorator) 动态地给一个对象添加一些额外的职责.就增加功能来说,Decorator 模式相比生成子类更为灵活. 装饰者模式是一种结构式模式 ...

  9. MongoDB中的连接池

    参见 http://www.cnblogs.com/huangfox/archive/2012/04/01/2428947.html

  10. POJ 2155 Matrix (二维线段树)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17226   Accepted: 6461 Descripti ...