黎活明Spring视频学习心得:
一 模拟Spring容器
1)读取Spring 的bean.xml文件,解析各个bean的id和class(封装成一个新类),得到list<类>;
2)根据得到的各beans利用反射技术实例化bean Map(id,Object);
3)写一个通过调用得到bean的实例:getBean(String id);
二 Spring实例化bean的三种方法
1)通过构造方法:<bean id="" class=""></bean>
2)通过静态工厂实例化: factory里面写一个静态方法creactePersonService,方法可以返回一个实例化的bean;
配置文件<bean id="personService1" class="service.PersonServiceBeanFactory" factory-method="creactePersonService"></bean>
3)使用实例化工厂创建bean实例 :factory里面写一个方法creactePersonService1,方法返回一个实例化的bean;
配置文件先实例化工厂,再在要获取的bean里面写一个属性factory-bean
<bean id="personServiceFactory" class="service.PersonServiceBeanFactory"></bean>
<bean id="personService2" factory-bean="personServiceFactory" factory-method="creactePersonService1"></bean>
三 bean的作用域
scope: prototype 和singleton :prototype 每次返回不同的实例,singleton每次返回同一个实例。
四bean初始化和销毁
默认情况下:
当scope="prototype"时,当getBean()时,才初始化bean;
当scope="singleton"时,容器一启动,变实例化bean。
可以设置bean实例化时间:
用于beans default-lazy-init="true"(少用),作用域所有bean
lazy-init="true" 用于bean
初始化方法
init-method="initMethodName"
容器关闭前执行方法
destroy-method="destroyMethodName"
AbstractApplicatinContext cxt=new ClassPathXmlApplicationContext("beans.xml");
---------------------------------------------PropertyDefinition.java------------------------------------------------------
------------------------------ItCastClassPathXmlApplicationContext.java------------------------------------------
一 模拟Spring容器
1)读取Spring 的bean.xml文件,解析各个bean的id和class(封装成一个新类),得到list<类>;
2)根据得到的各beans利用反射技术实例化bean Map(id,Object);
3)写一个通过调用得到bean的实例:getBean(String id);
二 Spring实例化bean的三种方法
1)通过构造方法:<bean id="" class=""></bean>
2)通过静态工厂实例化: factory里面写一个静态方法creactePersonService,方法可以返回一个实例化的bean;
配置文件<bean id="personService1" class="service.PersonServiceBeanFactory" factory-method="creactePersonService"></bean>
3)使用实例化工厂创建bean实例 :factory里面写一个方法creactePersonService1,方法返回一个实例化的bean;
配置文件先实例化工厂,再在要获取的bean里面写一个属性factory-bean
<bean id="personServiceFactory" class="service.PersonServiceBeanFactory"></bean>
<bean id="personService2" factory-bean="personServiceFactory" factory-method="creactePersonService1"></bean>
三 bean的作用域
scope: prototype 和singleton :prototype 每次返回不同的实例,singleton每次返回同一个实例。
四bean初始化和销毁
默认情况下:
当scope="prototype"时,当getBean()时,才初始化bean;
当scope="singleton"时,容器一启动,变实例化bean。
可以设置bean实例化时间:
用于beans default-lazy-init="true"(少用),作用域所有bean
lazy-init="true" 用于bean
初始化方法
init-method="initMethodName"
容器关闭前执行方法
destroy-method="destroyMethodName"
AbstractApplicatinContext cxt=new ClassPathXmlApplicationContext("beans.xml");
cxt.close();
---------------------------------------------------------beans.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"
xmlns:aop="http://www.springframework.org/schema/aop"
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/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- scope="prototype"每次调用都会返回一个新的对象 -->
<!--
<bean id="personService" class="service.impl.PersonServiceImpl" scope="prototype"></bean>
-->
<!-- 使用静态工厂方法创建bean实例。 -->
<!--
<bean id="personService1" class="service.PersonServiceBeanFactory" factory-method="creactePersonService"></bean>
-->
<!-- 使用实例化工厂创建bean实例。 -->
<!--
<bean id="personServiceBeanFactory" class="service.PersonServiceBeanFactory"></bean>
<bean id="personService2" factory-bean="personServiceBeanFactory" factory-method="creactePersonService1"></bean>
-->
<!-- 指定bean初始化方法和销毁方法 -->
<bean id="ss" class="service.impl.PersonServiceImpl" init-method="init">
<property name="personDao" ref="personDao"></property>
</bean>
<bean id="personDao" class="dao.impl.PersonDaoImpl"></bean>
</beans>
package junit.test;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.PersonService;
import service.impl.PersonServiceImpl;
public class SpringTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Test public void instanceSpring(){
ItCastClassPathXmlApplicationContext ctx=new ItCastClassPathXmlApplicationContext("beans.xml");
/*PersonService ps=(PersonServiceImpl) ctx.getBean("personService");
PersonService ps1=(PersonServiceImpl) ctx.getBean("personService");
System.out.println(ps==ps1);
ps.save();*/
PersonService ps=(PersonServiceImpl) ctx.getBean("ss");
ps.save();
}
}
----------------------------------------------------BeanDefinition.java---------------------------------------------
package junit.test;
import java.util.ArrayList;
import java.util.List;
public class BeanDefinition {
private String id;
private String clazz;
private List<PropertyDefinition> properties=new ArrayList<PropertyDefinition>();
public BeanDefinition(){}
public BeanDefinition(String id,String clazz){
this.id=id;
this.clazz=clazz;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getClazz() {
return clazz;
}
public void setClazz(String clazz) {
this.clazz = clazz;
}
public List<PropertyDefinition> getProperties() {
return properties;
}
public void setProperties(List<PropertyDefinition> properties) {
this.properties = properties;
}
}
---------------------------------------------PropertyDefinition.java------------------------------------------------------
package junit.test;
public class PropertyDefinition {
private String name;
private String ref;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRef() {
return ref;
}
public void setRef(String ref) {
this.ref = ref;
}
public PropertyDefinition(String name, String ref) {
super();
this.name = name;
this.ref = ref;
}
}
------------------------------ItCastClassPathXmlApplicationContext.java------------------------------------------
package junit.test;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.io.SAXReader;
public class ItCastClassPathXmlApplicationContext {
private List<BeanDefinition> beanDefines=new ArrayList<BeanDefinition>();
private Map<String,Object> sigletons=new HashMap<String, Object>();
public ItCastClassPathXmlApplicationContext(String filename){
this.readXml(filename);
this.instanceBeans();
this.injectBeans();
}
/**
* 为bean对象注入值。
*
* @description
* @return void
*/
private void injectBeans() {
for(BeanDefinition beanDefinetion :beanDefines){
Object bean=sigletons.get(beanDefinetion.getId());
if(bean!=null){
try {
PropertyDescriptor[] ps=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
for(PropertyDefinition propertyDefinition:beanDefinetion.getProperties()){
for(PropertyDescriptor propertydesc:ps){
if(propertyDefinition.getName().equals(propertydesc.getName())){
Method setter=propertydesc.getWriteMethod();
Object value=sigletons.get(propertyDefinition.getRef());
if(setter!=null){
setter.setAccessible(true);
setter.invoke(bean, value);
}
break;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
/**
* 完成bean实例化
*/
private void instanceBeans(){
for(BeanDefinition beanDefine:beanDefines){
try {
sigletons.put(beanDefine.getId(),Class.forName(beanDefine.getClazz()).newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
*
* 用于读取配置文件
*/
private void readXml(String filename) {
SAXReader saxReader=new SAXReader();
Document document=null;
try{
//URL xmlpath=this.getClass().getClassLoader().getResource(filename);
document=saxReader.read(new File(filename));
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);
XPath propertysub=element.createXPath("ns:property");
propertysub.setNamespaceURIs(nsMap);
List<Element> properties=propertysub.selectNodes(element);
for(Element property:properties){
String name=property.attributeValue("name");
String ref=property.attributeValue("ref");
System.out.println(name+"="+ref);
PropertyDefinition propertyDefinition=new PropertyDefinition(name,ref);
beanDefine.getProperties().add(propertyDefinition);
}
beanDefines.add(beanDefine);
}
}catch(Exception e){
e.printStackTrace();
}
}
public Object getBean(String beanName){
return sigletons.get(beanName);
}
}