Spring管理Bean的三种方式

随时随地阅读更多技术实战干货,获取项目源码、学习资料,请关注源代码社区公众号(ydmsq666)

主要有三种方式:BeanWrapper、BeanFactory和使用ApplicationContext

1、BeanWrapper

package com.example.demo.test;

import java.util.Date;

public class HelloWorld {

	private String msg;

	private Date date;

	public HelloWorld() {
	}

	public Date getDate() {
		return date;
	}

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

	public String getMsg() {
		return msg;
	}

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

}
package com.example.demo.test;

import java.util.Date;

import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

public class SpringTest {

	public static void main(String[] args)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		Object obj = Class.forName("com.example.demo.test.HelloWorld").newInstance();
		BeanWrapper bw = new BeanWrapperImpl(obj);
		bw.setPropertyValue("msg", "hello world!");
		bw.setPropertyValue("date", new Date());
		System.out.println(bw.getPropertyValue("date") + " " + bw.getPropertyValue("msg"));
	}
}

2、BeanFactory

这个接口有多个实现,最常用的实现是XmlBeanFactory,示例如下

<?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="HelloWorld" class="com.example.demo.test.HelloWorld">
		<property name="msg">
			<value>HelloWorld</value>
		</property>
		<property name="date">
			<ref bean="date"/>
		</property>
	</bean>
	<bean id="date" class="java.util.Date"></bean>
	
</beans>
package com.example.demo.test;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class SpringTest {

	public static void main(String[] args) {
		ClassPathResource res = new ClassPathResource("config/config.xml");
		XmlBeanFactory factory = new XmlBeanFactory(res);
		HelloWorld h = (HelloWorld) factory.getBean("HelloWorld");
		System.out.println(h.getDate() + " " + h.getMsg());
	}
}

3、ApplicationContext

ApplicationContext建立在BeanFactory之上,并增加了其它功能,例如对于国际化的支持、获取资源、事件传递等。示例如下,HelloWorld.java和配置都不用改,测试代码如下

package com.example.demo.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class SpringTest {

	public static void main(String[] args) {
		ApplicationContext ac = new FileSystemXmlApplicationContext("src/config/config.xml");
		HelloWorld h = (HelloWorld) ac.getBean("HelloWorld");
		System.out.println(h.getDate() + " " + h.getMsg());
	}
}

最常用的是ApplicationContext

  • 2
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

u010142437

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值