BeanWapper BeanWapperImpl

在Spring中,Bean的管理主要有3种方法,分别是使用BeanWrapper管理Bean、使用BeanFactory管理Bean和使用ApplicationContext管理Bean。 
1、使用BeanWrapper管理Bean 
在org.springframework.beans包中,还有两个非常重要的类:BeanWrapper接口及它的实现BeanWrapperImpl。BeanWrapper封装了一个Bean的行为,提供了设置和获得属性值的功能。通过BeanWrapper可以获得Bean的属性和描述、查询只读或者可写属性。 
下面通过BeanWrapper来实现输出HelloWorld,其实现思路是: 

----HelloWord.java---- 
import java.util.Date; 
public class HelloWorld{ 
private String msg = null; 
private Date date = null; 
public helloWorld(){ 

public void setMsg(String msg){ 
this.msg = msg; 


public String getMsg(){ 
return this.msg; 

public void setDate(Date date){ 
this.date = date; 

public Date getDate(){ 
return this.date; 



----测试程序TestHelloWorl.java----- 
import org.springframework.beans.BeanWrapper; 
import org.springframework.beans.BeanWrapperImpl; 
import org.springframework.context.ApplicationContext; 

public class TestHelloWorld{ 
public static void main(String[] args)throws InstantiationExceptoin,IllegalAccessException,ClassNotFoundException{ 
  Object obj = Class.forName("HelloWorld").newInstance(); 
  BeanWrapper bw = new BeanWrapperImpl(obj); 
  bw.setPropertyValue('msg',"HellowWorld"); 
  bw.setPropertyValue("date",new Date()); 
  System.out.println(bw.getPropertyValue("date")+""+bw.getPropertyValue("msg")); 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值