zoukankan      html  css  js  c++  java
  • Spring2.5零散笔记

    IOC(控制反转),也称DI(Denpendency Inject依赖注入)。
    1.把自己new的东西改为由容器提供。
        初始化具体bean
        动态装配
    2.好处:灵活配置。
    
    控制反转:将控制权交给容器
    依赖注入:setdao根据实际的dao注入(setdao依赖于实际的dao)
    
    spring是一个IOC容器,可以实例化具体的bean,还可以动态装配。
    还支持AOP:安全检查,管理transaction。
        
    
    FAQ:不给提示
    a.window-preferences-myeclipse-xml catalog
    b.User Specified Entries - add
        location:
            ....../spring-framework-2.5.6/dist/resources/spring-beans-2.5.xsd
        Key Type:
            Schema Location
        Key:
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        
    注入类型:
        1.setter(重要)
        2.构造方法
        3.接口注入
    Id&Name:
        name和id都可以,但是name中可以含有特殊字符。
    简单属性注入:<property name=... value=...>
    
    Bean的scope(生存范围):
        默认是sigleton:单例,每次取到的bean都是同一个。
        还有prototype:取到的不是同一个,每次都创建新的对象。
    还有和web相关的三种方法,较少用。
    
    集合注入。
    
    自动装配:
        byName
        byType
    一般写在bean标签上,如果所有的bean都用同一种,可以使用beans的属性:dafaule-autowire
    
    生命周期:
        lazy-init    (一开始不初始化,用到的时候才初始化)
        init-method="init" destory-method="destory"    不要和prototype一起使用
    类被初始化的时候调用init,被消亡的时候调用destory
    
    @Autowired
        默认按类型by Type
        如果想用byName,使用@Qulifier
        写在priavate field(第三种注入形式)--不建议,破坏封装
        如果写在set上,@quailfier需要写在参数上
    
    @Resourde(重要)
        加入:j2ee/common-annotations.jar
        默认按名字,名字找不到,才按类型
        可以指定特定名称
        推荐使用
        不足:如果没有源码,就无法使用annotaton,只能使用xml
    
    @Component
        初始化的名字默认为类名首字母小写
        可以指定初始化bean的名字
    
    @Scope
    
    @PostConstruct = init-method
    @PreDestroy = destroy-method
    
    面向切面编程:
        Aspect-Oriented-Programming
    好处:可以动态的添加和删除在切面上的逻辑而不影响原来的执行代码
    例如:
    servlet:filter
    struct:Intercepter
    还可以应用于:权限检查,异常处理,效率计算,事务管理。
    
    概念(第22集后面):
        JoinPoint
        PointCut
        Aspect
        Advice
        Weave
    
    Spring AOP配置与应用:
    两种方式:使用Annotation和使用xml
    
    Annotation:
        加上对应的xsd文件spring-aop.xsd
        beans.xml<aop:aspect:j-autoproxy />
        此时就可以解析对应的Annotation
        建立我们的拦截类
        用@Aspect注解这个类
        建立处理方法
        用@before来注解这个方法
        写明白切入点
        让spring对我们的拦截器进行管理@Component
    常见的Accotation:
        @Pointcut
        @Before
        @AfterReturning
        @AfterThrowing
        @After
        @Around
        
    Spring整合Hibernate:
        Spring指定datasource
        1.参考文档,zhao dbcp.BasicDataSource
            c3p0
            dbcp
            proxxo
    在DAO或者Service中注入dataSource
    在Spring中可以hi用PropertyHolderConfigure来读取Properties文件的内容。
    Spring 整合Hibernate:
        1.<bean..AnnotationSessionFactoryBean>
            <property dataSource>
            <annotationClasses>
        2.引入Hibernate系列jar包
        User上加Annotation
        UserDAO或者UserService注入SessionFactory
        jar包问题一个一个解决。
    
    
    声明式的事务管理:
        事务加载DAO层还是Service层?
        annotation:
        加入annotation.xsd
        加入txManager bean
        <tx:annotation-driven
        在需要的事务的方法上加@Transational
        需要注意,使用SessionFactory.getCurrentSession不要使用openSession
    @Transactional详解:
        什么时候rollback:
        1.运行期异常,非运行期异常不会触发rolback
        2.必须uncheck(没有catch)
        3.不管什么异常,只要你catch了,spring就会放弃管理
        4.事物传播特性:propagation_required
        5.read_only
    
    xml(推荐,可以同时配置很多方法)
        1.<bean txManager
        2.<aop:config
            <aop:pointcut
            <aop:advisor pointcut-ref advice-ref
        3.<tx:advise:id transaction-manager=
    
    HibernateTemplate、HibernateCallback、HibernateDaoSupport(不重要)
    介绍:
        设计模式:Template Method
        Callback----回调、钩子(函数)
        1.第一种:
            <1.在Spring中初始化HibernateTemplate,注入sessionFactory
            <2.DAO里注入HibernateTemplate
            <3.save写getHibernateTemplate.save(0
        2.第二种:
            <1.从HibernateDaoSupport继承
            <2.必须卸载xml文件中,无法使用Annotation,因为set方法在父类中,而且是final的
    
    spring整个hibernate的时候使用packageToScan属性,可以让spring自动扫面对应包下面的实体类。
    
    整合步骤:
        需要的额jar包在48集。
        将这些所有的jar包保存到一个位置,使用的时候直接copy。
        首先加入jar包
    整合Spring+Hibernate:
        1.建立package
            <1.dao/daoimpl/model/service/serviceimpl/test
        2.建立对应的接口与类框架
            S2SH_01
        3.建立Spring的配置文件(建议自己保留一份经常使用的配置文件,以后用到的时候直接copy改)
        4.建立数据库
        5.加入Hibernate注解
            <1.在实体类上加相应的注解@Entity@Id等
            <2.在beans配置文件配置对应的实体类,使之受管
        6.写dao service的实现
        7.加入Spring注解
            <1.在对应Service及DAO实现中加入@Component,让spring对其初始化
            <2.在service上加入@Transactional或者使用xml方式(此处建议后者,因为更简单)
            <3.在DAO中注入sessionFactory
            <4.在servcie中注入DAO
            <5.写dao与service的实现
        8.写测试
    整合Strust2
        结合点:Struts2的Action由Spring产生
        步骤:
        1.修改web.xml加入struts的filter
        2.再加入spring的listener,这样的话,webapp一旦启动,spring容器就初始化
        3.规划struts的action和jsp展现
        4.加入struts.xml
            修改配置,有spring替代struts产生Action对象
        5.修改action配置
            把类名改为bean对象的名称,这个时候就可以使用首字母小写了
            @Scope("prototype")不要忘记
        struts的读常量顺序:
        struts-default.xml----struts-plugin.xml----struts.xml----struts.properties----web.xml
    
    中文问题:
        1.struts2.1.8已经修正,只需要改i18n.encoding=gbk
        2.只用spring的characterencoding
        3.需要严格注意filter的顺序
        4.需要加到struts2的filter前面
    
    LazyInitializationException:
        1.OpenSessionInViewFilter
        2.需要严格顺序问题
        3.需要加到struts2的filter前面
    
        
  • 相关阅读:
    6. Flask请求和响应
    5. Flask模板
    FW:Software Testing
    What is the difference between modified duration, effective duration and duration?
    How to push master to QA branch in GIT
    FTPS Firewall
    Query performance optimization of Vertica
    (Forward)5 Public Speaking Tips That'll Prepare You for Any Interview
    (转)The remote certificate is invalid according to the validation procedure
    Change
  • 原文地址:https://www.cnblogs.com/mosquito-woo/p/3880740.html
Copyright © 2011-2022 走看看