• AODp


    一、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"); 

        } 

    }  

  • 相关阅读:
    Java虚拟机基础
    排序系列之插入排序
    排序系列之冒泡排序
    成为优秀程序员的10个要点
    23个适合Java开发者的大数据工具和框架
    成为一个更优秀的开发者的10种方式
    Mybatis自动生成实体类
    Maven-SSM项目pom.xml配置以及springmvc配置以及mybatis配置及web.xml配置
    SSM项目layui分页实例
    图书管理系统(毕业论文)
  • 原文地址:https://www.cnblogs.com/lihaiyang123/p/7084595.html
Copyright © 2020-2023  润新知