剖析Spring管理Bean的原理(学习笔记)
2009-02-07 19:39
344 查看
/**
* 模拟spring的解析bean.xml的类
* @author river
*
*/
public class RiverClassPathXMLApplicationContext
{
private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
private Map<String, Object> sigletons = new HashMap<String, Object>();
public RiverClassPathXMLApplicationContext(String filename)
{
this.readXML(filename);
this.instanceBeans();
}
/**
* 完成实例化
*/
private void instanceBeans()
{
for (BeanDefinition beanDefinition : beanDefinitions)
{
if (beanDefinition.getClassName() != null
&& !"".equals(beanDefinition.getClassName().trim()))
{
try
{
sigletons.put(beanDefinition.getId(), Class.forName(
beanDefinition.getClassName()).newInstance());
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
public Object getBean(String beanName)
{
return this.sigletons.get(beanName);
}
/**
* 读取配置文件,filename为bean的配置文件
*
* @param filename
*/
private void readXML(String filename)
{
SAXReader saxReader = new SAXReader();
Document document = null;
try
{
URL xmlpath = this.getClass().getClassLoader()
.getResource(filename);
document = saxReader.read(xmlpath);
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");// 假如命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");// 创建beans/bean
xsub.setNamespaceURIs(nsMap);// 设置命名空间
List<Element> beans = xsub.selectNodes(document);// 获取文档下的bean
for (Element element : beans)
{
String id = element.attributeValue("id");// 获取id属性
String clazz = element.attributeValue("class");// 获取class属性
BeanDefinition beanDefine = new BeanDefinition(id, clazz);
beanDefinitions.add(beanDefine);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
PersonService 类的一个实现类:
public class PersonServiceBean implements PersonService {
/* (non-Javadoc)
* @see com.river.spring.PwesionService#save()
*/
public void save()
{
System.out.println("save ()");
}
}
bean.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personService" class="com.river.service.impl.PersonServiceBean"/>
</beans>
测试:
/**
* personService为bean中的id
*/
public void instanceSpring()
{
RiverClassPathXMLApplicationContext ctx = new RiverClassPathXMLApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();
}
结果:save(); 阅读更多
* 模拟spring的解析bean.xml的类
* @author river
*
*/
public class RiverClassPathXMLApplicationContext
{
private List<BeanDefinition> beanDefinitions = new ArrayList<BeanDefinition>();
private Map<String, Object> sigletons = new HashMap<String, Object>();
public RiverClassPathXMLApplicationContext(String filename)
{
this.readXML(filename);
this.instanceBeans();
}
/**
* 完成实例化
*/
private void instanceBeans()
{
for (BeanDefinition beanDefinition : beanDefinitions)
{
if (beanDefinition.getClassName() != null
&& !"".equals(beanDefinition.getClassName().trim()))
{
try
{
sigletons.put(beanDefinition.getId(), Class.forName(
beanDefinition.getClassName()).newInstance());
} catch (Exception e)
{
e.printStackTrace();
}
}
}
}
public Object getBean(String beanName)
{
return this.sigletons.get(beanName);
}
/**
* 读取配置文件,filename为bean的配置文件
*
* @param filename
*/
private void readXML(String filename)
{
SAXReader saxReader = new SAXReader();
Document document = null;
try
{
URL xmlpath = this.getClass().getClassLoader()
.getResource(filename);
document = saxReader.read(xmlpath);
Map<String, String> nsMap = new HashMap<String, String>();
nsMap.put("ns", "http://www.springframework.org/schema/beans");// 假如命名空间
XPath xsub = document.createXPath("//ns:beans/ns:bean");// 创建beans/bean
xsub.setNamespaceURIs(nsMap);// 设置命名空间
List<Element> beans = xsub.selectNodes(document);// 获取文档下的bean
for (Element element : beans)
{
String id = element.attributeValue("id");// 获取id属性
String clazz = element.attributeValue("class");// 获取class属性
BeanDefinition beanDefine = new BeanDefinition(id, clazz);
beanDefinitions.add(beanDefine);
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
PersonService 类的一个实现类:
public class PersonServiceBean implements PersonService {
/* (non-Javadoc)
* @see com.river.spring.PwesionService#save()
*/
public void save()
{
System.out.println("save ()");
}
}
bean.xml的配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="personService" class="com.river.service.impl.PersonServiceBean"/>
</beans>
测试:
/**
* personService为bean中的id
*/
public void instanceSpring()
{
RiverClassPathXMLApplicationContext ctx = new RiverClassPathXMLApplicationContext("beans.xml");
PersonService ps = (PersonService) ctx.getBean("personService");
ps.save();
}
结果:save(); 阅读更多
相关文章推荐
- Spring学习笔记(3)----编码剖析Spring管理Bean的原理
- Spring学习笔记(3)----编码剖析Spring管理Bean的原理
- spring2.5.6学习笔记四:编码剖析Spring管理Bean的原理
- ITCAST视频-Spring学习笔记(编码剖析Spring管理Bean的原理)
- Spring学习笔记——Sprin管理Bean的原理
- spring的管理bean和依赖注入的原理剖析
- Spring、编码剖析Spring管理Bean的原理
- ITCAST视频-Spring学习笔记(Spring管理的Bean的生命周期)
- ITCAST视频-Spring学习笔记(编码剖析@Resource注解的实现原理)
- Java框架spring 学习笔记(十):bean管理(注解和配置文件混合使用)
- spring2.5.6学习笔记六:配置Spring管理的bean的作用域
- ITCAST视频-Spring学习笔记(编码剖析Spring依赖注入的原理)
- Spring、编码剖析Spring管理Bean的原理
- Spring学习笔记(6)----编码剖析Spring依赖注入的原理
- ITCAST视频-Spring学习笔记(编码剖析Spring装配基本属性的原理)
- ITCAST视频-Spring学习笔记(让Spring自动扫描和管理Bean)
- Spring、编码剖析Spring管理Bean的原理
- ITCAST视频-Spring学习笔记(配置Spring管理的bean的作用域)
- Spring第三弹—–编码剖析Spring管理Bean的原理
- Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)