spring学习笔记_第1页
spring学习笔记_第2页
spring学习笔记_第3页
spring学习笔记_第4页
spring学习笔记_第5页
已阅读5页,还剩8页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Spring 主要的接口和类主要的接口和类 继承关系继承关系 关联关系关联关系 实现关系实现关系 org springframework beans factoryorg springframework beans factory InterfaceInterface BeanFactoryBeanFactory 在 Spring 中 BeanFactory是 IoC 容器的核心接口 它的职责包括 实例化 定位 配置应用程序中的对象及建立这些对象间的依赖 org springframework contextorg springframework context InterfaceInterface ApplicationContextApplicationContext 描述描述 ApplicationContextApplicationContext 继承了继承了 BeanFactoryBeanFactory 并提供了下面的功能并提供了下面的功能 通过该接口的实现类来访问我们在容器中注册的通过该接口的实现类来访问我们在容器中注册的 beanbean MessageSource 提供国际化的消息访问 资源访问 如 URL 和文件 事件传播 实现了ApplicationListener接口的 bean org springframework core ioorg springframework core io InterfaceInterface ResourceResource 提供更强的访问底层资源能力的抽象 我们能够通过 该接口的实现类读取资源文件 实现类实现类 AbstractResource ByteArrayResource ClassPathResource DescriptiveResource FileSystemResource InputStreamResource PortletContextResource ServletContextResource UrlResource UrlResourceUrlResource UrlResource 封装了 URL 它能够被用来访 问任何通过 URL 可以获得的对象 例如 文件 HTTP 对象 FTP 对象等 所有的 URL 都有个标准的 String 表示 这些标准前缀可以标识不同的 URL 类型 包括 file 访问文件系统路径 http 通过 HTTP 协议访问 的资源 ftp 通过 FTP 访问的资源等等 UrlResource 对象可以在 Java 代码中显式地使用 UrlResource 构造函数来创建 但更多的是通过调用 带表示路径的 String 参数的 API 函数隐式地创建 在后一种情况下 JavaBeans 的 PropertyEditor 会 最终决定哪种类型的 Resource 被创建 如果这个字 符串包含一些众所周知的前缀 比如 classpath 它就会创建一个对应的已串行化的 Resource 然而 如果不能分辨出这个前缀 就会假定它是个标准的 URL 字符串 然后创建 UrlResource ClassPathResourceClassPathResource 这个类标识从 classpath 获得的资源 它会使用线程 context 的类加载器 class loader 给定的类加载 器或者用来载入资源的给定类 如果类路径上的资源存在于文件系统里 这个 Resource 的实现会提供类似于 java io File 的功能 而如果资源是存在于还未解开 被 servlet 引擎或其 它的环境解开 的 jar 包中 这些 Resource 实现会 提供类似于 URL 的功能 ClassPathResource 对象可以在 Java 代码中显式地使 用 ClassPathResource 构造函数来创建 但更多的是 通过调用带表示路径的 String 参数的 API 函数隐式 地创建 在后一种情况下 JavaBeans 的 PropertyEditor 会分辨字符串中 classpath 前缀 然后相应创建 ClassPathResource FileSystemResourceFileSystemResource 这是为处理 java io File 而准备的 Resource 实现 它既可以作为 File 提供 也可以作为 URL ServletContextResourceServletContextResource 这是为 ServletContext 资源提供的 Resource 实现 它负责解析相关 web 应用根目录中的相对路径 它始终支持以流和 URL 的方式访问 但是只有当 web 应用包被解开并且资源在文件系统的物理路径上时 才允许以 java io File 方式访问 是否解开并且在 文件系统中访问 还是直接从 JAR 包访问或以其它方 式访问如 DB 这是可以想象的 仅取决于 Servlet 容 器 InputStreamResourceInputStreamResource 这是为给定的 InputStream 而准备的 Resource 实 现 它只有在没有其它合适的 Resource 实现时才使 用 而且 只要有可能就尽量使用 ByteArrayResource 或者其它基于文件的 Resource 实现 与其它 Resource 实现不同的是 这是个 已经 打开 资源的描述符 因此 isOpen 函数返回 true 如 果你需要在其它位置保持这个资源的描述符或者多次 读取一个流 请不要使用它 ByteArrayResourceByteArrayResource 这是为给定的 byte 数组准备的 Resource 实现 它 会为给定的 byte 数组构造一个 ByteArrayInputStream 它在从任何给定的 byte 数组读取内容时很有用 因 为不用转换成单一作用的 InputStreamResource org springframework core ioorg springframework core io InterfaceInterface ResourceLoaderResourceLoader 提供了调用 Resource 接口的能力 public interface ResourceLoader Resource getResource String location 前缀前缀例子例子说明说明 classpath classpath com myapp config xml 从 classpath 中加载 file file data config xml 作为 URL 从文件系统中 加载 a http http myserver logo png 作为 URL 加载 none data config xml 根据 ApplicationContext 进行判断 org springframework beans factory configorg springframework beans factory config InterfaceInterface BeanPostProcessorBeanPostProcessor 当我们象容器请求我们的 bean 的时候 实现了该接口就可以拦 截我们所有的 bean 给我们个处理的机会 也是我们的扩展点 我们自己实现该接口做所有 bean 调用过程中的拦截器 AOP AOPAOP 应用范围应用范围 Authentication 权限 Caching 缓存 Context passing 内容传递 Error handling 错误处理 Lazy loading 懒加载 Debugging 调试 logging tracing profiling and monitoring 记录跟踪 优化 校准 Performance optimization 性能优化 Persistence 持久化 Resource pooling 资源池 Synchronization 同步 Transactions 事务 使用的使用的 AOP 好处好处 使用 AOP 可以让我们从一个更高的抽象概念来理解软件系统 使得系统层次清晰 代理模式和 java 动态代理的列子 AOP 术语术语 AOP 代理 代理 AOP Proxy 代理对象代理对象 org springframework aop frameworkorg springframework aop framework ClassClass ProxyFactoryBeanProxyFactoryBean 连接点 连接点 Joinpoint 在程序执行过程中某个特定的点在程序执行过程中某个特定的点 比如方比如方 法执行前法执行前 执行后执行后 通知 通知 Advice 就是你要在目标对象调用前 就是你要在目标对象调用前 给他的额外功能给他的额外功能 spring spring 中的中的 AdiveAdive 就是连接点就是连接点 和和 AdviceAdvice 的结合的结合 前置通知前置通知 org springframework aoporg springframework aop MethodBeforeAdvice MethodBeforeAdvice 注意如果执行过程中抛出异常该 advice 不能捕获异常 异常将专递出去 后置通知后置通知 org springframework aop AfterReturningAdviceorg springframework aop AfterReturningAdvice 注意如果执行过程中抛出异常该 advice 不能捕获异常 异常将专递出去 环绕通知环绕通知 org aopalliance intercept MethodInterceptororg aopalliance intercept MethodInterceptor 兼容 AOP 联盟接口 请尽量使用该 adive 因为异常能够在 该 advic 中处理 异常通知异常通知 org springframework aop ThrowsAdviceorg springframework aop ThrowsAdvice 异常处理的拦截器 目标对象 目标对象 Target Object 需要给代理的对象需要给代理的对象 在在 ProxyFactoryBeanProxyFactoryBean 里里 面配置面配置 切入点 切入点 Pointcut 在在 springspring 中中 advisoradvisor 实现了实现了 Pointcut Pointcut 理解理解 sping 的的 advisor 在在 ProxyFactoryBeanProxyFactoryBean 里面配置里面配置 ProxyFactoryBeanProxyFactoryBean 作用负责产生代理的类作用负责产生代理的类 org springframework aop framework Class ProxyFactoryBean 最主要配置的方法 void setInterceptorNamessetInterceptorNames String interceptorNames Set the list of Advice Advisor bean names void setProxyInterfacessetProxyInterfaces Class proxyInterfaces Set the names of the interfaces we re proxying void setTargetNamesetTargetName String targetName Set the name of the target bean void setProxyTargetClasssetProxyTargetClass boolean proxyTargetClass Set whether to proxy the target class directly instead of just proxying specific interfaces 如果没有接口如果没有接口 使用使用 CGLIB 基于继承扩展的代理基于继承扩展的代理 注意注意 springspring 通常使用接口来完成代理的通常使用接口来完成代理的 就是开个接口 主体类实现接口 如果被代理的类没有接口该怎么办那如果被代理的类没有接口该怎么办那 在这种情况下 你可以配置在这种情况下 你可以配置SpringSpring使使 用用CGLIBCGLIB代理代理 aop HelloInterface 把原来的把原来的proxyInterfacesproxyInterfaces 换为换为proxyTargetClassproxyTargetClass true Advice 作用你的拦截目的作用你的拦截目的 前置通知前置通知 org springframework aoporg springframework aop MethodBeforeAdvice MethodBeforeAdvice 注意如果执行过程中抛出异常该 advice 不能捕获 异常 异常将专递出去 后置通知后置通知 org springframework aop AfterReturningAdvicorg springframework aop AfterReturningAdvic e e 注意如果执行过程中抛出异常该 advice 不能捕获 异常 异常将专递出去 环绕通知环绕通知 org aopalliance intercept MethodInterceptororg aopalliance intercept MethodInterceptor 兼容 AOP 联盟接口 请尽量使用该 adive 因为异常 能够在 该 advic 中处理 异常通知异常通知 org springframework aop org springframework aop T ThrowsAdvicehrowsAdvice 异常处理的拦截器 PointcutPointcut 作用为了细力度的控制作用为了细力度的控制 拦截拦截 代码执行的部位代码执行的部位 比如在那个类的那个比如在那个类的那个 方法执行方法执行 org springframework aoporg springframework aop InterfaceInterface PointcutPointcut All Known Implementing Classes AbstractExpressionPointcut AbstractRegexpMethodPointcut AnnotationMatchingPointcut AspectJExpressionPointcut ComposablePointcut ControlFlowPointcut DynamicMethodMatcherPointcut DynamicMethodMatcherPointcutAdvisor JdkRegexpMethodPointcut NameMatchMethodPointcut Perl5RegexpMethodPointcut StaticMethodMatcherPointcut StaticMethodMatcherPointcutAdvisor 创建自定义的创建自定义的 pointcut doc AdvisorAdvisor 为了方便的使用为了方便的使用 切入点切入点 和和 adivceadivce 的组合的组合 spingsping 提供了提供了 advisoradvisor 概念概念 注意注意 每个每个 advisoradvisor 对应对应 1 1 个切入点和个切入点和 1 1 个个 adviceadvice org springframework aoporg springframework aop InterfaceInterface PointcutAdvisorPointcutAdvisor 在 spring 中 spring 实现的 piontuct 都实现了对应的 PointcutAdvisor 所以在使用的使 用你就不要显示的设置 piontuct 了 All Known Implementing Classes AbstractBeanFactoryPointcutAdvisor AbstractGenericPointcutAdvisor AbstractPointcutAdvisor AspectJExpressionPointcutAdvisor AspectJPointcutAdvisor DynamicMethodMatcherPointcutAdvisor NameMatchMethodPointcutAdvisor PersistenceExceptionTranslationAdvisor ReflectiveAspectJAdvisorFactory Syntheti cInstantiationAdvisor RegexpMethodPointcutAdvisor StaticMethodMatcherPointcutAdvisor TransactionAttributeSourceAdvisor 注意有 2 个 Advisor 是特别的 是 给你自己提供的 Pointcut 来装配 的 DefaultBeanFactoryPointcutAdvisor DefaultPointcutAdvisor Spring 整合整合 Hibernate 1 在在 spring 的的 xml 文件中配置文件中配置 org springframework orm hibernate3 LocalSessionFactoryBean 最主要配置的方法 voidsetDataSource DataSourceDataSource dataSource dataSource 设置数据源设置数据源 就是数据库驱动的连接就是数据库驱动的连接 DataSourceDataSource 通过通过 mons dbcp BasicDataSource 类来设置进去类来设置进去 voidsetHibernateProperties PropertiesProperties hibernateProperties hibernateProperties 设置 hibernate 的属性 比如 方言 show sql 连接池配置 voidsetMappingResources StringString mappingResources mappingResources Set Hibernate mapping resources to be found in the class path like example hbm xml or mypackage example hbm xml 作用该类通过编程方式封装了 org hibernate cfg Configuration 那么我们就可以通过 spring 的 xml 配置文件 来配置 hibernate 的配置了 2 有了有了 LocalSessionFactoryBean 对象我们就可以启动对象我们就可以启动 org springframework orm hibernate3 support HibernateDaoSupport 来得到来得到 DAO 的支持了的支持了 作用使用 spring 提供的 DAO 支持 简化数据操作 提供一制的数据库异常 把针对于数据库的操作 封装成为了 RuntimeException 异常 我们可以象在 hibernate 中使用我们熟悉的 seesion 对象 或者使用简化 session 操作的 org springframework orm hibernate3 HibernateTemplate 模版对象 在在 SpringSpring 中使用事务的支持中使用事务的支持 3 在在 xml 文件中配置事务管理器文件中配置事务管理器 spring 中的事务管理器中的事务管理器 比如你用 jdbc 事务的就用DataSourceTransactionManager 比如你用 hibernae 的就选择HibernateTransactionManager org springframework orm hibernate3 HibernateTransactionManager 最主要配置 voidsetSessionFactorysetSessionFactory SessionFactory sessionFactory Set the SessionFactory that this instance should manage transactions for 1 4 在在 xml 文件中配置事务代理类文件中配置事务代理类 org springframework transaction interceptor TransactionProxyFactoryBean 最主要配置 voidsetTransactionManagersetTransactionManager PlatformTransactionManager transactionManager Set the transaction manager voidsetTransactionAttributessetTransactionAttributes Properties transactionAttributes Set properties with method names as keys and transaction attribute descriptors parsed via TransactionAttributeEditor as values e g key myMethod value PROPAGATION REQUIRED readOnly TransactionDefinition 事务属性定义事务属性定义 Isolation 主要的作用是定义事务的隔离级别主要的作用是定义事务的隔离级别 int ISOLATION DEFAULTISOLATION DEFAULT 使用底层数据库预设定的隔离层次 static int ISOLATION READ COMMITTEDISOLATION READ COMMITTED 允许事务读取其他的并行的事务已经送出的 commit 的数据字段 能够防止脏读 static int ISOLATION READ UNCOMMITTEDISOLATION READ UNCOMMITTED 允许事务读取其他并行的事务还没送出的数据允许事务读取其他并行的事务还没送

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论