spring4纯注解方式实现aop配置

maven配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.outofmemory</groupId>
	<artifactId>spring-aop-aspect</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>spring-aop-aspect</name>
	<url>http://maven.apache.org</url>
	<properties>
		<spring.version>4.1.1.RELEASE</spring.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.6.12</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.6.12</version>
		</dependency>
		<dependency>
			<groupId>cglib</groupId>
			<artifactId>cglib</artifactId>
			<version>2.2</version>
		</dependency>
	</dependencies>
</project>

创建代理标志注解(只有拥有此注解的方法才被代理)

package com.sisheng.equity.test;

import java.lang.annotation.*;

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface UseProxy {
}

创建要被代理的类(一般创建接口,然后代理接口)

package com.sisheng.equity.test;


import org.springframework.stereotype.Component;

@Component
public class Person {

    @UseProxy
    public void sayHello(String name) {
        System.out.println(name + ":hehe");
    }
}

创建代理类

package com.sisheng.equity.test;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

@Aspect
public class PersonAdvice {

    //定义切点
    @Pointcut(value = "@annotation(com.sisheng.equity.test.UseProxy)")
    private void anyMethod(){}//定义一个切入点

    @AfterReturning("anyMethod()")
    public void doAfter(){
        System.out.println("最终通知");
    }

    @AfterThrowing("anyMethod()")
    public void doAfterThrow(){
        System.out.println("例外通知");
    }

    @After("anyMethod()")
    public void after(JoinPoint point) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        //获取被代理的方法对象
        Method targetMethod = methodSignature.getMethod();
        //获取被代理的类对象
        Class targetClass = point.getSignature().getDeclaringType();
        //获取被代理方法的注解信息
        Annotation[] annotations = targetMethod.getAnnotations();
        //获取被代理方法传入的参数的信息
        Object[] params = point.getArgs();

        System.out.println("method name:"+point.getSignature().getName());
        System.out.println("method name:"+targetMethod.getName());
        System.out.println("class name:"+targetClass.getName()+":before");
        System.out.println("annotations name:"+annotations[0]);
        System.out.println("params name:"+params[0]);

        System.out.println("后置通知");
    }

    @Before("anyMethod()")
    public void before(JoinPoint point) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) point.getSignature();
        //获取被代理的方法对象
        Method targetMethod = methodSignature.getMethod();
        //获取被代理的类对象
        Class targetClass = point.getSignature().getDeclaringType();
        //获取被代理方法的注解信息
        Annotation[] annotations = targetMethod.getAnnotations();
        //获取被代理方法传入的参数的信息
        Object[] params = point.getArgs();

        System.out.println("method name:"+point.getSignature().getName());
        System.out.println("method name:"+targetMethod.getName());
        System.out.println("class name:"+targetClass.getName()+":before");
        System.out.println("annotations name:"+annotations[0]);
        System.out.println("params name:"+params[0]);

        System.out.println("前置通知");
    }

    @Around("anyMethod()")
    public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("进入环绕通知");
        Object object = pjp.proceed();//执行该方法
        System.out.println("退出方法");
        return object;
    }
}

创建spring配置类

package com.sisheng.equity.test;

import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Component;

@Configuration
//自动扫描配置
@ComponentScan(basePackages = {"com.sisheng.equity.test"}, useDefaultFilters = false, includeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Aspect.class, Component.class})
})
//启用自动代理功能(相当于xml配置中这句话:<aop:aspectj-autoproxy/>)
@EnableAspectJAutoProxy
public class AppConfig {

}

创建测试类

package com.sisheng.equity.test;

import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {AppConfig.class})
public class TestLock {

    @Resource
    private Person person;

    @Before
    public void setUp() throws Exception {
    }

    @Test
    public void testMyDao() {
        person.sayHello("nihao");
    }

    @Ignore
    public void testOtherSpringObject() {
        System.out.println("Not yet implemented");
    }
}






转载于:https://my.oschina.net/u/2316420/blog/662412

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值