一个完整的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. 约瑟夫(环)问题(Josephus problem)

    问题描述:皇帝决定找出全国中最幸运的一个人,于是从全国选拔出 n 个很幸运的人,让这 n 个人围着圆桌进餐,可是怎么选择出其中最幸运的一个人呢?皇帝决定:从其中一个人从 1 开始报数,按顺序数到第 k ...

  2. oracle中的查询语句(关于出库入库信息表,明细表,把捆包箱表,单位信息表的集中查询)

    --查出所有现金中心的单位IDwith AllUnitas(select t.ORGANIZATIONID orgid,t.parentidfrom CDMS_ORGANIZATION t where ...

  3. openldap加密传输sssd

    http://blog.father.gedow.net/2015/09/29/sssd-ldap-sudo/ yum -y install openldap-clients sssd authcon ...

  4. weed-fs 压力测试

    阅读<Weed-FS/杂草文件系统 小文件存储集群 安装 使用 测试>中提到weedfs的负载压力不是很好,在看过代码后进行了相应测试,未发现负载压力有何问题.   weedfs mast ...

  5. jquery的扩展之extend函数

    1.$.extend()使用 作用:扩展全局的函数 $.extend({ sayHellow:function(pram){ alert(pram+"hellow"); } }) ...

  6. free pascal 错误代码表

    free pascal 错误代码表 为了方便对照检查运行时错误代码,这里把所有的错误代码的含义整理出来.(最大序号为232,中间不一定连续.user.pdf P175) Run-time errors ...

  7. vnc

    Xvnc, Xvnc-core, vncagent, vncinitconfig, vnclicense, vnclicensehelper, vnclicensewiz, vncpasswd, vn ...

  8. mysql 索引分类

    在数据库表中,对字段建立索引可以大大提高查询速度.通过善用这些索引,可以令 MySQL的查询和运行更加高效.索引是快速搜索的关键.MySQL索引的建立对于MySQL的高效运行是很重要的.下面介绍几种常 ...

  9. Looping Techniques

    [Looping Techniques] 1.When looping through dictionaries, the key and corresponding value can be ret ...

  10. Bridge(桥接)-对象结构型模式

    1.意图 将抽象部分与它的实现部分分离,使它们都可以独立地变化. 2.动机 在抽象类与它的实现之间起到桥梁作用,使它们可以独立地变化. 3.适用性 不希望在抽象和它的实现部分之间有一个固定的绑定关系. ...