代码改变世界

Java AOP 注解配置与xml配置

2017-02-17 20:40  甘雨路  阅读(532)  评论(0编辑  收藏  举报
<?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:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:context="http://www.springframework.org/schema/context"
    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/jdbc 
        http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 配置自动扫描 -->
    <context:component-scan base-package="cn.zr.aoptest"/>
    
    
    <!--注解方式  配置aop自动代理 -->
    <aop:aspectj-autoproxy/>
    
    <!-- xml方式  配置aop -->
    <bean id="personDaoImpl" class="cn.zr.aoptest.dao.person.impl.PersonDaoImpl"></bean>
    <bean id="personManager" class="cn.zr.aop.utils.PersonManager"></bean>
    <aop:config>
        <aop:pointcut
            expression="execution(* cn.zr.aoptest.dao.person.impl.*.*(..))"
            id="pointcut" />
        <aop:aspect id="xmlaop" ref="personManager">
             <aop:before method="beforeInfo" pointcut-ref="pointcut" />
            <aop:after-returning method="afterReturnInfo" pointcut-ref="pointcut"/>
            <aop:after method="afterInfo" pointcut-ref="pointcut"/>
            <aop:after-throwing method="exceptionInfo" throwing="throwable" pointcut-ref="pointcut"/>
            <aop:around method="aroundInfo" pointcut-ref="pointcut"/>
        </aop:aspect>
    </aop:config>






</beans>
package cn.zr.aop.utils;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

public class PersonManager {
    
    public void beforeInfo(JoinPoint joinPoint){
        System.out.println("获取信息...前");
        //获取参数
        Object[] objects = joinPoint.getArgs();
        for (Object object : objects) {
            System.out.println(object);
        }
    }
    
    public void afterReturnInfo(){
        System.out.println("获取信息...后");
        
    }
    
    public void afterInfo(){
        System.out.println("获取信息...最终");
        
    }
    
    
    public void exceptionInfo(Throwable throwable){
        System.out.println("出现异常:"+throwable);
        
    }
    
    public Object aroundInfo(ProceedingJoinPoint pjp){
        
        Object obj = null;
        System.out.println("===  环绕前  ===");
        try {
            obj = pjp.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("===  环绕后  ===");
        return obj;
    }
}
package cn.zr.aoptest.dao.person.impl;



public class PersonDaoImpl {
    
    
    public void getInfo() {
        int num = 10/0;
        System.out.println("获取信息");
    }
    
}
package cn.zr.aoptest.dao.person.impl;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class TransactionManager {
    // 设置切点
    @Pointcut("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))")
    public void methodPointcut() {
    }
    
    // 前置通知
    @Before(value=("execution(* cn.zr.aoptest.Userdao.impl.UserDaoImpl.*(..))"))
    public void beginTransactionManager(JoinPoint jp){
        System.out.println("...开始事务...");
        Object[] objs = jp.getArgs();
        for (Object object : objs) {
            System.out.println(object+"!!!");
        }
    }
    
    // 后置通知
    @AfterReturning("methodPointcut()")
    public void commitTrasactionManager(){
        System.out.println("===提交事务===");
    }
    
    // 最终通知(finally)
    @After("methodPointcut()")
    public void finallyManager(){
        
        System.out.println("~~~无论是否异常,始终执行~~~");
    }
    
    // 异常通知
    //@AfterThrowing(pointcut = "controllerAspect()", throwing="e") 
    @AfterThrowing(value="methodPointcut()",throwing="ep")
    public static void exceptionManager(Throwable ep){
        System.out.println("<<<事务回滚>>>,exception:"+ep);
    }
    
    // 环绕通知
    @Around("methodPointcut()")
    public Object aroundTrasactionManager(ProceedingJoinPoint joinPoint) {
        Object obj = null;
        System.out.println("---=围绕通知前=---");
        try {
            obj = joinPoint.proceed();
        } catch (Throwable e) {
            e.printStackTrace();
        }
        System.out.println("***=围绕通知后=***");
        return obj;
    }
    
    
    
    
}
package cn.zr.aoptest.Userdao.impl;

import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl {
    
    
    /**
     * 添加用户操作
     */
    public void addUser(String name){
        
        System.out.println("我要进行添加用户操作"+name);
        int count = 0;
        int num = 100/count;
    }
    
}
package cn.zr.aoptest.test;

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

import cn.zr.aoptest.Userdao.impl.UserDaoImpl;
import cn.zr.aoptest.dao.person.impl.PersonDaoImpl;

public class BeanTest {

    public static void main(String[] args) {
        
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        
        UserDaoImpl impl = (UserDaoImpl) ac.getBean(UserDaoImpl.class);
        impl.addUser("lf");
        
        PersonDaoImpl personDaoImpl = (PersonDaoImpl) ac.getBean("personDaoImpl");
        personDaoImpl.getInfo();
        
    }

}