介绍
这篇文章将为大家详细讲解有关弹簧使用顾问配置aop的方法,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
1,目标类
包com.gec.target; 公开课Hadoop { 公共空间食(){ System.out.println(“大象正在吃东西1“); 尝试{//耗时5秒 thread . sleep (5000); }捕捉(InterruptedException e) { e.printStackTrace (); } } }
2,增强类,此类必须要实现增强方位接口
包com.gec.advice; 进口org.springframework.aop.MethodBeforeAdvice; 进口java.lang.reflect.Method; 公共类BeforeMethodAdvice实现MethodBeforeAdvice { @Override 公共空间之前(方法方法,对象[]对象,对象o)抛出Throwable { System.out.println(“你如何); } }
3,配置文件
& lt;及# 63;xml version=?.0”;编码=癠TF-8", # 63;比; http://www.springframework.org/schema/beans" & lt;豆类xmlns=? xmlns: xsi=癶ttp://www.w3.org/2001/XMLSchema-instance" xmlns: p=癶ttp://www.springframework.org/schema/p" xmlns:上下文=癶ttp://www.springframework.org/schema/context" xmlns: util=癶ttp://www.springframework.org/schema/util" xmlns: aop=癶ttp://www.springframework.org/schema/aop"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/context http://www.springframework.org/schema/context/spring-context-4.3.xsd ,http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"的在 & lt; aop: aspectj-autoproxy/比; & lt; bean id=癰eforeMethodAdvice"类=癱om.gec.advice.BeforeMethodAdvice"/比; & lt; bean id=癶adoop"类=癱om.gec.target.Hadoop"/比; & lt; aop: config> & lt; !——定义一个切面——比; & lt; aop:顾问advice-ref=癰eforeMethodAdvice"切入点=爸葱?*食(. .)),/比; & lt;/aop: config> & lt;/beans>
4,测试
公共静态void main (String [] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext (“beans.xml"); Hadoop Hadoop=(Hadoop) ctx.getBean (“hadoop"); hadoop.eatting (); }
关于弹簧使用顾问配置aop的方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看的到。