Spring学习笔记.docx
- 文档编号:29911759
- 上传时间:2023-08-03
- 格式:DOCX
- 页数:38
- 大小:722.63KB
Spring学习笔记.docx
《Spring学习笔记.docx》由会员分享,可在线阅读,更多相关《Spring学习笔记.docx(38页珍藏版)》请在冰豆网上搜索。
Spring学习笔记
Spring1
(重点)Spring简介1
(重点)IoC1
第一个Spring项目1
(重点)Bean的生命周期5
(重点)与Hibernate整合6
SSH整合22
Spring
(重点)Spring简介
核心是个轻量级(Lightweight)的容器,实现了IoC(InversionofControl)模式的容器(管理对象的生命周期,对象的组态,依赖式注入,可以控制对象在创建时是以原型和单例的方式来建立),是非侵入性框架(让应用程序感受不到框架的存在)
作用:
实现容器和程序之间的截耦合
(重点)IoC
InversionofControl---------控制反转
DI(DependencyInjection)----注入依赖:
程序不依赖于实例,而是程序与实例都依赖于抽象
IoC的抽象概念:
依赖关系的转移
像是高层模块不应该依赖低层模块,而是模块都必须依赖于抽象
实现必须依赖抽象,而不是抽象依赖实现
应用程序不应依赖于容器,而是容器服务于应用程序
低层:
贴近数据库
高层:
贴近业务逻辑
第一个Spring项目
新建一个web项目---右键----Myeclipse----AddSpring。
。
。
Next
Finish
1)User.java
publicclassUser{
privateStringname;
privateDatedate;
publicStringgetName(){
returnname;
}
publicvoidsetName(Stringname){
this.name=name;
}
publicDategetDate(){
returndate;
}
publicvoidsetDate(Datedate){
this.date=date;
}
2)applicationContext.xml
--一个bean标签对应一个类,可以是vo可以是DAO--> 3)TestIoC.java publicclassTestIoC{ publicstaticvoidmain(String[]args){ ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml"); Useruser=(User)context.getBean("user"); System.out.println(user.getName()+""+user.getDate()); } } 结果: 注意: 1)ApplicationContext的作用是负责读取Bean定义的文件,维护Bean之间的关系 2)ClassPathXmlApplicationContext从Classpath中读取XML文件,括号中写路径 3) 4) 注入方式: 类型2-----set型注入 类型3-----构造函数式注入 绑定分类: 1)按类型绑定 2)按名称绑定 id和vo中的Date的date必须相同 3)按构造函数绑定 4)自动绑定 (重点)Bean的生命周期 如果使用BeanFactory来管理Bean 1)Bean的建立 2)属性注入 3)BeanNameAware的setBeanName() 4)BeanFactoryAware的setBeanFactory() 5)BeanPostProcessors的processBeforeInitialization() 6)InitializingBean的afterPropertiesSet() 7)Bean定义档中定义init-method 8)BeanPostProcessors的processaAfterInitialization() 9)DisposableBean的destroy() 10)Bean定义档中定义destroy-method 如果是ApplicationContext: 则在执行BeanFactoryAware的setBeanFactory()阶段之后,若Bean有实例org.springframework.context.ApplicationContextAware接口,则执行其setApplicationContext()方法,接着才继续进行BeanPostProcessors的processBeforeInitialization()及之后的流程 (重点)与Hibernate整合 右键----AddHibernate。 。 。 Next Next Next Next Finish 在applicationContext.xml中增加了: 将表关联上: 右键-----Hibernate。 。 。 ---- Next Next Finish applicationContext.xml 新增: 完成 1)新建包com.cstp.hibernate.dao------新建接口Iperson publicinterfaceIPerson{ publicvoidsave(PersontransientInstance); publicvoiddelete(PersonpersistentInstance); publicPersonfindById(java.lang.Integerid); publicListfindByExample(Personinstance); publicListfindByProperty(StringpropertyName,Objectvalue); publicListfindByName(Objectname); publicListfindByAge(Objectage); publicListfindBySex(Objectsex); publicListfindAll(); publicvoidattachDirty(Personinstance); } 2)新建包com.cstp.hibernate.dao.impl 将PersonDAO复制到此包下,将原来的删除,并继承接口Iperson publicclassPersonDAOextendsHibernateDaoSupportimplementsIperson{ …………….. } 3)将applicationContext.xml文件修改: 4)新建测试类: TestDAO.java publicclassTestDAO{ publicstaticvoidmain(String[]args){ ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml"); IPersondao=(IPerson)context.getBean("PersonDAO"); Personperson=newPerson(); person.setAddress("110"); person.setAge(22); person.setBirthday(newDate()); person.setCity("eee"); person.setEmail("12312"); person.setHobby("like"); person.setName("LOVE"); person.setPr("123"); person.setRemark("null"); person.setSex("男"); person.setTel("567667"); person.setTown("23432"); dao.save(person); } } 结果为: Hibernate: selectmax(id)fromperson Hibernate: insertintotest.person(name,age,birthday,sex,tel,pr,city,town,email,address,hobby,remark,id)values(? ? ? ? ? ? ? ? ? ? ? ? ? ) 但是在数据库中没有插入 此时需要使用到事务(事务仅仅是创建是不能用的) 1)第一种方法 在applicationContext.xml中添加,绿色字体为固定 --创建一个事务--> --使用事务接口,将接口与之对应的dao进行对照,都放在事务中进行管理,设置它们不同的方法的事务行为--> --①生成一个代理工厂bean--> --id随便取--> --②把事务放进来--> --③找到一个代理接口--> --④有哪些属性行为,*代表全部--> 注意: 解释PROPAGATION_REQUIRED: 支持现在的事务,如果没有的话就建立一个新的事务 是设置哪些方法需要事务,*代表所有方法需要事务 2)第二种方法 ①新建包com.cstp.service 新建接口PersonService,内容和Iperson一样 新建PersonServiceImpl.java publicclassPersonServiceImplimplementsPersonService{ privatePersonDAOpersondao;//一定得有get和set方法 publicPersonDAOgetPersondao(){ returnpersondao; } publicvoidsetPersondao(PersonDAOpersondao){ this.persondao=persondao; } publicvoidattachDirty(Personinstance){ persondao.attachDirty(instance); } publicvoiddelete(PersonpersistentInstance){ persondao.delete(persistentInstance); } publicListfindAll(){ returnpersondao.findAll(); } publicListfindByAge(Objectage){ returnpersondao.findByAge(age); } publicListfindByExample(Personinstance){ returnpersondao.findByExample(instance); } publicPersonfindById(Integerid){ returnpersondao.findById(id); } publicListfindByName(Objectname){ returnpersondao.findByName(name); } publicListfindByProperty(StringpropertyName,Objectvalue){ returnpersondao.findByProperty(propertyName,value); } publicListfindBySex(Objectsex){ returnpersondao.findBySex(sex); } publicvoidsave(PersontransientInstance){ persondao.save(transientInstance); } } ②在applicationContext.xml文件中添加 --创建一个事务--> --将事务声明单独独立出来,可以进行复用--> --将需要进行事务管理的service放到相应的事务中去--> ③测试类改为 publicclassTestDAO{ publicstaticvoidmain(String[]args){ ApplicationContextcontext=newClassPathXmlApplicationContext("applicationContext.xml"); PersonServiceservice=(PersonService)context.getBean("serviceProxy"); Personperson=newPerson(); person.setAddress("110"); person.setAge(22); person.setBirthday(newDate()); person.setCity("eee"); person.setEmail("12312"); person.setHobby("like"); person.setName("LOVE2"); person.setPr("123"); person.setRemark("null"); person.setSex("男"); person.setTel("567667"); person.setTown("23432"); service.save(person); } } 3)第三种方法(运用AOP的方式) 在applicationContext.xml中修改: xmlversion="1.0"encoding="UTF-8"? > 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"> class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> value="classpath: hibernate.cfg.xml"> --创建一个事务--> --声明一个事务,以及相关的事件行为--> adviceid="txAdvice"transaction
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Spring 学习 笔记