一个完整的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. python_day3

    一.set的基本操作 >>> s1=set("abc123abc") >>> s1 #把字符串转化为set,去重 set([']) >&g ...

  2. 【转】web测试总结

    1.字符型输入框: (1)字符型输入框:英文全角.英文半角.数字.空或者空格.特殊字符“~!@#¥%……&*?[]{}”特别要注意单引号和&符号.禁止直接输入特殊字符时,使用“粘贴.拷 ...

  3. Ping 命令的使用方法总结

    一.Ping 命令 “Ping”命令是我们在判断网络故障常用的命令,但您真正明白这个命令运行后会发生什么,以及出现的各种信息说明了什么吗?其实熟练的掌握 Ping 命令的各种技巧可以帮助你解决很多网络 ...

  4. JavaScript笔记基础篇(一)

    一. 常用正则表达式汇总以及部分问题解决方案 正则匹配: var str = "This is my test"; var test = new RegExp("test ...

  5. 28、跟我来一起精通echarts吧

    1.折线图 option = { title : { text: '未来一周气温变化', subtext: '纯属虚构' }, tooltip : { trigger: 'axis' }, legen ...

  6. CentOS7服务器相关配置

    ====================查看CentOS版本==================== cat /etc/redhat-release ====================YUM更新 ...

  7. nginx 启动,停止和重新加载配置

    要启动nginx的,运行可执行文件.一旦nginx的启动时,它可以通过与-s参数调用可执行来控制.使用以下语法 nginx -s signal 其中,信号可以是下列之一: stop - fast sh ...

  8. 往sql数据库表中添加字段

    通用式: alter table [表名] add [字段名] 字段属性 default 缺省值 default 是可选参数增加字段: alter table [表名] add 字段名 smallin ...

  9. UVa 458 - The Decoder

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=94&page=s ...

  10. css部分的复习

    常见的块元素有<h1><h6>.<p><div><ul><li><ol>等,其中<div>标记是最典型的 ...