Spring 使用javaconfig配置aop

Posted junge8619

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring 使用javaconfig配置aop相关的知识,希望对你有一定的参考价值。

1.在xml中需要配置自动代理


/**
 * 
 */
package com.junge.demo.spring.dao;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;

import com.junge.demo.spring.advice.MyLogImpl;

/**
 * @author Administrator
 *
 */
@Configuration
@ComponentScan(basePackageClasses= {MyLogImpl.class,UserDao.class})
@EnableAspectJAutoProxy
public class DaoConfig {

}

2.java类需要添加@Aspect注解。

package com.junge.demo.spring.advice;

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;

import com.alibaba.fastjson.JSONObject;

@Component("myLog")
@Aspect
public class MyLogImpl {
    
    @Pointcut("execution(* com.junge.demo.spring.dao..*(..))")
    public void pointcut() {
        
    }

    @Before("pointcut()")
    public void beforeFunc() {
        System.out.println("beforeFunc ...");

    }

    @After("pointcut()")
    public void afterFunc() {
        System.out.println("afterFunc ...");

    }

    @AfterReturning("pointcut()")
    public void returnFunc() {
        System.out.println("returnFunc ...");

    }

    @AfterThrowing(pointcut="pointcut()", throwing="e")
    public void throwExpFunc(Exception e) {
        System.out.println("throwExpFunc ...");
        System.out.println(e.getMessage());

    }

    @Around("pointcut()")
    public void aroundFunc(ProceedingJoinPoint point) {
        System.out.println("arount before invoke ...");
        
        if (null != point.getArgs() && point.getArgs().length > 0) {
            for (Object arg : point.getArgs()) {
                System.out.println("around args:" + JSONObject.toJSON(arg));
            }
        }
        System.out.println(point.getTarget());
        System.out.println(point.getThis());
        System.out.println(point.getKind());
        System.err.println(point.getClass());
        System.out.println(point.getSignature());
        
        try {
            Object result = point.proceed();
            System.out.println("around result:" + JSONObject.toJSONString(result));
            System.out.println("around after return ...");
        } catch (Throwable e) {
            System.out.println("around after throws ...");
        }
        
        System.out.println("around after ...");

    }

}

 3.运行结果

/**
 * 
 */
package com.junge.demo.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import com.junge.demo.spring.dao.DaoConfig;
import com.junge.demo.spring.dao.UserDao;
import com.junge.demo.spring.dao.impl.cc.dd.ff.ee.SubClassTest;
import com.junge.demo.spring.service.IPerface;
import com.junge.demo.spring.service.ServiceConfig;

/**
 * @author Administrator
 *
 */
public class ServiceConfigApp {

    private static ApplicationContext applicationContext;

    /**
     * @param args
     */
    public static void main(String[] args) {
        applicationContext = new AnnotationConfigApplicationContext(ServiceConfig.class, DaoConfig.class);
//        System.out.println(applicationContext.getBeansOfType(IPerface.class));
//        applicationContext.getBean(UserDao.class);
        
        applicationContext.getBean(SubClassTest.class).addTest(3, 1);
    }

}

 

以上是关于Spring 使用javaconfig配置aop的主要内容,如果未能解决你的问题,请参考以下文章

Spring 使用javaconfig配置

Spring JavaConfig配置

IOC和AOP是什么鬼?

使用Java的方式配置Spring--JavaConfig实现配置

Spring重温--Spring JavaConfig

使用JavaConfig方式配置dubbox