spring 马士兵笔记

课程内容

  1. 面向接口(抽象)编程的概念与好处

  2. IOC/DI的概念与好处

    1. inversion of control

    2. dependency injection

  3. AOP的概念与好处

  4. Spring简介

  5. Spring应用IOC/DI(重要)

    1. xml

    2. annotation

  6. Spring应用AOP(重要)

    1. xml

    2. annotation

  7. Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2整合(重要)

    1. opensessionInviewfilter(记住,解决什么问题,怎么解决)

  8. Spring JDBC

面向接口编程(面向抽象编程)

  1. 场景:用户添加

  2. Spring_0100_AbstractOrientedProgramming

    1. 不是AOP:AspectOriented Programming

  3. 好处:灵活

什么是IOCDI),有什么好处

  1. 把自己new的东西改为由容器提供

    1. 初始化具体值

    2. 装配

  2. 好处:灵活装配

  3. 倚赖注入:谁调用谁,把后面的谁设置到前面的谁就是依赖注入。

Spring简介

  1. 项目名称:Spring_0200_IOC_Introduction

  2. 环境搭建

    1. 只用IOC

      1. spring.jar , jarkata-commons/commons-loggin.jar

  3. IOC容器

    1. 实例化具体bean

    2. 动态装配

  4. AOP支持

    1. 安全检查

    2. 管理transaction

Spring IOC配置与应用

  1. FAQ:不给提示:

    1. window – preferences – myeclipse – xml – xml catalog

    2. User Specified Entries – add

      1. Location: D:\share\0900_Spring\soft\spring-framework-2.5.6\dist\resources\spring-beans-2.5.xsd

      2. URI: file:///D:/share/0900_Spring/soft/spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd

      3. Key Type: Schema Location

      4. Key: http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

  2. 注入类型

    1. Spring_0300_IOC_Injection_Type

    2. setter(重要)

    3. 构造方法(可以忘记)

    4. 接口注入(可以忘记)

  3. id vs. name

    1. Spring_0400_IOC_Id_Name

    2. name可以用特殊字符

  4. 简单属性的注入

    1. Spring_0500_IOC_SimpleProperty

    2. <property name=… value=….>

  5. <bean 中的scope属性

    1. Spring_0600_IOC_Bean_Scope

    2. singleton 单例

    3. proptotype 每次创建新的对象

  6. 集合注入

    1. Spring_0700_IOC_Collections

    2. 很少用,不重要!参考程序

  7. 自动装配

    1. Spring_0800_IOC_AutoWire

    2. byName

    3. byType

    4. 如果所有的bean都用同一种,可以使用beans的属性:default-autowire

  8. 生命周期

    1. Spring_0900_IOC_Life_Cycle

    2. lazy-init (不重要)

    3. init-method destroy-methd 不要和prototype一起用(了解)

  9. Annotation第一步:

    1. 修改xml文件,参考文档<context:annotation-config/>

  10. @Autowired

    1. 默认按类型bytype

    2. 如果想用byName,使用@Qulifier

    3. 写在privatefield(第三种注入形式)(不建议,破坏封装)

    4. 如果写在set上,@qualifier需要写在参数上

  11. @Resource(重要)

    1. 加入:j2ee/common-annotations.jar

    2. 默认按名称,名称找不到,按类型

    3. 可以指定特定名称

    4. 推荐使用

    5. 不足:如果没有源码,就无法运用annotation,只能使用xml

  12. @Component @Service @Controller @Repository

    1. 初始化的名字默认为类名首字母小写

    2. 可以指定初始化bean的名字

  13. @Scope

  14. @PostConstruct = init-method; @PreDestroy = destroy-method;


