基于@Aspect实现AOP的两种方式

第一种:

package com.anno;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)    //什么时候运行和结束
@Target( {ElementType.METHOD , ElementType.TYPE})    //能放在哪里.
public @interface AnnotationTwo {
}
package com.anno;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

@Aspect
@Component
@EnableAspectJAutoProxy
public class AspectAnnotationTOW {
	
	@Pointcut("@annotation(AnnotationTwo)")
	public void aspectTow(){
		
	}
	
	@Around("aspectTow()")
	public void aspectservice(ProceedingJoinPoint joinPoint){
		System.out.println(111);
		try {
			Object proceed = joinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
	}
	
}

第二种:

package com.testAnnotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)    //什么时候运行和结束
@Target( {ElementType.METHOD , ElementType.TYPE})    //能放在哪里.
public @interface AnnotationOfTest {
	public String say();
	public long howLong() default Long.MAX_VALUE;
}
package com.testAnnotation;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;

import com.rabbitmq.tools.jsonrpc.ProcedureDescription;


@Component
@EnableAspectJAutoProxy
@Aspect
public class AspectAnnotation {
	@Around("@annotation(annotationOfTest)")
	public Object interceptor(ProceedingJoinPoint pjp, AnnotationOfTest annotationOfTest ) {
		String say = annotationOfTest.say();
		long howlong = annotationOfTest.howLong();
			try {
				System.out.println(say+"\t"+howlong);
				Object object = pjp.proceed();
				return object;
			} catch (Throwable e) {
				e.printStackTrace();
			}
			return null;
	}
 }

配置文件:

<?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:util="http://www.springframework.org/schema/util"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
	http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
	http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-4.3.xsd">
  
    <context:component-scan base-package="com.testAnnotation"></context:component-scan>
	<context:component-scan base-package="com.service"></context:component-scan>
	<context:component-scan base-package="com.anno"></context:component-scan>
	
	<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
	 
   
    
 </beans> 
 
 
 

service:

package com.service;

import java.util.Date;

import org.springframework.stereotype.Service;

import com.anno.AnnotationTwo;
import com.testAnnotation.AnnotationOfTest;

@Service
public class service {
	@AnnotationTwo
	 public void go()
	 {
		 System.out.println(new Date().toLocaleString());
	 }
	

}

项目结构:

测试:

package com.test;

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

import com.service.service;
import com.testAnnotation.AnnotationOfTest;

public class Test {
	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		service bean = applicationContext.getBean(service.class);
		bean.go();
	}
}

 

所需要的包:

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务