spring笔记

合集下载

Spring笔记

Spring笔记

Spring环境搭建1、spring依赖库a)SPRING_HOME/dist/spring.jarb)SPRING_HOME/lib/Jakarta-commons/commons-loggin.jarc)SPRING_HOME/lib/log4j/log4j-1.2.14.jar2、拷贝spring配置文件到src下3、拷贝log4j配置文件到src下4、在UserManagerImpl 中提供构造函数或setter方法,spring将实例化好的UserDao实现注入给我们5、让spring管理我们的对象的创建和依赖,必须在spring配置中进行定义6、编写客户端Spring Ioc容器的关键点:*必须将被管理的对象定义到spring配置文件中*必须定义构造函数或setter方法,让spring将对象注入过来什么是属性编辑器,作用:*自定属性编辑器,spring配置文件中的字符串转换成相应的对象进行注入*spring已经有内置的属性编辑器,我们可以可以根据需求自己定义属性编辑器*如何定义属性编辑器*继承PropertyEditorSuppotr类,覆写setAsText()方法,如:public class UtilDatePropertyEditor extends PropertyEditorSupport {private String format = "yyyy-MM-dd";public void setAsText(String text) throwsIllegalArgumentException {System.out.println("UtilDatePropertyEditor.setAsText() --- text="+ text);SimpleDateFormat sdf = new SimpleDateFormat(format);try {Date d = sdf.parse(text);this.setValue(d);} catch (ParseException e) {e.printStackTrace();}}public void setFormat(String format) {this.format = format;}}*将属性编辑器注册到spring中,如:<bean id="customEditorConfigurer"class="org.springframework.beans.factory.config.CustomEditorConfi gurer"><property name="customEditors"><map><entry key="java.util.Date"><beanclass="com.hdaccp.spring.UtilDatePropertyEditor"><property name="format" value="yyyy-MM-dd"/></bean></entry></map></property></bean>依赖对象的注入方式,可以采用:*ref 属性*<ref>标签<property name="Bean4"><ref bean="Bean4"/></property>*内部<bean>来定义如何将公共的注入定义描述出来?*通过<bean>标签定义公共属性,指定<bean id="beanAbstract" abstract="true">*具有相同属性的类在<bean>标签中指定其parent属性 (示例代码见如下:)<bean id="beanAbstract" abstract="true"><property name="id" value="1000"/><property name="name" value="Jack"/></bean><bean id="bean3" class="com.hdaccp.spring.Bean3"parent="beanAbstract"><property name="password" value="123"/></bean><bean id="bean4"class="com.hdaccp.spring.Bean4" parent="beanAbstract"/> Spring Bean的作用域:Scope可以取值:*singleton:每次调用getBean的时候返回相同的实例*prototype:每次调用getBEan的时候返回不同的实例自动装配(autowrite)*根据名称自动装配:Spring 对AOP的(采用Annotation的方法)1、spring的依赖库*SPRING_HOME/dist/spring.jar*SPRING_HOME/lib/Jakarta-commons/commons_logging.jar*SPRING_HOME/lib/aspectj/*.jar2、采用Aspect定义切面(采用Annotation方式实现AOP)3、在Aspect定义Ponitcut和Advice4、启用Aspect对Annotation的支持并具将Aspect类和目标对象配置到Ioc容器中<!--启用Aspect对Annotation的支持 --><aop:aspectj-autoproxy/><bean id="securityHandler"class="com.hdaccp.spring.SecurityHanderler"/><bean id="userManager" class="erManagerImpl"/>注意:在这种方法定义中,切入点的方法是不被执行的,它存在的目的仅仅是为了重中切入点,即Advice中通过方法名引用这个切入点AOP:*Cross cuttion concern ---横切性关注点*Aspect ---(横切性关注点模块化——切面)*Advice---横切性关注点具体实现*Pointcut ---应用切面范围*Joinpont ---执行点、spring目前只是方法*Weave ---Aspect应用目录对象的过程—织入*Target Object ---只是对象实现——目标对象*proxy ---代理*Introduction/***定义Aspect*@author Administrator**/@Aspectpublic class SecurityHanderler {/***定义Pointcut,Pointcut的名称就是allAddMethod,此方法不能有返回值和参数,只是该方法只是一个*标识**Pointcut的内容是一个表达式描述那些对象的那些方法(订阅JoinPoint)*/@Pointcut("execution(* add(...))")private void allAddMethod(){};/***定义Advice,标识在那个切入点,何处织入此方法*/@Before("allAddMethod()")private void checkSecurity(){System.out.println("--------------------checkSecurity()------------------");}}Spring 对AOP的(采用配置文件的方法)<bean id="securityHanderler"class="com.hdaccp.spring.SecurityHanderler"/><bean id="userManager" class="erManagerImpl"/><aop:config><aop:aspect id="security" ref="securityHanderler"><aop:pointcut id="allAddMethod" expression="execution(*erManagerImpl.add*(..))"/><aop:before method="checkSecurity" pointcut-ref="allAddMethod"/></aop:aspect></aop:config>Spring对AOP的支持* Aspect默认情况下不用实现接口,但对于目标对象(Target Object.java),在默认情况下必须实现接口,如果没有实现接口必须引入CGLIB库* 我们可以通过Advice中添加一个JoinPoint参数,这个值会由spring自动传入,从JoinPoint中可以取得参数值,方法名等等*如果目标对象实现了接口,默认情况下会采用JDK的动态代理实现AOP*如果目标对象实现了接口,可以强制使用CGLIB 实现AOP*如果目标对象实现了接口,必须采用CGLIB库,spring会自动在JDK动态理和CGLIB之转换如何强制使用CGLIB实现AOP?* 添加CGBLB库,SPRING_HOME/CGLIB/*.jar* 在spring配置文件中加入<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>JDK动态代理和CGLIB字节码生成的区别?* JDK动态代理只能对实现了接口的类生成代理,而不能针对类* CGLIB是针对类实现代理,主要是对指定的类生成一个子类,覆盖其中方法因为是继承,所以该类或方法最好不要声明成finalHibernate编程式事务1、getCurrentSession()与openSession()区别?*采用getCurrentSession()创建session会绑定到当前线程中,而采用openSession创建的session则不会*用getCurrentSession()创建session在commit或rollback时会自动关闭,而采用openSession()2、使用getCurrentSession()需要在hbiernate.cfg.xml文件中加入如下配置:*如果使用的是本地事务(jdbc事务)<property name="hibernate.current_session_context_class">thread</property> * 如果使用的是全局事务(jta事务)<property name="hibernate.current_session_context_class">jta</property>Hibernate声明式事务1、声明式事务配置* 配置SessionFactory<!-- 配置sessionFactory --><bean id="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation"><value>classpath:hibernate.cfg.xml</value></property></bean>* 配置事务管理器<!-- 配置事务管理器 --><bean id="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"><ref bean="sessionFactory"/></property></bean>* 事务的传播性<!-- 配置事务传播性 --><tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes><tx:method name="add*" propagation="REQUIRED"/><tx:method name="del*" propagation="REQUIRED"/><tx:method name="modify*" propagation="REQUIRED"/><tx:method name="*" read-only="true"/></tx:attributes></tx:advice>*那些业务逻辑方法使用事务<aop:config><aop:pointcut id="allManagerMethod" expression="execution(*ermgr.manager.*.*(..)) "/><aop:advisor pointcut-ref="allManagerMethod" advice-ref="txAdvice"/></aop:config>2、编写业务逻辑方法* 继承HibernateDaoSupport类,使用HibernateTemplate来持久化,HiberanteTemplate是Hibernate Session的轻量级封装* 默认情况下运行期异常才会回滚(包括继承了RuntimeException子类),普通异常是不会滚的* 编写业务逻辑方法时,最好将异常一直向上抛出,在表示层(Struts)处理*关于事务边界的设置,通常设置到业务层,不要添加到Dao上3、了解事务的几种传播特性1.PROPAGATION_PEQUIRED:如果存在一个事务,则支持当前事务。

spring学习笔记)(spring学习笔记))

spring学习笔记)(spring学习笔记))

---------------------------------------------------------------最新资料推荐------------------------------------------------------ spring学习笔记)(spring学习笔记))spring 学习笔记)(spring 学习笔记)) First, the spring configuration file; XML, version=, 1, encoding=, UTF-8 beans xmlns= mons.dbcp.BasicDataSource property name= URL value= jdbc:oracle:thin:@localhost:1521:cissst / property name= username value= Yxt / property name= password value= Yxt / property name= driverClassName value= oracle.jdbc.driver.OracleDriver / /bean bean id= SessionFactory Class= org.springframework.orm.hibernate3.LocalSessionFactoryBean property name= dataSource ref= DataSource / property name= mappingResources list valuecom/yxt/entity/users.hbm.xml/value /list /property property name= hibernateProperties props prop key=, hibernate.dialect,org.hibernate.dialect.Oracle9Dialect/prop prop key=, hibernate.show_sql, true/prop prop key=, hibernate.hbm2ddl.auto, update/prop /props /property /bean bean id=, hibernateTemplete, class=, org.springframework.orm.hibernate3.HibernateTemplateproperty name= sessionFactory ref= SessionFactory / /bean1 / 6bean id=, transActionManager, class=, org.springframework.orm.hibernate3.HibernateTransactionMana ger property name= sessionFactory ref= SessionFactory / /bean bean id=, baseDaoImpProxy, class=, org.springframework.transaction.interceptor.TransactionProx yFactoryBean property name= target ref= baseDaoImp / property name= transactionAttributes props prop key=, save*, PROPAGATION_REQUIRED/prop prop key=, delete*, PROPAGATION_REQUIRED/prop prop key=, update*, PROPAGATION_REQUIRED/prop prop key=, get*, PROPAGATION_REQUIRED, readOnly/prop prop key=, find*, PROPAGATION_REQUIRED, readOnly/prop prop, key=, *, PROPAGATION_REQUIRED, readOnly/prop /props /property /bean bean id= users class= ers / bean id= baseDaoImp class= com.yxt.daoImp.BaseDaoImp parent= hibernateTemplete / bean id=, baseServiceImp, class=, com.yxt.serviceImp.BaseserviceImp property name= BaseDao ref= baseDaoImp / /bean bean name=, /baseAction, class=, com.yxt.action.BaseAction property name= service ref= baseServiceImp / property name= users ref= users / /bean /beans Two, spring integration struts1 SSH collection: first add spring, add struts or hibernate, as long as the---------------------------------------------------------------最新资料推荐------------------------------------------------------ hibernate link database information to spring management will do. The first step: configuring the struts controller in spring, as follows, noting that the value of the name property must be the value of the request path of the path tag in struts. bean name=, /baseAction, class=, com.cissst.action.BaseAction The second step: to rewrite the struts configuration file, the value of the type of the action tag is configured as follows, or refer to the spring development manual, the web, chapter. action Path= /baseAction Type= org.springframework.web.struts.DelegatingActionProxy Parameter= method /action The third step: to overwrite the web.xml file, configure as follows: ! -- the main role when the project started some initialization parameters or class -- listenerlistener-classorg.springframework.web.context.ContextLoader Listener/listener-class /listener context-param param-namecontextConfigLocation/param-nameparam-value/WEB-INF/classes/applicationContext*.xml/param-v alue /context-param Three, transaction management; ! -- --HibernateTransactionManager: is a class provided by spring database transaction management bean id= transactionManager3 / 6Class=org.springframework.orm.hibernate3.HibernateTransactionMana ger property name= sessionFactory ref= sessionFactory / /bean ! -- TransactionProxyFactoryBean: this class is the tool we use when managing the transaction with our HibernateTransactionManager -- bean id= ibaseDaoProxy Class=org.springframework.transaction.interceptor.TransactionProx yFactoryBean property name= transactionManager ref= transactionManager / property name= target ref= ibaseDao / property name= transactionAttributes props prop key=, save*, PROPAGATION_REQUIRED/prop prop key=, delete*, PROPAGATION_REQUIRED/prop prop key=, update*, PROPAGATION_REQUIRED/prop prop key=, get*, PROPAGATION_REQUIRED, readOnly/prop prop key=, find*, PROPAGATION_REQUIRED, readOnly/prop prop, key=, *, PROPAGATION_REQUIRED, readOnly/prop /props /property /bean Four, spring in common nouns explanation. 1). (Inversion of, Control, inversion of control IoC): refers to the spring through an interface to manage a group of mutual work, management is the management of the spring format, new file object, there is the relationship between these categories---------------------------------------------------------------最新资料推荐------------------------------------------------------ (dependent). 2) injection (DI): putting the underlying interface into the upper layer, we call injection. 3) dependency: refers to the interface of another class in a class and stands in a hierarchical perspective. 4) dependency injection: the technique of controlling inversion 1) implemented by the set method of attributes. 2) implemented by a constructor Five, spring AOP explain. AOP: aspect oriented programming sometimes called oriented programming is the main method of business oriented or object-oriented programming. (1). What is the section (aspect): the cross section of the object object ---- operation in the process of its essence is a kind of ball is new. (2). The connection point (joinPoint): ----- a stage in the operation of procedure connection point refers to the methods in a class or abnormal.(3). The processing logic (advice) Notice: I - business logic written in a connection point is connected in the internal business process. (around) the connection point before and after the main connection point. (before) some connections that are processed before the most significant join point. (after) some connection points that are processed after the most significant join point. (throw) an exception may occur5 / 6in the notification process. (4) point of tangency (pointCut): refers to a set of connected points. Conclusion: in short, aspect oriented programming (aspect oriented programming) is actually oriented to the most important business methods or object programming, and the secondary things are left to third party technologies (spring, AOP) to do. Six, configure the AOPin the spring configuration file Case: use the programming idea of AOP to deal with our affairs. In fact, the use of the Aop programming idea in spring is the business inside it. 1. replace the head of the spring configuration file. ! -- configuration - affairs tx:advice id=, allManagerMothod, transaction-manager=, transactionManager tx:attributes tx:method name= save* propagation= REQUIRED / tx:method name= delete* propagation= REQUIRED / tx:method name= update* propagation= REQUIRED / tx:method name= get* propagation= REQUIRED read-only= true / tx:method name= * propagation= REQUIRED read-only= true / /tx:attributes /tx:advice - configure AOP-- aop:config aop:pointcut id= serviceOperation expression= execution (* com.cissst.daoImp.*.* (..)): aop:advisor advice-ref= allManagerMothod pointcut-ref= serviceOperation / /aop:config。

Spring学习笔记(一)

Spring学习笔记(一)

Spring学习笔记(⼀)Spring学习笔记(⼀)这是⼀个沉淀的过程,⼤概第⼀次接触Spring是在去年的这个时候,当初在实训,初次接触Java web,直接学习SSM框架(当是Servlet都没有学),于是,养成了⼀个很不好的学习习惯,就是“照猫画虎”。

别⼈做什么,照着样⼦就是了,没有任何的思考,这样的学习习惯肯定不会⾛太远。

现在我产⽣很多疑惑,这是什么?为什么这么做?如何做的更好?因此这次笔记的主题就是《这是什么?》1. spring框架概述1.1 什么是springSpring是⼀个开源框架,Spring是于2003 年兴起的⼀个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍⽣⽽来。

它是为了解决企业应⽤开发的复杂性⽽创建的。

框架的主要优势之⼀就是其分层架构,分层架构允许使⽤者选择使⽤哪⼀个组件,同时为 J2EE 应⽤程序开发提供集成的框架。

Spring使⽤基本的JavaBean 来完成以前只可能由EJB完成的事情。

然⽽,Spring的⽤途不仅限于服务器端的开发。

从简单性、可测试性和松耦合的⾓度⽽⾔,任何Java应⽤都可以从Spring中受益。

Spring的核⼼是控制反转(IoC)和⾯向切⾯(AOP)。

简单来说,Spring是⼀个分层的JavaSE/EE full-stack(⼀站式) 轻量级开源框架。

[^摘⾃]: (百度百科)轻量级:与EJB对⽐,依赖资源少,销毁的资源少。

分层:⼀站式,每⼀个层都提供的解决⽅案1.2 spring由来Expert One-to-One J2EE Design and DevelopmentExpert One-to-One J2EE Development without EJB1.3 spring核⼼Spring的核⼼是控制反转(IoC)和⾯向切⾯(AOP)1.4 spring优点⽅便解耦,简化开发(⾼内聚低耦合)Spring就是⼀个⼤⼯⼚(容器),可以将所有对象创建和依赖关系维护,交给Spring管理spring⼯⼚是⽤于⽣成beanAOP编程的⽀持Spring提供⾯向切⾯编程,可以⽅便的实现对程序进⾏权限拦截、运⾏监控等功能声明式事务的⽀持只需要通过配置就可以完成对事务的管理,⽽⽆需⼿动编程⽅便程序的测试Spring对Junit4⽀持,可以通过注解⽅便的测试Spring程序⽅便集成各种优秀框架Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接⽀持降低JavaEE API的使⽤难度Spring 对JavaEE开发中⾮常难⽤的⼀些API(JDBC、JavaMail、远程调⽤等),都提供了封装,使这些API应⽤难度⼤⼤降低1.5 spring体系结构Spring框架是⼀个分层架构,它包含⼀系列的功能要素并被分为⼤约20个模块。

100-Spring学习笔记-面试题

100-Spring学习笔记-面试题

100-Spring学习笔记-⾯试题⼀、基本概念1、Spring优点1.1、轻量级、⾮⼊侵式:对现有的类结构没有影响1.2、可以提供众多服务,如事务管理,WS等1.3、对主流的框架提供了很多的集成⽀持,如hibernate,Struts2,像⼀个胶⽔⼀样,把⼀些好的框架粘合在⼀起⽅便实⽤。

1.4、使⽤Spring的IOC容器,将对象之间的依赖关系交给Spring,降低组件之间的耦合性,让我们更专注于应⽤逻辑1.5、Spring DI 机制降低了业务对象替换的复杂性1.6、Spring的⾼度可开放性,并不强制依赖于Spring,开发者可以⾃由选择Spring部分或全部2、Spring缺点2.1、没有SpringBoot好⽤⼆、Spring类1、ApplicationContext1.1、FileSystemXmlApplicationContext1.2、ClassPathXmlApplicationContext1.3、WebXmlApplicationContext三、SpringMVC1、执⾏流程1.1、⽤户发送请求⾄前端控制器DispatcherServlet1.2、DispatcherServlet收到请求调⽤HandlerMapping处理器映射器。

1.3、处理器映射器根据请求url找到具体的处理器,⽣成处理对象及处理器拦截器(如果有则⽣成)⼀并返回给DispatcherServlet 1.4、DispatcherServlet通过HandlerAdapter处理器适配器调⽤处理器1.5、执⾏处理器(Controller,也叫后端控制器)1.6、Controller执⾏完成返回ModelAndView1.7、HandlerAdapter将controller执⾏结果ModelAndView返回给DispaServlet1.8、DispatcherServlet将ModelAndView传给ViewReslover视图解析器1.9、ViewReslover解析后返回具体View1.10、DispatcherServlet对View进⾏渲染视图(即将模型数据填充视图中)1.11、DispatcherServlet响应⽤户2、常⽤注解2.1、@Controller ⽤于定义控制器类2.2、@ResponseBody 表⽰⽅法的返回结果直接写⼊HTTP response body中2.3、@PathVariable 获取路径参数2.4、@RequestParam ⽤在⽅法的参数前⾯2.5、@RequestBody 返回JSON格式2.6、@RestController 是@Controller和@ResponseBody的合集2.7、@RequestMapping 提供理由信息,负责URL到Controller中的具体函数2.8、@ControllerAdvice 统⼀处理异常3、servlet⽣命周期3.1、加载和实例化3.2、初始化3.3、请求处理3.4、服务终⽌四、AOP1、基本概念1.1、核⼼业务功能和切⾯功能分别独⽴进⾏开发,然后把切⾯功能和核⼼业务功能“编织”在⼀起,这就叫AOP1.2、让关注点代码与业务代码分离1.3、⾯向切⾯编程就是指:对很多功能都有的重复代码抽取,再运⾏的时候往业务⽅法上动态注⼊“切⾯类代码”1.4、应⽤场景:⽇志、事务管理、权限控制2、实现原理2.1、jdk动态代理2.1.1、主要通过Proxy.newProxyInstance()和InvocationHandler这两个类和⽅法实现2.1.2、实现过程①、创建代理类proxy实现Invocation接⼝,重写invoke()⽅法:调⽤被代理类⽅法时默认调⽤此⽅法②、将被代理类作为构造函数的参数传⼊代理类proxy③、调⽤Proxy.newProxyInsatnce(classloader,interfaces,handler)⽅法⽣成代理类。

Spring学习笔记

Spring学习笔记

Spring笔记SPRING笔记 (1)第一章SPRING简介 (3)一、S PRING是什么? (3)1、Spring是一个开源的用于简化采用Java语言开发企业级程序的一个分层的框架 (3)2、分层结构: (3)3、Spring源代码是公开的,但是非标准的 (4)二、S PRING的优点 (4)1、采用J2EE开发存在的问题: (4)2、为什么要使用Spring (4)三、S PRING的组成 (5)第二章SPRING的IOC (5)一、I O C的概念 (5)二、I O C的分类 (6)三、装配介绍 (6)1、Spring容器 (6)2、Spring开发要用到的JAR文件 (7)四、基本装配 (7)1、set方式注入 (7)1)注入简单属性(String和8中基本类型) (7)2)注入对象 (9)3)注入集合类型 (9)2、构造器方式装配(constructor注入) (11)3、比较set和构造注入方式 (11)五、复杂装配 (11)1、工厂方式装配 (11)1)静态工厂方式装配 (11)2)实例工厂方式装配 (12)2、自动装配(Autowring Properties) (12)3、bean的定义的继承 (13)4、bean的依赖关系检查 (13)5、scope (14)六、B EAN的生命周期 (14)1、步骤 (14)2、接口介绍 (15)七、事件处理 (16)1、事件监听原理 (16)2、编程步骤 (16)第三章SPRING的AOP (17)一、AOP的概念 (17)1、什么是AOP (17)2、AOP编程的好处 (17)二、AOP相关概念 (17)三、AOP基本原理 (18)四、S PRING的通知(具体的业务逻辑) (18)1、Spring的通知类型 (18)五、切入点(P OINTCUT) (20)1、pointcut (20)接口Pointcut:org.springframework.aop.Pointcut (20)接口Classfilter:org.springframework.aop.ClassFilter (20)接口MethodMather:org.springframework.aop.MethodMatcher (21)接口Advisor:org.springframework.aop.PointcutAdvisor (21)2、预定义切入点 (22)1)静态切入点: (22)2)动态切入点: (24)3)切入点的交叉与合并: (24)4)Introduction(非重点,严重影响性能,慢5到10倍) (24)六、自动代理 (25)1、使用BeanNameAutoProxyCreator进行自动代理 (25)2、DefaultAdvisorAutoProxyCreator (25)第四章SPRING对持久层的支持 (26)一、S PRING对持久层支持采用的策略: (26)二、S PRING对JDBC的支持 (26)Step 1: 配置数据源 (26)方式一:采用Spring内置的数据源(测试用,性能不高) (26)方式二:采用开源数据库产品如DBCP (27)方式三:直接使用容器提供的数据源(如Tomcat,Weblogic,SunAppServer) (27)Step 2: 配置JdbcTemplate模板类(封装了绝大多数数据库操作) (28)Step 3: 配置DAO (28)Step 4: 配置Service (28)三、S PRING对H IBERNATE的支持 (28)Step1:配置数据源 (28)Step2:配置sessionfactory (28)Step3:配置DAO (29)第五章SPRING对事务的支持 (29)一、事务的概念 (29)二、S PRING的事务机制 (29)三、S PRING事务编程 (30)1、整体介绍 (30)2、Spring声明式事务的编程 (30)3、Spring中事务的使用 (32)四、S PRING事务与EJB事务 (33)第六章框架整合 (33)一、SSH (33)二、S PRING与S TRUTS整合 (34)1、前提: (34)2、加载Spring方式: (34)1)采用ContextLoaderListener来创建ApplicationContext: (34)2)采用ContextLoaderPlugIn来创建ApplicationContext (34)3、集成方案 (34)方案一:通过Spring的ActionSupport类 (34)方式二:通过Spring的DelegatingActionProxy类 (35)方式三:通过Spring的DelegatingRequestProcessor类 (35)三、关于S PRING与EJB (36)第一章Spring简介spring in actionspring 技术手册▲精通Spring2.x企业级开发陈雄华一、Spring是什么?1、Spring是一个开源的用于简化企业级开发的分层框架2、分层结构:接口接口表示层业务层持久层域层1)Presentation layer(表示层)(1) 表示逻辑(生成界面代码)(2) 接收请求(3) 处理业务层抛出的异常(4) 负责规则验证(数据格式,数据非空等)(5) 流程控制2)Service layer(服务层/业务层)(1) 封装业务逻辑处理,并且对外暴露接口(2) 负责事务,安全等服务以及数据访问逻辑3)Persistence layer(持久层)提供透明的数据访问和接口(1) 封装数据访问的逻辑,暴露接口(2) 提供方便的数据访问的方案(查询语言,API,映射机制等)4)Domain layer(域层)域层就是一堆实体的集合,包括实体的关系。

Spring总结

Spring总结

Spring总结笔记(轻量级框架)1:Spring概述Spring由7个良好的模块构成:Spring AOP(面向切面编成) ,Spring ORM(对象关系模型,包含了与数据库持久化密切相关的技术如:Hibernate), Spring Web ,Spring Context,Spring DAO,Spring Web MVC,Spring Context(配置文件,为Spring 框架提供上下文文件)和Spring Core.2:Spring的核心思想是:控制反转(Inversion of Control,IoC)和依赖注入(Dependency Injection,DI)控制反转:就是应用本身不负责以来对象的创建及维护,以来对象的创建及维护是由外部容器负责的,这样控制权就由应用转到了外部容器,控制权的转移即为反转。

依赖注入:就是指在运行期间有外部容器动态的依赖对象注入到组件(有本质上它们是同种思想的不同说法)有三种依赖注入方式:1:接口注入2:set注入3:构造注入提示:set注入与构造注入比较类似,如果是在动态的不确定的环境下需要注入一个对象,没有必要调用构造函数而实例化一个对象,所以此时应set注入。

3:Bean与Sring极端的讲Spring=Beans+XML在Spring中的所有组件都会被认为是一个Bean,Bean是容器管理的一个基本单位,bean比一定都是标准的JavaBean,可以是数据源(如Hibernate的SessionFactory)。

Spring通过叫BeanFactory(实现了工厂模式)的容器对bean进行管理。

BeanFactory包含的基本方法:public Object getBean(String name):根据名称返回bean实例public Object getBean(String name,Class requiredType):根据名称和类型返回bean实例4:XML文件中配置Bean<bean id="HelloWorld" class="com.spring.text.HelloWorld"scope="prototype"></bean>id:是BeanFactory中存在的一个标识,可以通过它找到这个Bean的引用。

Spring使用笔记

Spring使用笔记一、spring简介1、什么是springspring是一个高度灵活的轻量级框架,其目的降低程序的耦合度与复杂度,面向接口编程2、spring的作用a、管理对象、维护对象之间的关系(IOC)spring的核心就是IOC容器,用来管理对象(创建、使用、回收),维护对象与对象之间的关系,从而大大降低了对象与对象之间的耦合度,便于团队开发b、处理通用性业务(AOP)spring提供了AOP编程思想,面向切面编程,可以把通用性业务提取出来,切入到任何面向对象的节点上c、整合第三方框架spring提供了一系列第三方框架的整合,可以整合struts、hibernate、mybati 等框架,降低了框架与框架之间的耦合度,降低了框架的使用难度二、IOC1、什么是IOCIOC全称:inversion of control 控制反转是一种编程思想(开发模式),用来管理对象的创建、使用、回收,并且维护对象与对象之间的关系,IOC解决了对象与对象之间解耦,使得我们面向接口编程2、搭建环境a、把spring相关jar包导入到项目中b、把spring配置文件applicationContext.xml放入到项目中的classPath路径c、把log4j.properties配置文件放入到项目中的classPath路径d、创建一个JavaBeane、在applicationContext.xml中,把JavaBean配置到IOC容器中<bean name="自定义名称" class="类的完整路径"></bean>f、编写springAPI获取JavaBean对象,代码如下:ClassPathXmlApplicationContext ac = newClassPathXmlApplicationContext("applicationContext.xml");Person p = (Person)ac.getBean("person");p.setName("李宇春");System.out.println(p.getName());ac.close();3、依赖注入a、什么是DI(依赖注入)DI全称:Dependency Injection 依赖注入对象与对象之间的依赖关系,通过配置文件注入到IOC容器中,依赖注入指的就是对象与对象之间的关系b、注入方式*****set方法构造方法静态工厂实例工厂*****4、spring IOC的工作原理IOC底层实现了一个工厂模式,通过读取applicationContext.xml配置文件中的<bean>标签,把<bean>标签class属性指向的类注入到IOC容器,通过set方法或构造方法进行依赖注入对象与对象之间的关系,产生一个BeanFactory,BeanFactory通过getBean方法获取对象*****5、IOC管理对象的特点a、对象的创建方式在spring配置文件中,<bean>标签通过scope属性定义对象的创建方式,具体如下:scope="singleton" 默认方式,采用单例模式scope="prototype" 采用原型模式,对象是多例的scope="request" 在web中request范围scope="session" 在web中session范围scope="global session" 在web中application范围b、创建对象的时机单例:单例对象是在IOC容器创建时被创建多例:多例对象是对象被使用到时被创建,BeanFactory执行getBean方法被创建c、单例对象延迟加载<bean>标签通过设置lazy-init="true",可以使单例对象第一次使用时被创建,当scope="prototype"时,lazy-init="true"会失效d、对象的初始化方法与销毁方法init-method="方法名" 此方法在创建对象完毕之后被调用destroy-method="方法名" 此方法在对象回收之前被调用三、AOP1、什么是AOPAOP全称:Aspect Oriented Programming(面向切面/方面编程)AOP是一种编程思想(开发模式),是基于OOP基础之上更高级的一种编程思想,把一系列通用性功能提取出来,切入到任何一个OOP的组件上把功能性代码与业务流程代码分离,通过配置文件进行切入,对业务流程代码无侵入性,实现可配置编程2、AOP组成部分a、切面组件(aspect)功能代码,起始就是JavaBeanb、切入点(pointcut)指的是切面组件切入到哪个位置上,这个位置指的是包的路径,比如:控制层、service层、dao层c、通知类型切面组件在切入点之前还是之后切入前置通知:<aop:before>后置通知:<aop:after-returning>环绕通知:<aop:around>最终通知:<aop:after>异常通知:<aop:after-throwing>try{前置通知环绕通知前置部分目标方法//比如:personService.add();环绕通知后置部分后置通知}catch(Exception e) {异常通知}finally {最终通知}。

Spring4.x 学习笔记

Spring 学习笔记1Sring初接触1.1 Spring是什么1.Spring 是一个开源框架.2.Spring 为简化企业级应用开发而生. 使用Spring 可以使简单的JavaBean 实现以前只有EJB 才能实现的功能.3.Spring 是一个IOC(DI) 和AOP 容器框架.1.2 Spring的作用1)用来整合其他框架,可以让程序的结构更为松散,实现低耦合2)简化事务控制,实现声明式事务(相对于编程式事务来言)1.3 具体描述具体描述Spring:轻量级:Spring 是非侵入性的- 基于Spring 开发的应用中的对象可以不依赖于Spring 的API依赖注入(DI --- dependency injection、IOC)面向切面编程(AOP --- aspect oriented programming)容器: Spring 是一个容器, 因为它包含并且管理应用对象的生命周期框架: Spring 实现了使用简单的组件配置组合成一个复杂的应用. 在Spring 中可以使用XML 和Java 注解组合这些对象一站式:在IOC 和AOP 的基础上可以整合各种企业应用的开源框架和优秀的第三方类库(实际上Spring 自身也提供了展现层的SpringMVC 和持久层的Spring JDBC)1.4 Spring模块组成1.5 两个核心思想:1.5.1IOC&DI:IOC控制反转Inversion of Control所谓的IOC:是指将某一类对象的控制权(对象创建和方法调用)交给外部容器来负责。

DI:依赖注入Dependency Inject所谓DI:对象依赖的属性,由容器给此属性赋值的过程1.5.2AOPaop:面向切面编程1.6 第一个spring程序1.6.1添加spring核心依赖库spring运行依赖于spring库,在maven中配置核心依赖如下:<!--添加spring的核心依赖库core--><dependency><groupId>org.springframework</groupId><artifactId>spring-core</artifactId><version>4.2.5.RELEASE</version></dependency><!--添加spring的核心依赖库beans--><dependency><groupId>org.springframework</groupId><artifactId>spring-beans</artifactId><version>4.2.5.RELEASE</version></dependency><!--添加spring的核心依赖库context--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>4.2.5.RELEASE</version></dependency>1.6.2创建spring的全局配置文件applicationContext.xml 在resouces目录下新建spring的配置文件applicationContext.xml1.6.3编写测试类,测试spring环境是否搭建成功若环境成功搭建,数据结果为非空,如下:1.7 IOC控制反转在spring之前,我们程序中需要一个对象实例是直接通过new的方式来创建,或者通过对象工厂的方式来创建,而在spring中,是通过IOC控制反转的方式来创建实例,简单来说就是不用显示的new对象,而是将对象的创建交给spring来实现。

Spring知识点最全笔记

说明笔记中每个知识点均标有如下标识中的一个或多个,释义如下:理解要求:了解:了解本知识点,明确本知识点的作用理解:理解本知识点所涉及内容的工作原理操作要求:操作:根据本知识点所涉及的内容,进行课堂案例的制作应用:根据本知识点所涉及的内容,进行融会贯通,灵活应用知识点相关:重点:本知识点为本课程重点内容难点:本知识点为本课程难点内容,理解难度较高(难点不一定是重点)目录spring简介【了解】 (3)资源包整理【了解】【应用】 (4)IoC【理解】【应用】【重点】 (5)DI【理解】【应用】【重点】 (7)BeanFactory【了解】 (8)Bean配置(XML)【理解】【应用】【重点】 (9)团队开发【了解】【操作】 (13)Bean配置(注解)【了解】【操作】 (14)Spring整合JUnit【了解】【操作】 (16)AOP简介【理解】【重点】 (17)AOP简介【理解】【重点】 (17)AOP基本概念【理解】【重点】 (18)AOP(XML)【理解】【应用】【重点】 (19)AOP(注解)【理解】【应用】【重点】 (24)CGLIB【理解】【难点】 (25)DAO模板类【了解】 (26)DAO支持抽象类【理解】【操作】 (29)事务管理基本概念【了解】 (31)声明式事务(XML)【理解】【重点】 (34)声明式事务(注解)【理解】【应用】【重点】 (36)SSH整合(XML)【理解】【应用】【重点】 (37)Hibernate模板类【理解】【应用】【重点】 (42)spring简介【了解】1.Spring是一个基于Java的轻量级的J2EE框架基于Java: 底层Java实现轻量级:性能较高,运行速度较快,内存消耗较少重量级组件:EJB、CGIJ2EE应用框架:Spring可以在JEE开发的三层架构的每一层为开发者带来帮助表现层:Servlet/JSP,Struts2,SpringMVC业务层:Bean管理、AOP、事务管理数据层:JDBC,Hibernate,Spring JDBCTemplate域模型层:实体类+DAO服务层:远端调用(webservice)[类似技术RMI+JNDI]Spring提供的每层的服务技术表现层:String MVC业务逻辑层:Bean管理、AOP、事务管理数据层:DAO支持抽象类,DAO模板技术JDBCTemplate2. Spring的发展历程创始人:Rod JohnsonExpert One-to-One J2EE Design and Development(2002)阐述了J2EE使用EJB开发设计的优点及解决方案Expert One-to-One J2EE Development without EJB(2004)阐述了J2EE开发过程中不使用EJB的解决方式(Spring雏形)3.Spring核心技术IOC:控制反转AOP:面向切面编程/面向方面编程4.Spring是一个超级的“黏合平台”,将很多技术黏合在一起,形成一个整体,使每个组件发挥其最大功效资源包整理【了解】【应用】1.本课程基于Spring3.2.0进行讲解2.下载地址:/release/org/springframework/spring/说明:通过Maven部署资源从仓库获取(后期课程)3.资源文件Spring资源包下spring-framework-3.2.0.RELEASE-dist.zipSpring依赖资源包spring-framework-3.0.2.RELEASE-dependencies.zip4.资源包目录层次结构docs:API帮助文档与官方手册libs:开发使用的jar包schema:开发使用的XML约束文档源文件5.开发jar包核心jar(4个)spring-beans-3.2.0.RELEASE.jarspring-context-3.2.0.RELEASE.jarspring-core-3.2.0.RELEASE.jarspring-expression-3.2.0.RELEASE.jar日志相关jar(2个)commons-logging-1.1.1.jarcommons-logging日志整合,与slf4j类似,由apache提供log4j-1.2.15.jarIoC【理解】【应用】【重点】1.IoC(Inversion of Control)控制反转A:控制:控制的双方是什么?谁控制谁?主控方:Spring被控方:应用程序所使用的资源(原始)应用程序控制自己执行操作需要使用的外部资源(Spring)Spring控制整个程序中所需要使用的外部资源B.反转:什么是反转?反转什么?正向:应用程序直接调用资源反向:应用程序依赖Spring为其提供资源反转的是资源的控制权应用程序由主动调用资源,变为被动的等待Spring提供资源C.有什么好处?正向缺点:应用程序控制的资源分布在程序的每一个地方反向的优点:所有的资源都在Spring中,便于管理总结:Spring反向控制应用程序所需要使用的外部资源。

Spring学习笔记

Spring串讲一、Spring的基本内容1.什么是Spring:是一个用来简化企业级程序开发的分层的框架。

它旨在分离体系结构的层次,因此每一层都可以修改而不会影响到其它层。

层与层之间的依赖通常是以接口的形式表现,以确保其耦合尽可能松散。

2.Spring的组成(7个模块):(1)SpringCore:Spring的核心容器,主要提供了组件的创建、装配、销毁的基本功能。

(2)SpringContext:Spring上下文,基于Spring核心容器,扩展了核心容器,主要提供了事件处理、国际化等功能。

(3)SpringAOP:提供了AOP编程的支持。

(4)SpringDAO:提供了JDBC的支持、一种实现编程性和声明性的事务管理方法。

(5)SpringORM:提供了对O/R mapping的支持,对Hibernate、JDBC等的再次封装。

(6)SpringWeb:基于Spring上下文的,提供了webApplication的容器,方便web的集成。

(7)SpringWebMVC:提供了MVC的一个完整的实现。

二、Spring的核心内容1.IOC (Inversion of control): 控制反转/依赖注入(1)IOC的概念:控制反转/依赖注入,组件之间的依赖关系由容器在运行时决定1)组件:JavaBean2)依赖关系:调用/包含3)容器:Spring4)控制权由对象本身转向容器;由容器根据配置文件去创建实例并创建各个实例之间的依赖关系(2)IOC的优点:1)代码的耦合度低2)代码的维护性好3)代码量少(3)IOC的核心--- BeanFactory1)BeanFactory是所有容器的父接口,它提供了基本的对象的装配的支持2)XmlBeanFactory是他最常用的实现之一。

3)在Spring中,BeanFactory创建的各个实例称作Bean4)BeanFactory只有在客户端调用时才实例化对象,即调用getBean()时容器才会创建对应的组件的实例,在默认情况下返回的对象采用单例模式。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

struts:web层,比较简单(ValueStack值栈,拦截器)hibernate:dao层,知识点杂spring:service层,重要,讲多少用多少--> 【了解】spring day01:基础(IoC控制反转、DI依赖注入)、整合Junit、整合webspring day02:AOP切面编程、JdbcTemplatespring day03:事务管理、SSH整合1spring框架概述1.1 什么是spring●Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。

它是为了解决企业应用开发的复杂性而创建的。

框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为J2EE 应用程序开发提供集成的框架。

Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。

然而,Spring的用途不仅限于服务器端的开发。

从简单性、可测试性和松耦合的角度而言,任何Java 应用都可以从Spring中受益。

Spring的核心是控制反转(IoC)和面向切面(AOP)。

简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式)轻量级开源框架。

●轻量级:与EJB对比,依赖资源少,销毁的资源少。

●分层:一站式,每一个层都提供的解决方案web层:struts,spring-MVCservice层:springdao层:hibernate,mybatis ,jdbcTemplate --> spring-data1.2 spring由来●Expert One-to-One J2EE Design and Development●Expert One-to-One J2EE Development without EJB1.3 spring核心●Spring的核心是控制反转(IoC)和面向切面(AOP)1.4 spring优点1.5 spring体系结构核心容器:beans、core、context、expression2入门案例:IoC【掌握】2.1 导入jar包● 4 + 1 :4个核心(beans、core、context、expression)+ 1个依赖(commons-loggins...jar)2.2 目标类●提供UserService接口和实现类●获得UserService实现类的实例之前开发中,直接new一个对象即可。

学习spring之后,将由Spring创建对象实例--> IoC 控制反转(Inverse of Control)2.3 配置文件●位置:任意,开发中一般在classpath下(src)●名称:任意,开发中常用applicationContext.xml●内容:添加schema约束2.4 测试3入门案例:DI【掌握】●DI Dependency Injection ,依赖注入is a :是一个,继承。

has a:有一个,成员变量,依赖。

class B {private A a; //B类依赖A类}依赖:一个对象需要使用另一个对象注入:通过setter方法进行另一个对象实例设置。

●例如:class BookServiceImpl{//之前开发:接口= 实现类(service和dao耦合)//private BookDao bookDao = new BookDaoImpl();//spring之后(解耦:service实现类使用dao接口,不知道具体的实现类)private BookDao bookDao;setter方法}模拟spring执行过程创建service实例:BookService bookService = new BookServiceImpl() -->IoC <bean> 创建dao实例:BookDao bookDao = new BookDaoImple() -->IoC将dao设置给service:bookService.setBookDao(bookDao); -->DI <property>3.1 目标类●创建BookService接口和实现类●创建BookDao接口和实现类●将dao和service配置xml文件●使用api测试3.1.1dao3.1.2service3.2 配置文件3.3 测试4myeclipse schema xml提示●步骤一:确定xsd文件位置spring-framework-3.2.0.RELEASE\schema\beans●步骤二:复制路径●步骤三:搜索“xml catalog”●步骤四:添加约束提示5核心API●api整体了解,之后不使用,在学习过程需要。

●BeanFactory :这是一个工厂,用于生成任意bean。

采取延迟加载,第一次getBean时才会初始化Bean●ApplicationContext:是BeanFactory的子接口,功能更强大。

(国际化处理、事件传递、Bean自动装配、各种不同应用层的Context实现)。

当配置文件被加载,就进行对象实例化。

ClassPathXmlApplicationContext 用于加载classpath(类路径、src)下的xml加载xml运行时位置--> /WEB-INF/classes/...xmlFileSystemXmlApplicationContext用于加载指定盘符下的xml加载xml运行时位置--> /WEB-INF/...xml通过java web ServletContext.getRealPath() 获得具体盘符6装配Bean 基于XML6.1 实例化方式●3种bean实例化方式:默认构造、静态工厂、实例工厂●6.1.1默认构造6.1.2静态工厂●常用与spring整合其他框架(工具)6.1.2.1 工厂6.1.2.2 s pring配置6.1.3实例工厂实例工厂:必须先有工厂实例对象,通过实例对象创建对象。

提供所有的方法都是“非静态”的。

6.1.3.1 工厂6.1.3.2 s pring配置6.2 Bean种类●普通bean:之前操作的都是普通bean。

<bean id="" class="A"> ,spring直接创建A实例,并返回●FactoryBean:是一个特殊的bean,具有工厂生成对象能力,只能生成特定的对象。

bean必须使用FactoryBean接口,此接口提供方法getObject() 用于获得特定bean。

<bean id="" class="FB"> 先创建FB实例,使用调用getObject()方法,并返回方法的返回值FB fb = new FB();return fb.getObject();●BeanFactory 和FactoryBean 对比?BeanFactory:工厂,用于生成任意bean。

FactoryBean:特殊bean,用于生成另一个特定的bean。

例如:ProxyFactoryBean ,此工厂bean用于生产代理。

<bean id="" class="....ProxyFactoryBean"> 获得代理对象实例。

AOP使用6.3 作用域●作用域:用于确定spring创建bean实例个数●取值:singleton 单例,默认值。

prototype 多例,每执行一次getBean将获得一个实例。

例如:struts整合spring,配置action多例。

6.4 生命周期6.4.1初始化和销毁6.4.1.1 目标类6.4.1.2 s pring配置6.4.1.3 测试6.4.2BeanPostProcessor 后处理Bean●spring 提供一种机制,只要实现此接口BeanPostProcessor,并将实现类提供给spring容器,spring容器将自动执行,在初始化方法前执行before(),在初始化方法后执行after() 。

配置<bean class="">●Factory hook(勾子) that allows for custom modification of new bean instances, e.g. checking for marker interfacesor wrapping them with proxies.●spring提供工厂勾子,用于修改实例对象,可以生成代理对象,是AOP底层。

模拟A a =new A();a = B.before(a) --> 将a的实例对象传递给后处理bean,可以生成代理对象并返回。

a.init();a = B.after(a);a.addUser(); //生成代理对象,目的在目标方法前后执行(例如:开启事务、提交事务)a.destroy()6.4.2.1 编写实现类6.4.2.2 配置●问题1:后处理bean作用某一个目标类,还是所有目标类?所有●问题2:如何只作用一个?通过“参数2”beanName进行控制6.5 属性依赖注入●依赖注入方式:手动装配和自动装配●手动装配:一般进行配置信息都采用手动基于xml装配:构造方法、setter方法基于注解装配:●自动装配:struts和spring 整合可以自动装配byType:按类型装配byName:按名称装配constructor构造装配,auto:不确定装配。

6.5.1.1 目标类6.5.1.2 s pring配置6.5.3P命令空间[了解]●对“setter方法注入”进行简化,替换<property name="属性名">,而是在<bean p:属性名="普通值" p:属性名-ref="引用值">●p命名空间使用前提,必须添加命名空间6.5.4SpEL[了解]●对<property>进行统一编程,所有的内容都使用value<property name="" value="#{表达式}">#{123}、#{'jack'} :数字、字符串#{beanId} :另一个bean引用#{beanId.propName} :操作数据#{beanId.toString()} :执行方法#{T(类).字段|方法} :静态方法或字段●阅读:6.5.5集合注入7装配Bean 基于注解●注解:就是一个类,使用@注解名称●开发中:使用注解取代xml配置文件。

相关文档
最新文档