版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BIGSEACOMING/article/details/81041473
<context:annotation-config />
<bean id="executionTimeLoggingSpringAop" class="big.ExecutionTimeLoggingSpringAOP" />
<aop:config>
<aop:pointcut id="executionTimeLoggingPointcut"
expression="execution(public * sayHello(..))" />
<aop:advisor id="executionTimeLoggingAdvisor"
advice-ref="executionTimeLoggingSpringAop"
pointcut-ref="executionTimeLoggingPointcut" />
</aop:config>
在上述代码中ExecutionTimeLoggingSpringAOP为实现了MethodBeforeAdvice, AfterReturningAdvice这两个接口的类,这两个接口分别是在目标方法执行前和后分别定义一个方法用于执行。aop:pointcut定义了切入点和表达式。aop:advisor用于将切入点和通知进行结合。
1.tips:在applicationcontext.getbean(returntype)获取spring上下文的bean,参数是需要返回的bean的类型,参数可以是超类或者接口,但是超类或者接口在spring上下文中的实现bean有且只有一个,如果没有抛出NoSuchBeanDefinitionException异常,如果由多个则抛出bean不唯一异常。在进行注解一个bean时,value值默认为空,bean的名称由类名或方法名代替,首写字母小写。
2.切入点指示符,匹配表达式:类型签名表达式
<aop:pointcut id="executionTimeLoggingPointcut"
expression="within(bean.*)" />
通过使用within可以指定接口,包名,类名类过滤方法。上述则指定了bean包名,将bean包中的所有方法都应用advice通知。
3.使用方法签名表达式关键字execution
4.
<aop:aspectj-autoproxy />
这句代码在xml中进行配置,表示开启切面编程。
5.@aspect在一个类上使用,表示这个类具有aop功能。