码迷,mamicode.com
首页 > 其他好文 > 详细

软件151 -刘光星

时间:2017-06-28 23:22:28      阅读:419      评论:0      收藏:0      [点我收藏+]

标签:www   创建   com   意思   save   before   expr   int   jdk 动态代理   

一、AOP是OOP的延续,是(Aspect Oriented Programming)的缩写,意思是面向切面编程。

AOP(Aspect Orient Programming),作为面向对象编程的一种补充,广泛应用于处理一些具有横切性质的系统级服务,

如事务管理、安全检查、缓存、对象池管理等。 AOP 实现的关键就在于 AOP 框架自动创建的 AOP 代理,

AOP 代理则可分为静态代理和动态代理两大类,其中静态代理是指使用 AOP 框架提供的命令进行编译,

从而在编译阶段就可生成 AOP 代理类,因此也称为编译时增强;

而动态代理则在运行时借助于 JDK 动态代理、CGLIB 等在内存中"临时"生成 AOP 动态代理类,因此也被称为运行时增强

AOP 的实现:

技术分享

MyInterceptor、MyInterceptor2分别是以annotations和xml定义的切面类

 

package com.service;    

import org.aspectj.lang.annotation.Aspect; 

import org.aspectj.lang.annotation.Before; 

import org.aspectj.lang.annotation.Pointcut;   

@Aspect 

public class MyInterceptor { 

    @Pointcut("execution (* com.serviceImpl.PersonServiceImpl.*(..))") 

    private void myMethod(){};   

    @Before("myMethod()") 

    public void doAccessCheck(){ 

        System.out.println("before"); 

    }     

}

[java] view plain copy

package com.service;

public class MyInterceptor2 { 

    public void doAccessCheck(){ 

        System.out.println("before"); 

    } 

业务和接口

[java] view plain copy

package com.service;    

public interface PersonService { 

    public void save(String name); 

    public void update(String name); 

[java] view plain copy

package com.serviceImpl; 

import com.service.PersonService;   

public class PersonServiceImpl implements PersonService { 

    @Override 

    public void save(String name) { 

        // TODO Auto-generated method stub 

        System.out.println("保存"); 

    } 

    @Override 

    public void update(String name) { 

        // TODO Auto-generated method stub 

        System.out.println("修改"); 

    } 

}

简单做个方法前通知,其他的都一样。

[java] view plain copy

<?xml version="1.0" encoding="UTF-8"?> 

<beans xmlns="http://www.springframework.org/schema/beans" 

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" 

    xsi:schemaLocation="http://www.springframework.org/schema/beans 

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

            http://www.springframework.org/schema/aop 

           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> 

    <aop:aspectj-autoproxy/>    

    <bean id="personServiceImpl" class="com.serviceImpl.PersonServiceImpl"></bean> 

    <bean id="personInterceptor" class="com.service.MyInterceptor2"></bean>   

    <aop:config> 

        <aop:aspect id="asp" ref="personInterceptor"> 

            <aop:pointcut id="myCut" expression="execution (* com.serviceImpl.PersonServiceImpl.*(..))"/> 

            <aop:before pointcut-ref="myCut" method="doAccessCheck"/> 

        </aop:aspect>      

    </aop:config> 

</beans>  

测试类

[java] view plain copy

package com.test;   

import org.junit.Test; 

import org.springframework.context.ApplicationContext; 

import org.springframework.context.support.ClassPathXmlApplicationContext;    

import com.service.PersonService;   

public class AopTest {     

    @Test 

    public void interceptorTest(){ 

        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml"); 

        PersonService ps = (PersonService) ac.getBean("personServiceImpl"); 

        ps.save("aa"); 

    } 

}  

软件151 -刘光星

标签:www   创建   com   意思   save   before   expr   int   jdk 动态代理   

原文地址:http://www.cnblogs.com/liuguangxing/p/7091830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!