<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<bean id="userBean" class="com.szy.spring.implbean.UserBean" />
</beans>
Spring的配置文件中记录了类的包路径,因此我们首先是要读入配置文件。在配置文件中Bean有id和class两个属性,我们首先定义一个Bean类,包含这两个属性:
package com.szy.spring.implbean;
public class Bean
{
private String id;
private String className;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getClassName()
{
return className;
}
public void setClassName(String className)
{
this.className = className;
}
}
由于配置文件是xml文件,在这里使用Jdom包操作xml文件,读入配置文件中的Bean信息。
/**
* 读取xml配置文件
* @param fileName 配置文件名
*/
private void readXML(String fileName)
{
// 寻找配置文件
URL xmlPath = this.getClass().getClassLoader().getResource(fileName);
Document doc = null;
Element root = null;
try
{
SAXBuilder sb = new SAXBuilder(false);
doc = sb.build(new FileInputStream(new File(xmlPath.toURI())));
// 设置命名空间
Namespace xhtml = Namespace.getNamespace("xhtml",
"http://www.springframework.org/schema/beans");
root = doc.getRootElement(); // 获取根元素
List<Element> list = root.getChildren("bean", xhtml); //获取全部bean节点
for (Element element : list)// 遍历节点,取得每个节点的属性
{
String id = element.getAttributeValue("id");
String className = element.getAttributeValue("class");
Bean bean = new Bean();
bean.setId(id);
bean.setClassName(className);
beanList.add(bean);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
其中beanList是一个List对象,因为在配置文件中存在很多Bean。
得到了所有的Bean对象后,下面就实例化每个Bean对象,结果存放在Map对象中。
/**
* bean的实例化
*/
private void instanceBeans()
{
for (Bean bean : beanList)
{
try
{
if (bean.getClassName() != null && !"".equals(bean.getClassName().trim()))
beanObject.put(bean.getId(), Class.forName(bean.getClassName()).newInstance());
} catch (Exception e)
{
e.printStackTrace();
}
}
}
其中beanObject为Map对象。
最后再加入一个方法,方便外部能获取Map中的对象
/**
* 获取bean实例
* @param beanName 配置文件中bean的Id
* @return
*/
public Object getBean(String beanName)
{
return this.beanObject.get(beanName);
}
完整的MyClassPathXMLApplicationContext见附件中的代码。
下面测试MyClassPathXMLApplicationContext类。
@Test
public void testMethod() throws Exception
{
//读取配置文件
MyClassPathXMLApplicationContext ctx=new MyClassPathXMLApplicationContext("applicationContext.xml");
//获取UserBean的实例
PersonBean bean=(PersonBean)ctx.getBean("userBean");
//调用方法
bean.show();
}
输出结果
Hello Kuka
成功。
上面仅是简单的演示了Spring管理Bean的原理,但是在实际操作中还需要考虑很对其它因素。
分享到:
相关推荐
Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-最新Spring2.5.6学习笔记-最新最新
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
黑马程序员-SpringCloud-学习笔记01-认识微服务
黑马程序员-SpringCloud-学习笔记-03-Eureka注册中心
开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-webmvc-4.3.6.RELEASE开发工具 spring-web...
Spring5 框架 ---- Spring5的新功能 ---- 代码 Spring5 框架 ---- Spring5的新功能 ---- 代码 Spring5 框架 ---- Spring5的新功能 ---- 代码 Spring5 框架 ---- Spring5的新功能 ---- 代码 Spring5 框架 ---- Spring...
Spring.NET学习笔记 - 刘冬.NET - 博客园
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 框架 ---- AOP ---- 代码 Spring5 ...
Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- ...
spring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jar
NULL 博文链接:https://springsfeng.iteye.com/blog/1149243
spring源码编译之后,各个项目可能缺失jar包 spring-cglib-repack-3.2.4.jar和spring-objenesis-repack-2.5.1.jar
spring源码缺失jar。spring-cglib-repack-3.2.5.jar,spring-objenesis-repack-2.6.jar