spring总结二,注解代替配置文件

一.注解代替xml配置
    准备工作:
        4+2 + spring-aop包
        xml中导入context约束
        在xml中开启扫描包中类的注解
    注解:
        @Component("BeanName") 将对象注册到spring容器
            |-    @Controler
            |-    @Service
            |-    @Repository
        
        @Scope    指定对象的作用范围
            |- singleton
            |- prototype
            
        @Value 值类型属性注入
            
        @Autowired 自动属性注入.根据类型注入.
        @Qulifier 指定注入的对象的名称
        
        @Resource 指定对象的名称注入
        
        @PostConstruct 初始化方法
        @PreDestory    销毁方法

二.spring AOP开发
        
        aop思想: 纵向重复,横向抽取.
            |- filter中
            |- 动态代理
            |- interceptor中
            
        spring AOP: 封装了动态代理技术.来体现aop.
        
        
        springaop实现:     可以对所有对象进行代理
                |- 动态代理     代理需要实现接口.
                |- cglib代理 对目标对象继承代理.
        
        springaop名词:
                join point: 连接点.所有可以织入通知的方法.
                point cut : 切入点.需要|已经织入通知的方法.
                advice:        需要增强的代码.
                weaving:    动词.将通知应用的切点的过程.
                target: 目标对象.
                proxy:    代理对象
                aspect: 切面. 切入点+通知
                
        步骤:
            1.导包
                4+2
                2 aop+aspect
                2 aop联盟+weaving
            2.准备目标对象

            3.准备通知类
                 前置通知
                 后置通知 方法正常结束
                 环绕通知
                 异常拦截通知
                 后置通知 无论如何都执行
        
            4.配置文件中配置,导入aop约束
                1>目标对象
                2>通知对象
                3><aop:config>
                        <aop:ponint-cut id="切点名称" expression="execution(切点表达式)" />
                        <aop:aspect ref="通知对象名称" >
                            <aop:before method="" ponintcut-ref=""  />
                            <aop:after-returning method="" ponintcut-ref=""  />
                            <aop:around method="" ponintcut-ref=""  />
                            <aop:after-throwing method="" ponintcut-ref=""  />
                            <aop:after method="" ponintcut-ref=""  />
                        </aop:aspect>
        
        扩展:使用注解完成aop
            1.导包
                4+2
                2 aop+aspect
                2 aop联盟+weaving
            2.准备目标对象

            3.准备通知类
            4.配置文件中配置,导入aop约束
                1>目标对象
                2>通知对象
                3><aop:aspect-autoproxy> 开启注解aop
                
            5.注解
                @Aspect 指定当前类是通知类
                
                @Before 前置通知方法
                @after-returning 后置通知方法
                @around 环绕通知方法
                @after-throwing 异常拦截通知方法
                @after 后通知方法
                
                @PointCut 抽取切点表达式

猜你喜欢

转载自blog.csdn.net/Hydra_shuang/article/details/81667787
今日推荐