• Spring Recipes

    1. 1.控制反转和容器

      1. 依赖注入的概念

      2. IoC 和 DI 的区别

        IoC 是一个通用的设计原则, DI 是具体的设计模式. DI 是 IoC 最典型的实现(但并不是唯一), 所以有时 IoC 和 DI 经常混用.

      3. 建立一个简单的 IoC 容器.

    2. 2.Spring 简介

    3. 3.Spring 中的 Bean 配置

      1. XML

        1. id 和 name 的区别

        2. 用 type 和 index 解决构造器岐义

        3. dependency-check 依赖检查

          none(默认) 不做任何的检查,所有属性都可以不设置 simple 如果简单类型(原始类型和集合类型)的属性未被设置,将抛 UnsatisfiedDependencyException objects 如果对象类型(除去原始类型和集合类型)的属性未被设置,将抛 UnsatisfiedDependencyException all 如果任意类型未被设置,将抛出 UnsatisfiedDependencyException

        4. autowire 自动装配

          no* 不执行自动装配,必须手动的装配依赖 byName 为每个 Bean 属性装配与其同名的 Bean byType 为每个 Bean 属性装配与其同类型的 Bean. 如果找到多个与属性类型一致的 Bean, 将抛出 UnsatisfiedDependecyException constructor 针对每个构造器的每个参数,首先找出类型与构造器参数一致的 Bean , 然后再找出具有最匹配参数的构造器.如果产生任何的岐义, 将抛出 UnsatisfiedDependecyException autodetect 如果能够找到默认没有参数的构造器,那么依赖将采用 byType 模式自动装配,否则,将采用 constructor 模式

        5. 继承 Bean 配置

        6. 为 Bean 属性定义集合

          1. 简单的通过内置的

          2. 使用工厂 Bean 定义集合

            ListFactoryBean SetFactoryBean MapFactoryBean

          3. 使用 Utility Schema 定义集合

      2. Annotation

        1. @Required 依赖检查,

          须注册 RequiredAnnotationBeanPostProcessor 或者在 bean 配置里包括

        2. @Autowired @Resource 自动装配 Bean

          要先在 IoC 容器里注册 AutowiredAnnotationBeanPostProcessor 或者在 bean 配置文件里包括 @Qualifier 指定 Bean 的名称

        3. 在 classpath 中扫描组件

          1. @Component @Repository @Service @Controller

          2. 过滤扫描的组件

          3. 命名被侦测到的组件

          1. AutowiredAnnotationBeanPostProcessor

          2. CommonAnnotationBeanPostProcessor

          3. PersistenceAnnotationBeanPostProcessor

          4. RequiredAnnotationBeanPostProcessor

    4. 4.高级 Spring IoC 容器

      1. 工厂方法创建 Bean

        1. 静态工厂方法

        2. 实例工厂方法

          1. 使用 传递方法参数

        3. 使用 Spring 的工厂 Bean 创建 Bean

          1. 当需要从 JNDI 查找对象(例如数据源)时,可以使用 JndiObjectFactoryBean

          2. 当使用经典的 Spring AOP 为 Bean 创建代理时,可以使用 ProxyFactoryBean

          3. 当在 IoC 容器里创建 Hibernate Session Factory 时,可以使用 LocalSessionFactoryBean

      2. 根据静态字段声明 Bean

        1. org.springframework.beans.factory.config.FieldRetrievingFactoryBean

      3. 根据对象属性声明 Bean

        1. org.springframework.beans.factory.config.PropertyPathFactoryBean

      4. 设置 Bean 的作用域

        1. singleton prototype request session globalSession

      5. 定制 Bean 的初始化和销毁过程

        1. 实现 InitializingBean 和 DisposableBean

        2. 设置 init-method 和 destroy-method

        3. 应用 @PostConstruct 和 @PreDestroy 注解

          CommonAnnotationBeanPostProcessor

      6. 使 Bean 感知容器

        1. BeanNameAware BeanFactoryAware ApplicationContextAware MessageSourceAware ApplicationEventPublisherAware ResourceLoaderAware

        1. 就是让 Bean 感知(使用)容器的资源 Spring 将通过定义在这些接口里的 setter 方法给 Bean 注入相应的资源

      7. 创建 Bean 后置处理器

        1. BeanPostProcessor

          1. Bean 的优先级问题

      8. 外部化 Bean 配置

        1. PropertyPlaceholderConfigurer

      9. 解析文本信息 I18N

        1. ResourceBundleMessageSource

      10. 使用应用程序事件进行通信

        1. ApplicationEvent

        2. ApplicationEventPublisherAware

        3. ApplicationListener

        1. 一旦有事件发生, Spring 将通知所有的事件监听器,所以,必须自己对事件进行过滤

      11. 在 Spring 里注册属性编辑器

      12. 加载外部资源

        1. ResourceLoader

    5. 5-6.Spring AOP

      1. AOP 原理

        企业级应用程序中典型的横切关注点包括了日志,验证,连接池,缓存,身份验证和事务.

        1. CGLIB 代理

        2. JDK 动态代理

          1. InvocationHandler

      2. 主流 AOP 框架

        1. AspectJ

        2. JBoss AOP

        3. Spring AOP

      3. Spring 1.x 经典 AOP

        通过一组 Spring 专有的 AOP API 来使用 AOP 在正式的 AOP 定义里, 存在多种类型的执行点,包括方法执行,构造器执行和字段访问. 在 Spring AOP 中,只支持方法执行.

        1. 通知 Advice

          通过一组 Spring 专有的 AOP API 来使用 AOP 在正式的 AOP 定义里, 存在多种类型的执行点,包括方法执行,构造器执行和字段访问. 在 Spring AOP 中,只支持方法执行.

          1. before advice

          2. after returning advice

          3. after throwing advice

          4. around advice

          1. 由 ProxyFactoryBean 创建代理

        2. 切入点 pointcut

          1. 方法名称切入点

            1. NameMatchMethodPointcut

          2. 正则表达式切入点

            1. JdkRegexpMethodPointcut

          3. AspectJ 表达式切入点

            1. AspectJExpressionPointcut

        3. 增强器 Advisor

          1. NameMatchMethodPointcutAdvisor

          2. RegexpMethodPointcutAdvisor

          3. AspectJExpressionPointcutAdvisor

        4. 为 Bean 自动创建代理

          1. BeanNameAutoProxyCreator

          2. DefaultAdvisorAutoProxyCreator

            这个自动代理创建器不需要任何配置.它会自动检查 IoC 容器里声明的每一个增强器和 Bean. 如果存在与增强器切入点匹配的 Bean , 那么 DefaultAdvisorAutoProxyCreator 将自动的为其创建代理.

        1. Advisor 用于将切入点和通知关联起来

        2. 匹配一个类中的某几个方法

        3. 匹配几个类中的几个方法

      4. Spring 2.x AOP

        或者通过 Bean 配置文件里基于 XML 的配置来使用 AOP 的, 通过编写运用 AspectJ 注解的 POJO

        1. AspectJ 注解

          要在 Spring IoC 容器里启用 AspectJ 注解支持,只需在 Bean 配置文件中定义一个空的 XML 的元素 即可. 然后 Spring 会自动为与 AspectJ 切面匹配的 Bean 创建代理.

          1. @Aspect

            使用 @Aspect 注解后,仍需在容器里注册这个 Bean 或添加注解 @Component

            1. @Before

            2. @After

              在 @AfterReturning @AfterThrowing 之后运行,和 java 的 finally 一样.

            3. @AfterReturning

            4. @AfterThrowing

            5. @Around

          2. 连接点 JoinPoint

            连接点是与切入点匹配的执行点 切入点是与一组连接点匹配的表达式 通知是莫个特定的连接点所采取的动作

            1. 连接点 JoinPoint, 切入点 Pointcut, 通知 Advice 的区别

          3. 指定切面优先级

            1. 实现 Ordered 接口

            2. @Order(int)

          4. 重用切入点定义

            1. @Pointcut

              将注解加在一个空的方法上,用这个方法名代替切入点 方法的可见性控制切入点的可见性

          5. 编写 AspectJ 切入点表达式

            1. 方法签名模式 execution(* *.*(..))

            2. 类型签名模式 within(*.*) 或 within(*.*+)

            3. Bean 名称模式 bean(*)

            4. 合并切入点表达式 && || !

            5. 声明切入点参数 &&target(target) &&args(arg)

          6. 引入 Introduction

            1. 为 Bean 引入行为

              类似与 python 的 mixin

              1. @DeclareParants(value = "引入的目标" defaultImpl = "新接口使用的实现类")

            2. 为 Bean 引入状态

        2. 基于 XML 配置

          1. 声明切面

          2. 声明切入点

          3. 声明通知

          4. 声明引入

        3. 整合 AspectJ

        4. 将 Spring Bean 注入到领域对象中

    6. 7.Spring 对 JDBC 的支持

      1. 在 Spring 中配置数据源

        1. DriverManagerDateSource

        2. 第三方数据源实现, 如 DBCP

      2. 用 JDBC 模板更新数据库

        1. JDBCTemplate.update(..)

        2. JDBCTemplate.batchUpdate(..)

      3. 用 JDBC 模板查询数据库

        1. 用 RowCallbackHandler 提取数据

        2. 用 RowMapper 提取数据

        3. 查询多行

        4. 查询单值

      4. 简化 JDBC 模板的创建

        JDBCTemplate 是线程安全

        1. 注入 JDBCTemplate

        2. 扩展 JdbcDaoSupport

      5. 在 java 1.5 里使用简单的 JDBC 模板

        1. SimpleJdbcTemplate

        2. SimpleJdbcDaoSupport

        3. ParameterizedRowMapper

      6. 在 JDBC 模板中使用具名参数

      7. 将 JDBC 操作建模为细粒度的对象

      8. 在 Spring JDBC 框架中处理异常

    7. 8.事务管理

      1. 事务的概念 (ACID)

        1. 原子性 atomicity

        2. 一致性 consistency

        3. 隔离性 isolation

        4. 持久性 durability

      2. 编程式的管理事务

        1. 用事务管理器 API 编程式地管理事务

        2. 用事务模板编程式地管理事务

      3. 声明式地管理事务

        1. 用经典的 Spring AOP

          1. TransactionInterceptor

          2. TransactionProxyFactoryBean

        2. 用事务通知

        3. 用 @Transactional 注解

          1. @Transactional

      4. 设置事务传播属性

        事务传播发生在事务方法被另一个方法调用的时候.

        1. Propagation

          MANDATORY Support a current transaction, throw an exception if none exists. NESTED Execute within a nested transaction if a current transaction exists, behave like PROPAGATION_REQUIRED else. NEVER Execute non-transactionally, throw an exception if a transaction exists. NOT_SUPPORTED Execute non-transactionally, suspend the current transaction if one exists. REQUIRED Support a current transaction, create a new one if none exists. REQUIRES_NEW Create a new transaction, suspend the current transaction if one exists. SUPPORTS Support a current transaction, execute non-transactionally if none exists.

      5. 设置隔离事务属性

        1. Isolation

          DEFAULT Use the default isolation level of the underlying datastore. READ_UNCOMMITTED A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur. READ_COMMITTED A constant indicating that dirty reads are prevented; non-repeatable reads and phantom reads can occur. REPEATABLE_READ A constant indicating that dirty reads and non-repeatable reads are prevented; phantom reads can occur. SERIALIZABLE A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented.

          1. 脏读 dirty read

          2. 不可重复读 non-repeatable read

          3. 幻读 phantom read

      6. 设置回滚事务属性

        1. rollBackFor

        2. noRollBackFor

      7. 设置超时和只读事务属性

        1. timeout readOnly

      8. 用加载时织入来管理事务

    8. 9.Spring 对 ORM 的支持

      1. Spring 2.5 只支持 Hibernate 3.1 或更高

      2. 不同数据访问策略的核心编程元素

        1. JDBC

          1. 资源: Connection

          2. 资源工厂: DataSource

          3. 异常: SQLException

        2. Hibernate

          1. Session

          2. SessionFactory

          3. HibernateException

        3. JPA

          1. EntityManager

          2. EntityManagerFactory

          3. PersistenceException

      3. Spring 对不同数据存取策略的支持类

        1. JDBC

          1. 模板类: JdbcTemplate

          2. DAO 支持类: JdbcDaoSupport

          3. 事务管理器: DataSourceTransaction

        2. Hibernate

          1. HibernateTemplate

          2. HibernateDaoSupport

          3. HibernateTransactionManager

        3. JPA

          1. JpaTemplate

          2. JpaDaoSupport

          3. JpaTransactionManager

    9. 10.Spring MVC 框架

    10. 11.Spring 和其他框架的整合

    11. 12.对测试的支持

    12. 13-19.Spring 高级主题

      1. 13.Spring Security 框架

      2. 14.Spring Portlet kuangjia

      3. 15.Spring Web Flow

      4. 16.Web Service

      5. 17.对 EJB 和 JMS 的支持

      6. 18.对 JMX 电子邮件,调度的支持

      7. 19.Spring 脚本编程

    1. Bean 的生命周期

      1. 1.通过构造器或者工厂方法创建 Bean 的实例

        1. 2.为 Bean 的属性设置值和对其他 Bean 的引用

          1. 3.调用定义在感知接口里的 setter 方法

            1. 4.将 Bean 实例传给每个 Bean 后置处理器的 postProcessBeforeInitialization() 方法

              1. 5.调用 Bean 的初始化回调方法

                1. 6.将 Bean 实例传给每个 Bean 后置处理器的 postProcessAfterInitialization() 方法

                  1. 7.Bean 可以使用了

                    1. 8.当容器关闭时,调用 Bean 的销毁回调方法

    2. 1.在容器中保存的 Bean 都是 Object, 使用前要自己转型 2.容器里的 Bean 是有优先级的, Spring 自己定义的 Bean 要比我们定义的 Bean 优先级高, 可以通过实现接口 PriorityOrdered 来改变优先级 3.不要自己去 new, 问 Spring 要对象. 4.Spring 里一切都是 Bean

    3. 署名-相同方式共享

      author: hellojinjie 署名-相同方式共享 http://creativecommons.org/licenses/by-sa/3.0/deed.zh

  • All Comments ( 2 )
    xermind said at 2010-03-31 14:01:07
    Nice
    louisliao said at 2010-03-31 02:49:42
    good

    Spring Recipes

    Added: 2010-02-23 08:30:46

    From: hellojinjie (Joined 2008-11-18 05:56:21)

    2433 views |407 downloads

    Spring Recipes

    More From: hellojinjie

    版本控制
    版本控制
    2010-03-07 11:22:53|88 views
    Head First 设计模式
    Head First 设计模式
    2010-03-07 11:18:49|73 views
    Struts 基础教程
    Struts 基础教程
    2010-03-07 11:18:26|56 views
    Hibernate Quickly
    Hibernate Quickly
    2010-03-04 02:24:37|75 views
    java 设计模式
    java 设计模式
    2010-03-04 02:24:28|91 views
    Spring Recipes
    Spring Recipes
    2010-02-23 08:30:46|2433 views
    2011 考研
    2011 考研
    2010-01-15 08:44:19|84 views
    大学
    大学
    2009-05-02 05:14:25|90 views