zoukankan      html  css  js  c++  java
  • [spring]AOP(切面)编程

    AOP 即 Aspect Oriented Program 面向切面编程
    首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能
    所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务
    所谓的周边功能,比如性能统计,日志,事务管理等等
     
    周边功能在Spring的面向切面编程AOP思想里,即被定义为切面
     
    在面向切面编程AOP的思想里面,核心业务功能和切面功能分别独立进行开发
    然后把切面功能和核心业务功能 "编织" 在一起,这就叫AOP
     
     
    =================
    一、applicationContext.xml配置的方法:
    <bean id="loggerAspect" class="aspect.LoggerAspect"/>
    
    <aop:config>
        <!--核心业务功能-->
        <aop:pointcut id="loggerCutpoint"
            expression=
                "execution(* service.ProductService.*(..)) "/>
    
        <!--辅助功能-->
        <aop:aspect id="logAspect" ref="loggerAspect">
            <aop:around pointcut-ref="loggerCutpoint" method="log"/>
        </aop:aspect>
    </aop:config>

    二、注解方式的切面:

    @Aspect 注解表示这是一个切面
    @Component 表示这是一个bean,由Spring进行管理
    @Around(value = "execution(* com.how2java.service.ProductService.*(..))") 表示对com.how2java.service.ProductService 这个类中的所有方法进行切面操作
     
    * 返回任意类型
    com.how2java.service.ProductService.* 包名以 com.how2java.service.ProductService 开头的类的任意方法
    (..) 参数是任意数量和类型
     
    package aspect;
    
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LoggerAspect {
    
        @Around(value = "execution(* service.ProductService.*(..))")
        public Object log(ProceedingJoinPoint joinPoint) throws Throwable {
            System.out.println("start log:" + joinPoint.getSignature().getName());
            Object object = joinPoint.proceed();
            System.out.println("end log:" + joinPoint.getSignature().getName());
            return object;
        }
    }

    其中在applicationContext.xml的配置:

        <context:component-scan base-package="com.afeng.aspect"/>
        <context:component-scan base-package="com.afeng.service"/>
        <aop:aspectj-autoproxy/> 
  • 相关阅读:
    亚马逊云服务器VPS Amazon EC2 免费VPS主机配置CentOS及其它内容
    Linux + Mono 目前已经支持Entity Framework 6.1
    CentOS上 Mono 3.2.8运行ASP.NET MVC4经验
    Linux CentOS下如何确认MySQL服务已经启动
    C#使用Timer.Interval指定时间间隔与指定时间执行事件
    MySQL数据库有外键约束时使用truncate命令的办法
    C++中字符和字符串的读取与使用
    结构体的运算符重载
    P1358 扑克牌
    P1284 三角形牧场
  • 原文地址:https://www.cnblogs.com/afeng2010/p/10060407.html
Copyright © 2011-2022 走看看