什么是AOP

  1. 面向切面编程Aspect-Oriented-Programming

    1. 是对面向对象的思维方式的有力补充

  2. Spring_1400_AOP_Introduction

  3. 好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码

    1. Filter

    2. Struts2interceptor

  4. 概念:

    1. JoinPoint:@Before("execution(voidcom.yangunilay.dao.impl.UserDaoImpl.add(com.yangunilay.model.User))"),save执行之前。

    2. PointCut@Pointcut(“execution(*com.yangunilay.*.*(..))”),连接点的集合

    3. Aspect(切面):@Ascept,所放的那个类就是一个切面

    4. Advice@Before@After等等,加在切入点上的逻辑

    5. Target:被代理对象

    6. Weave:支路

Spring AOP配置与应用

  1. 两种方式:

    1. 使用Annotation

    2. 使用xml

  2. Annotation

    1. 加上对应的xsd文件spring-aop.xsd

    2. beans.xml <aop:aspectj-autoproxy />

    3. 此时就可以解析对应的Annotation

    4. 建立我们的拦截类

    5. @Aspect注解这个类

    6. 建立处理方法

    7. @Before来注解方法

    8. 写明白切入点(execution…….

    9. spring对我们的拦截器类进行管理@Component

  3. 常见的Annotation:

    1. @Pointcut

    2. @Before

    3. @AfterReturning

    4. @AfterThrowing

    5. @After

    6. @Around

  4. 织入点语法

    1. void !void

    2. 参考文档(*..

  5. xml配置AOP

    1. interceptor对象初始化

    2. <aop:config

      1. <aop:aspect …..

        1. <aop:pointcut

        2. <aop:before

Spring整合Hibernate

  1. Spring 指定datasource

    1. 参考文档,找dbcp.BasicDataSource

      1. c3p0

      2. dbcp

      3. proxool

    2. DAO或者Service中注入dataSource

    3. Spring中可以使用PropertyPlaceHolderConfigure来读取Properties文件的内容

  2. Spring整合Hibernate

    1. <bean .. AnnotationSessionFactoryBean>

      1. <property dataSource

      2. <annotatedClasses

    2. 引入hibernate系列jar

    3. User上加Annotation

    4. UserDAO或者UserServie注入SessionFactory

    5. jar包问题一个一个解决

  3. 声明式的事务管理

    1. 事务加在DAO层还是Service层?

    2. annotation

      1. 加入annotation.xsd

      2. 加入txManagerbean

      3. <tx:annotation-driven

      4. 在需要事务的方法上加:@Transactional

      5. 需要注意,使用SessionFactory.getCurrentSession不要使用OpenSession

    3. @Transactional详解

      1. 什么时候rollback

        1. 运行期异常,非运行期异常不会触发rollback

        2. 必须uncheck(没有catch)

        3. 不管什么异常,只要你catch了,spring就会放弃管理

        4. 事务传播特性:propagation_required

        5. read_only

    4. xml(推荐,可以同时配置好多方法)

      1. <bean txmanager

      2. <aop:config

        1. <aop:pointcut

        2. <aop:advisor pointcut-ref advice-ref

      3. <tx:advice: id transaction-manager =

    5. HibernateTemplateHibernateCallbackHibernateDaoSupport(不重要)介绍

      1. 设计模式:TemplateMethod

      2. Callback:回调/钩子函数

      3. 第一种:(建议)

        1. spring中初始化HibernateTemplate,注入sessionFactory

        2. DAO里注入HibernateTemplate

        3. savegetHibernateTemplate.save();

      4. 第二种:

        1. HibernateDaoSupport继承

        2. 必须写在xml文件中,无法使用Annotation,因为set方法在父类中,而且是final

    6. spring整合hibernate的时候使用packagesToScan属性,可以让spring自动扫描对应包下面的实体类

Struts2.1.6 + Spring2.5.6 + Hibernate3.3.2

  1. 需要的jar包列表

    jar包名称

    所在位置

    说明

    antlr-2.7.6.jar

    hibernate/lib/required

    解析HQL

    aspectjrt

    spring/lib/aspectj

    AOP

    aspectjweaver

    ..

    AOP

    cglib-nodep-2.1_3.jar

    spring/lib/cglib

    代理,二进制增强

    common-annotations.jar

    spring/lib/j2ee

    @Resource

    commons-collections-3.1.jar

    hibernate/lib/required

    集合框架

    commons-fileupload-1.2.1.jar

    struts/lib

    struts

    commons-io-1.3.2

    struts/lib

    struts

    commons-logging-1.1.1

    单独下载,删除1.0.4(struts/lib)

    struts

    spring

    dom4j-1.6.1.jar

    hibernate/required

    解析xml

    ejb3-persistence

    hibernate-annotation/lib

    @Entity

    freemarker-2.3.13

    struts/lib

    struts

    hibernate3.jar

    hibernate


    hibernate-annotations

    hibernate-annotation/


    hibernate-common-annotations

    hibernate-annotation/lib


    javassist-3.9.0.GA.jar

    hiberante/lib/required

    hibernate

    jta-1.1.jar

    ..

    hibernate transaction

    junit4.5



    mysql-



    ognl-2.6.11.jar

    struts/lib


    slf4j-api-1.5.8.jar

    hibernate/lib/required

    hibernate-log

    slf4j-nop-1.5.8.jar

    hibernate/lib/required


    spring.jar

    spring/dist


    struts2-core-2.1.6.jar

    struts/lib


    xwork-2.1.2.jar

    struts/lib

    struts2

    commons-dbcp

    spring/lib/jarkata-commons


    commons-pool.jar

    ..


    struts2-spring-plugin-2.1.6.jar

    struts/lib


  2. BestPractice

    1. 将这些所有的jar包保存到一个位置,使用的时候直接copy

  3. 步骤

    1. 加入jar

    2. 首先整合Spring+ Hibernate

      1. 建立对应的package

        1. dao / dao.impl / model / service / service.impl/ test

      2. 建立对应的接口与类框架

        1. S2SH_01

      3. 建立spring的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接copy改)

      4. 建立数据库

      5. 加入Hibernate注解

        1. 在实体类上加相应注解@Entity@Id

        2. beans配置文件配置对应的实体类,使之受管

      6. daoservice的实现

      7. 加入Spring注解

        1. 在对应ServiceDAO实现中加入@Component,让spring对其初始化

        2. Service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)

        3. DAO中注入sessionFactory

        4. Service中注入DAO

        5. DAOService的实现

      8. 写测试

    3. 整合Struts2

      1. 结合点:Struts2ActionSpring产生

      2. 步骤:

        1. 修改web.xml加入strutsfilter

        2. 再加入springlistener,这样的话,webapp一旦启动,spring容器就初始化了

        3. 规划strutsactionjsp展现

        4. 加入struts.xml

          1. 修改配置,由spring替代struts产生Action对象

        5. 修改action配置

          1. 把类名改为bean对象的名称,这个时候就可以使用首字母小写了

          2. @Scope(“prototype”)不要忘记

      3. struts的读常量:

        1. struts-default.xml

        2. struts-plugin.xml

        3. struts.xml

        4. struts.properties

        5. web.xml

      4. 中文问题:

        1. Struts2.1.8已经修正,只需要改i18n.encoding= gbk

        2. 使用springcharacterencoding

        3. 需要严格注意filter的顺序

        4. 需要加到Struts2filter前面

      5. LazyInitializationException

        1. OpenSessionInViewFilter

        2. 需要严格顺序问题

        3. 需要加到struts2filter前面



struts2-spring-plugin问题

/*@Resource(name ="memberServiceImpl")不起作用了,actionstruts2-spring-plugin插件管,不归spring管,spring容器产生负责给action用的对象

* 做法:1。把@Resource(name= "projectServiceImpl")写在private属性上

* 2。在action上什么都不写,默认按照名字自动注入,把名字都约定好,而ServiceImpl上写上@Componentaction对应service属性的名字就行

* 3.按照类型注入,将struts2-spring-plugin改成按类型注入,则ServiceImpl上写上@Component也不用写了

* */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值