Spring-管理Bean 使用BeanWrapper管理Bean

简介:
使用BeanWrapper管理Bean
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------


目录
相关文章
|
1月前
|
Java 应用服务中间件 Spring
Spring系列文章:Bean的作⽤域
Spring系列文章:Bean的作⽤域
|
1月前
|
Java Spring 容器
Spring系列文章:Bean的获取⽅式
Spring系列文章:Bean的获取⽅式
|
22小时前
|
开发框架 监控 Java
深入探索Spring Boot的监控、管理和测试功能及实战应用
【5月更文挑战第14天】Spring Boot是一个快速开发框架,提供了一系列的功能模块,包括监控、管理和测试等。本文将深入探讨Spring Boot中监控、管理和测试功能的原理与应用,并提供实际应用场景的示例。
14 2
|
14天前
|
消息中间件 安全 Java
在Spring Bean中,如何通过Java配置类定义Bean?
【4月更文挑战第30天】在Spring Bean中,如何通过Java配置类定义Bean?
21 1
|
17天前
|
前端开发 Java 数据格式
【Spring系列笔记】定义Bean的方式
在Spring Boot应用程序中,定义Bean是非常常见的操作,它是构建应用程序的基础。Spring Boot提供了多种方式来定义Bean,每种方式都有其适用的场景和优势。
32 2
|
19天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
25天前
|
安全 Java 数据库
第4章 Spring Security 的授权与角色管理(2024 最新版)(下)
第4章 Spring Security 的授权与角色管理(2024 最新版)
11 0
|
25天前
|
安全 Java 数据库
第4章 Spring Security 的授权与角色管理(2024 最新版)(上)
第4章 Spring Security 的授权与角色管理(2024 最新版)
35 0
|
27天前
|
Java 数据库连接 开发者
浅谈Spring的Bean生命周期
浅谈Spring的Bean生命周期
22 1
|
1月前
|
XML Java 数据格式
Spring IOC—基于XML配置和管理Bean 万字详解(通俗易懂)
Spring 第二节 IOC—基于XML配置和管理Bean 万字详解!。
94 5