web填坑-AOP

2017-02-22  本文已影响34人  在南方的北方人_Elijah

AOP面向切面编程

AOP AOP 横切逻辑 AOP术语

利用Spring注解方式实现AOP功能

注解实现AOP功能

student.java

package com.jike.spring.chapter09.aop.aspect;

public class Student {
            public String print(String name){  
                    System.out.println("print() method:" + name);  
                    return "hello";  
                }  

}

StuInterceptor.java

package com.jike.spring.chapter09.aop.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class StuInterceptor {

    /**
     * 打印方法AOP
     */
    @Pointcut("execution(* com.jike.spring.chapter09.aop.aspect.Student.print(..))")
    // @Pointcut("execution(* com.jike.spring.chapter09.aop.aspect.Student.*(..))")
    public void printMethod() {
    }

    @Before("printMethod()")
    public void printBeforeAdvice() {
        System.out.println("printBeforeAdvice()!");
    }

    @AfterReturning(pointcut = "printMethod()", returning = "flag")
    public void printAfterAdvice(String flag) {
        System.out.println("printAfterAdvice()! " + flag);
    }

    @After("printMethod()")
    public void finallyAdvice() {
        System.out.println("finallyAdvice()!");
    }

    @Around("printMethod() && args(name)")
    public Object printAroundAdvice(ProceedingJoinPoint pjp, String name)
            throws Throwable {
        Object result = null;
        if (name.equals("whc"))
            pjp.proceed();
        else
            System.out.println("print()方法以及被拦截...");
        return result;
    }
}

conf-aspect.xml配置文件

<?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.xsd  
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
    <aop:aspectj-autoproxy />
    <bean id="stuInterceptor" class="com.jike.spring.chapter09.aop.aspect.StuInterceptor"></bean>
    <bean id="stu" class="com.jike.spring.chapter09.aop.aspect.Student"></bean>
</beans>    

这里的<aop:aspectj-autoproxy />配置了aop切面代理的自动加载。

main.java入口类

package com.jike.spring.chapter09.aop.aspect;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
     public static void main(String[] args) {  
               ApplicationContext ctx = new ClassPathXmlApplicationContext("conf/conf-aspect.xml");  
                 Student stu = (Student)ctx.getBean("stu");  
                    stu.print("whc");  
                }  

}

打印结果
上一篇 下一篇

猜你喜欢

热点阅读