Spring基于advisor配置aop过程解析

网友投稿 253 2022-11-15


Spring基于advisor配置aop过程解析

1、目标类

package com.gec.target;

public class Hadoop {

public void eatting() {

System.out.println("大象正在吃东西 1");

try {

//耗时5秒

Thread.sleep(5000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

2、增强类,此类必须要实现增强方位接口

package com.gec.advice;

import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class BeforeMethodAdvice implements MethodBeforeAdvice {

@Override

public void before(Method method, Object[] objects, Object o) throws Throwable {

System.out.println("how are you");

}

}

3、配置文件

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:util="http://springframework.org/schema/util"

xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/behttp://ans

http://springframework.org/schema/beans/spring-beans-4.3.xsd http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.3.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd">

&lhttp://t;/beans>

4、测试

public static void main(String[] args) {

ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

Hadoop hadoop= (Hadoop) ctx.getBean("hadoop");

hadoop.eatting();

}

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p"

xmlns:context="http://springframework.org/schema/context"

xmlns:util="http://springframework.org/schema/util"

xmlns:aop="http://springframework.org/schema/aop" xsi:schemaLocation="http://springframework.org/schema/behttp://ans

http://springframework.org/schema/beans/spring-beans-4.3.xsd http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.3.xsd http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.3.xsd

http://springframework.org/schema/aop http://springframework.org/schema/aop/spring-aop-4.3.xsd">

&lhttp://t;/beans>

4、测试

public static void main(String[] args) {

ApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml");

Hadoop hadoop= (Hadoop) ctx.getBean("hadoop");

hadoop.eatting();

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring Cloud Alibaba整合Sentinel的实现步骤
下一篇:SpringCloud Feign转发请求头(防止session失效)的解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~