Spring-管理Bean 使用BeanWrapper管理Bean
来源:互联网 发布:淘宝店铺分店 编辑:程序博客网 时间:2024/05/22 05:07
使用BeanWrapper管理Bean
1.修改Bean,增加一个无参数的构造函数
2.配置文件基本不需要改变
3.修改测试代码
1.修改Bean,增加一个无参数的构造函数
2.配置文件基本不需要改变
3.修改测试代码
Bean如下所示:
package com.gc.action;import java.util.Date;public class HelloWorld //implements InitializingBean,DisposableBean{private String msg=null;//该变量用来存储字符串private Date date=null;//该变量用来存储日期 public HelloWorld(){ }//设定变量msg的set方法public void setMsg(String msg) {this.msg=msg;}//获取变量msg的get方法public String getMsg() {return this.msg;}public Date getDate() {return this.date;}public void setDate(Date date) {this.date = date;}}
配置文件:
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.HelloWorld" init-method="afterPropertiesSet" destroy-method="cleanup"> <!--将其变量msg通过依赖注入--> </bean> </beans>
测试程序:
package com.gc.test;import java.util.Date;import org.springframework.beans.BeanWrapper;import org.springframework.beans.BeanWrapperImpl;import org.springframework.context.ApplicationContext;import org.springframework.context.support.AbstractApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.gc.action.HelloWorld;public class TestHelloWorld { public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { //通过Class.forName()方法获取类HelloWorld的一个实例 Object obj=Class.forName("com.gc.action.HelloWorld").newInstance(); //通过BeanWrapper来设定类HelloWorld的属性 BeanWrapper bw=new BeanWrapperImpl(obj); //根据类变量设定变量的值 bw.setPropertyValue("msg","HelloWorld"); bw.setPropertyValue("date",new Date()); System.out.println(bw.getPropertyValue("date")+" "+bw.getPropertyValue("msg")+"------"); }}
输出:
Tue Mar 20 19:29:35 CST 2012 HelloWorld------
- Spring-管理Bean 使用BeanWrapper管理Bean
- Spring中Bean的管理 – 使用BeanWrapper
- Spring-管理Bean 使用BeanFactory管理Bean
- 使用Spring管理Bean
- servlet 使用spring管理bean
- servlet 使用spring管理bean
- Spring Bean基本管理
- Spring 管理Bean
- spring管理bean原理
- spring管理bean生命周期
- spring的bean管理
- Spring Bean基本管理
- spring bean 管理
- Spring-管理bean
- Spring bean 生命周期管理
- spring管理bean生命周期
- spring Bean管理
- Spring的bean管理
- 解决FastCGI Error Error Number: -2147467259 (0x80004005). 和 Error Number: -1073741819 (0xc0000005).
- Server 2008 禁止IE安全配置
- 搭建Eclipse+MyEclipse开发环境
- Linux设备驱动工程师之路——platform按键驱动 来自于衡阳师范学院08电2
- HDU 2446 Shell Pyramid 二分查找
- Spring-管理Bean 使用BeanWrapper管理Bean
- (转自博客园)Java文件操作---文件读取和写入示例
- SVN學習(2)
- 实用的测试工具简介
- Silverlight之文件操作
- 谈谈是多做站好还是做一个站好
- 我所知道的正则表达式
- pls_integer类型
- 网页自动跳转