spring学习笔记.docx
- 文档编号:10570413
- 上传时间:2023-05-26
- 格式:DOCX
- 页数:38
- 大小:30.62KB
spring学习笔记.docx
《spring学习笔记.docx》由会员分享,可在线阅读,更多相关《spring学习笔记.docx(38页珍藏版)》请在冰点文库上搜索。
spring学习笔记
Spring的定义:
Spring是一个开源的控制反转(InversionofControl,IoC)和面向切面(AOP)的容器框架.它的主要目得是简化企业开发.
在项目中引入spring立即可以带来下面的好处
●降低组件之间的耦合度,实现软件各层之间的解耦。
●可以使用容器提供的众多服务,如:
事务管理服务、消息服务等等。
当我们使用容器管理事务时,开发人员就不再需要手工控制事务.也不需处理复杂的事务传播。
●容器提供单例模式支持,开发人员不再需要自己编写实现代码。
●容器提供了AOP技术,利用它很容易实现如权限拦截、运行期监控等功能。
●容器提供的众多辅作类,使用这些类能够加快应用的开发,如:
JdbcTemplate、HibernateTemplate。
●Spring对于主流的应用框架提供了集成支持,如:
集成Hibernate、JPA、Struts等,这样更便于应用的开发。
对于spring容器,它提供了很多服务,但这些服务并不是默认为应用打开的,应用需要某种服务,还需要指明使用该服务,如果应用使用的服务很少,如:
只使用了spring核心服务,那么我们可以认为此时应用属于轻量级的,如果应用使用了spring提供的大部分服务,这时应用就属于重量级。
目前EJB容器就因为它默认为应用提供了EJB规范中所有的功能,所以它属于重量级。
使用Spring需要的jar
到http:
//www.springsource.org/download下载spring,然后进行解压缩,在解压目录中找到下面jar文件,拷贝到类路径下
dist\spring.jar
lib\jakarta-commons\commons-logging.jar
如果使用了切面编程(AOP),还需要下列jar文件
lib/aspectj/aspectjweaver.jar和aspectjrt.jar
lib/cglib/cglib-nodep-2.1_3.jar
如果使用了JSR-250中的注解,如@Resource/@PostConstruct/@PreDestroy,还需要下列jar文件
lib\j2ee\common-annotations.jar
spring的配置文件模版
xmlversion="1.0"encoding="UTF-8"?
>
//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"> ..... 该配置模版可以从spring的参考手册或spring的例子中得到。 配置文件的取名可以任意,文件可以存放在任何目录下,但考虑到通用性,一般放在类路径下。 编写spring配置文件时,不能出现帮助信息 由于spring的schema文件位于网络上,如果机器不能连接到网络,那么在编写配置信息时候就无法出现提示信息,解决方法有两种: 1。 让机器上网,eclipse会自动从网络上下载schema文件并缓存在硬盘上。 2。 手动添加schema文件,方法如下: windwos->preferences->myeclipse->filesandeditors->xml->xmlcatalog 点"add",在出现的窗口中的KeyType中选择URI,在location中选"Filesystem",然后在spring解压目录的dist/resources目录中选择spring-beans-2.5.xsd,回到设置窗口的时候不要急着关闭窗口,应把窗口中的KeyType改为Schemalocation,Key改为http: //www.springframework.org/schema/beans/spring-beans-2.5.xsd 实例化spring容器 方法一: 在类路径下寻找配置文件来实例化容器 ApplicationContextctx=newClassPathXmlApplicationContext(newString[]{"beans.xml"}); 方法二: 在文件系统路径下寻找配置文件来实例化容器 ApplicationContextctx=newFileSystemXmlApplicationContext(newString[]{“d: \\beans.xml“}); Spring的配置文件可以指定多个,可以通过String数组传入。 当spring容器启动后,因为spring容器可以管理bean对象的创建,销毁等生命周期,所以我们只需从容器直接获取Bean对象就行,而不用编写一句代码来创建bean对象。 从容器获取bean对象的代码如下: ApplicationContextctx=newClassPathXmlApplicationContext(“beans.xml”); OrderServiceservice=(OrderService)ctx.getBean("personService"); 运用自己写的ApplicationContext的实现类,模仿spring的实现原理,用dom4j来读取xml文件。 /** *用于存放配置文件中,类的id和类名的类 * * *@author谭栋 * *@since2010-10-26下午12: 39: 08 * *@versionv1.0 */ publicclassBeanDefinition{ privateStringid; privateStringclassName; publicBeanDefinition(Stringid,StringclassName){ this.id=id; this.className=className; } publicStringgetId(){ returnid; } publicvoidsetId(Stringid){ this.id=id; } publicStringgetClassName(){ returnclassName; } publicvoidsetClassName(StringclassName){ this.className=className; } } ** *@author谭栋 * *@since2010-10-27下午10: 25: 27 * *@versionv1.0 * *用与存放property的属性的类 */ publicclassPropertyDefinition{ privateStringname; privateStringref; privateStringvalue; publicStringgetName(){ returnname; } publicvoidsetName(Stringname){ this.name=name; } publicPropertyDefinition(Stringname,Stringref,Stringvalue){ this.name=name; this.ref=ref; this.value=value; } publicStringgetValue(){ returnvalue; } publicvoidsetValue(Stringvalue){ this.value=value; } publicStringgetRef(){ returnref; } publicvoidsetRef(Stringref){ this.ref=ref; } } /* *测试实现注入的类 */ publicclassPersonServiceBeanimplementsPersonService{ /*(non-Javadoc) *@see.service.impl.PersonService#save() */ privatePersonDaospersonDaos; privateIntegerid; publicIntegergetId(){ returnid; } publicvoidsetId(Integerid){ this.id=id; } publicPersonDaosgetPersonDaos(){ returnpersonDaos; } publicvoidsetPersonDaos(PersonDaospersonDaos){ this.personDaos=personDaos; } publicvoidsave(){ System.out.println("id: "+this.id); personDaos.save(); } } importjava.beans.IntrospectionException; importjava.beans.Introspector; importjava.beans.PropertyDescriptor; importjava.lang.reflect.Method; import.URL; importjava.util.ArrayList; importjava.util.HashMap; importjava.util.List; importjava.util.Map; importmons.beanutils.ConvertUtils; importorg.dom4j.Document; importorg.dom4j.Element; importorg.dom4j.XPath; importorg.dom4j.io.SAXReader; import.service.impl.PropertyDefinition; /** *@author谭栋 * *@since2010-10-26上午06: 47: 18 * *@versionv1.0 * *模仿spring,实现spring的内部的实现原理 */ publicclassTdwxyClassPathXmlApplicationContext{ privateList privateMap /** *运用构造函数,进行配置文件的读取和类的实例化 */ publicTdwxyClassPathXmlApplicationContext(StringconfigLocation){ this.readXML(configLocation);//读取配置文件 this.addBeans();//实例化类 this.injectBeans();//注入依赖对象或基本数据类型 } /** *注入对象或基本数据类型 */ privatevoidinjectBeans(){ for(BeanDefinitionbeanDefinition: beanDefines){ Objectbean=beans.get(beanDefinition.getId()); if(bean! =null){ try{ //获取bean实例的属性 PropertyDescriptor[]propertyDescriptor=Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors(); //让bean的属性名和property的name属性比较 for(PropertyDefinitionpropertyDefinition: beanDefinition.getPropertys()){ for(PropertyDescriptorpropertyDesc: propertyDescriptor){ if(propertyDefinition.getName().equals(propertyDesc.getName())){ //如果相同,则调用属性的setter(写)方法 Methodmethod=propertyDesc.getWriteMethod(); Objectvalue=null; if(method! =null){ if(propertyDefinition.getRef()! =null&&! "".equals(propertyDefinition.getRef().trim())){ value=beans.get(propertyDefinition.getRef());//获取实例对象 }else{ //运用工具类,将string类型的数据,转换成类中定义的类型 value=ConvertUtils.convert(propertyDefinition.getValue(),propertyDesc.getPropertyType());//获取基本类型 } method.setAccessible(true); //如果是私有的,也设置为可访问 method.invoke(bean,value); //传入bean的实例或基本数据类型 } break; } } } }catch(Exceptione){ //TODOAuto-generatedcatchblock e.printStackTrace(); } } } } /** *运用dom4j读取xml文件 * *获取benas.xml的节点信息,并把id和class存入listbeanDefines中 *@paramfilename */ privatevoidreadXML(Stringfilename){ SAXReadersaxReader=newSAXReader(); Documentdocument=null; try{ URLxmlpath=this.getClass().getClassLoader().getResource(filename); document=saxReader.read(xmlpath); Map nsMap.put("ns","http: //www.springframework.org/schema/beans");//加入命名空间 XPathxsub=document.createXPath("//ns: beans/ns: bean");//创建beans/bean查询路径 xsub.setNamespaceURIs(nsMap);//设置命名空间 List for(Elementelement: beans){ Stringid=element.attributeValue("id");//获取id属性值 Stringclazz=element.attributeValue("class");//获取class属性值 BeanDefinitionbeanDefine=newBeanDefinition(id,clazz); XPathproXPath=element.createXPath("ns: property");//获取当前节点下的子节点 proXPath.setNamespaceURIs(nsMap); List for(Elementproperty: propertys){ Stringname=property.attributeValue("name"); Stringref=property.attributeValue("ref"); Stringvalue=property.attributeValue("value"); PropertyDefinitionpropertyD=newPropertyDefinition(name,ref,value); beanDefine.getPropertys().add(propertyD);//设置bean的属性 } beanDefines.add(beanDefine); } }catch(Exceptione){ e.printStackTrace(); } } /** *根据id获取类的实例 *@paramname *@return */ publicObjectgetBean(Stringname){ Objectobj=beans.get(name); if(obj! =null) returnobj; else{ System.out.println("您输入的节点不存在~! 请重新输入~! "); returnnull; } } /** *实例化类,并存入到mapbeans中 * */ publicvoidaddBeans(){ for(BeanDefinitionb: beanDefines){ try{ if(b.getClassName()! =null){ Objectobj=Class.forName(b.getClassName()).newInstance(); beans.put(b.getId(),obj); } }catch(Exceptione){ e.printStackTrace(); } } } } Bean.xml的配置 xmlversion="1.0"encoding="UTF-8"? > //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"> factory-method="createBean"/> /** *spring的测试类 * *@author谭栋 * *@since2010-10-28上午09: 07: 01 * *@versionv1.0 */ publicclassSpringTest{ @BeforeClass publicstaticvoidsetUpBeforeClass()throwsException{ } @TestpublicvoidinstanceSpring(){ //ApplicationContextctx=newClassPathXmlApplicationContext("beans.xml"); TdwxyClassPathXmlApplicationContextctx=newTdwxyClassPathXmlApplicationContext("beans.xml"); PersonServiceps=(PersonService)ctx.getBean("personService"); ps.save(); } } 三种实例化bean的方法 1.使用类构造器实例化
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- spring 学习 笔记