spring环境的搭建
需要导入的jar文件为
spring-framework-2.5.6/dist/spring.jar
spring-framework-2.5.6/lib/jakarta-commons/commons-logging.jar
创建spring的XML文件,我取名为bean.xml
头文件可以从例子或文档中拷贝,我从/spring-framework-2.5.6/docs/reference/html_single/index.html文档拷贝
<?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">
</beans>
创建bean并将其注入到spring xml文件 当中
<bean id="personservice" class="joeho.blog.service.impl.PersonServiceBean"></bean>
bean 中id或name属性都代表bean的别名,其中name属性可以带特殊字符,id中则不能
测试环境是否成功,编写junit实例进行测试
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test
public void instanceSpring(){
ApplicationContext act = new ClassPathXmlApplicationContext("Beans.xml");
PersonService ps=(PersonService)act.getBean("personservice");
ps.save();
}
}
模拟spring容器管理Bean的一个例子
package junit.test;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import joeho.vo.BeanDefinition;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class JoehoaClassPathXMLApplicationContext {
private List<BeanDefinition> beanDefines = new ArrayList<BeanDefinition>();
private Map<String,Object> singeton = new HashMap<String,Object>();
public JoehoaClassPathXMLApplicationContext(String xmlName) {
this.readXml(xmlName);
this.instanceBeans();
}
/**
* 读XML文件
* @param xmlName
*/
private void readXml(String xmlName) {
SAXReader saxReader = new SAXReader();
Document document = null;
try {
URL xmlPath = this.getClass().getClassLoader().getResource(xmlName);
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> list = xsub.selectNodes(document);//获取文档下的所有BEAN节点
for(Element element:list){
String id = element.attributeValue("id");//获取ID属性值
String classzz = element.attributeValue("class");//获取class属性值
BeanDefinition beanDefin = new BeanDefinition(id,classzz);
beanDefines.add(beanDefin);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
/**
* 完成bean的实例化
*/
private void instanceBeans() {
for(BeanDefinition beans:beanDefines){
try {
if(!beans.getId().equals("")&&beans!=null&&!beans.getClasszz().equals("")){
singeton.put(beans.getId(), Class.forName(beans.getClasszz()).newInstance());
}
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 获取Bean实例
* @param beanName
* @return
*/
public Object getBean(String beanName){
return singeton.get(beanName);
}
}
package joeho.vo;
public class BeanDefinition {
private String id;
private String classzz;
public BeanDefinition(String id, String classzz) {
this.id = id;
this.classzz = classzz;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClasszz() {
return classzz;
}
public void setClasszz(String classzz) {
this.classzz = classzz;
}
}