标签:name .. spring void joinPoint Aspect aop println public
1、创建切面类
@Component
@Aspect
public class LogAspect {
@Before(value = "execution(* com.cl.service..*.*(..))")
public void before(JoinPoint joinPoint){
//获取方法名
String name = joinPoint.getSignature().getName();
System.out.println("[ before ]"+name+"执行");
}
@After(value = "execution(* com.cl.service..*.*(..))")
public void after(JoinPoint joinPoint){
//获取方法名
String name = joinPoint.getSignature().getName();
System.out.println("[ after ]"+name+"执行");
}
@AfterReturning(value = "execution(* com.cl.service..*.*(..))",returning = "result")
public void afterReturning(JoinPoint joinPoint,Object result){
//获取方法名
String name = joinPoint.getSignature().getName();
System.out.println("[ afterReturning ]"+name+"方法的结果为:"+result);
}
@AfterThrowing(value = "execution(* com.cl.service..*.*(..))",throwing = "exception")
public void afterThrowing(JoinPoint joinPoint,Exception exception){
//获取方法名
String name = joinPoint.getSignature().getName();
System.out.println("[ afterReturning ]");
exception.printStackTrace();
}
}
@Aspecet 注解
将当前类标记为切面类@Component注解
通过包扫描,将含有此标签的类注入到 IoC
2、配置 bean.xml 文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 扫描 @component、@Repository、@service、@control 等注解,扫描到了之后自动添加到 IoC 容器中 -->
<context:component-scan base-package="com.cl"></context:component-scan>
<!-- 开启 @aspect 注解 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
3、编写业务代码并测试
- 业务接口
public interface IRandomTest {
int runTest();
}
- 业务接口实现
@Component
public class RandomTest implements IRandomTest {
public int runTest() {
System.out.println("执行了业务代码");
return 4;
}
}
- 测试类
public class Text {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("Spring.xml");
IRandomTest randomTest = (IRandomTest) applicationContext.getBean("randomTest");
randomTest.runTest();
}
}
注意点: 因为业务接口实现类使用了 @Component 注解
,包扫描会扫描到这个注解,它会将 RandomTest 自动注入到 IoC 容器中,所以可以在测试类中直接到 IoC 容器中取出 bean 。
标签:name,..,spring,void,joinPoint,Aspect,aop,println,public 来源: https://blog.csdn.net/Start1234567/article/details/112333692
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。