本文主要是介绍spring aspectj JoinPoint,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 定义目标接口实现* @author thinkpad**/
public class HelloWorld2ServiceImpl implements IHelloWorld2Service {//使用@Pointcut进行命名切入点声明@Pointcut(value="execution(* com.boventech..*.HelloWorld2ServiceImpl(..))&& args(param)")@Overridepublic void sayHello2(String param) {System.out.println("============Hello World!");}
}
//使用@Aspect将POJO声明为切面;
@Aspect
public class HelloWorldAspectAnnotation {/*** JoinPoint接口* @param joinPoint*//*public interface JoinPoint {String toString(); //连接点所在位置的相关信息String toShortString(); //连接点所在位置的简短相关信息String toLongString(); //连接点所在位置的全部相关信息Object getThis(); //返回AOP代理对象Object getTarget(); //返回目标对象Object[] getArgs(); //返回被通知方法参数列表Signature getSignature(); //返回当前连接点签名SourceLocation getSourceLocation();//返回连接点方法所在类文件中的位置String getKind(); //连接点类型StaticPart getStaticPart(); //返回连接点静态部分}*///定义前置通知,注意这里是sayHello2//使用@Before进行前置通知声明,其中value用于定义切入点表达式或引用命名切入点@Before(value="execution(* com.boventech..*.sayHello2(..))&& args(param)",argNames="param")public void beforeAdvice(JoinPoint joinPoint,String param) {System.out.println("===param:" + param);System.out.println("=======================");System.out.println(joinPoint.getArgs().length);System.out.println("=======================");System.out.println(joinPoint.toString());System.out.println("=======================");System.out.println(joinPoint.getTarget());System.out.println("=======================");System.out.println(joinPoint.getThis());System.out.println("=======================");System.out.println("===========before advice");}/*value:指定切入点表达式或命名切入点;pointcut:同样是指定切入点表达式或命名切入点,如果指定了将覆盖value属性指定的,pointcut具有高优先级;*/@AfterReturning(value="execution(* com.boventech..*.sayHello2(..))&& args(param)",argNames="param",pointcut="execution(* com.boventech..*.sayHello2(..))&& args(param)")public void afterFinallyAdvice(JoinPoint joinPoint,String param) {System.out.println("param:"+param);System.out.println("===========");System.out.println("===========after finally advice");}
}
public class AopAnnotationTest {@Testpublic void testHelloworld() {ApplicationContext ctx = new ClassPathXmlApplicationContext("/helloWorld2.xml");IHelloWorld2Service helloworldService =ctx.getBean("helloWorld2Service", IHelloWorld2Service.class);String param = "12";helloworldService.sayHello2(param);}}
与前面blog比较,可明白JoinPoint的用法
这篇关于spring aspectj JoinPoint的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!