美文网首页
Spring AOP

Spring AOP

作者: Storydo | 来源:发表于2016-11-13 19:13 被阅读62次

AOP使用

在不影响业务层逻辑的情况下,植入其它代码,比如事务、日志、埋点统计之类的。另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar

使用注解形式

1.使用注解@Aspect来定义一个切面,在切面中定义切入点(@Pointcut),通知类型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2.开发需要被拦截的类。
3.将切面配置到xml中,当然,我们也可以使用自动扫描Bean的方式。这样的话,那就交由Spring AOP容器管理。

@Aspect
@Component
public class Log {
@Pointcut("execution(* com.story.*.service..*Service.*(..))")
public void method(){};

/*@Before("execution(* com.story.*.service..*Service.*(..)))")*/
@Before("method()")
public void before() {
    System.out.println("method start");
} 
@After("method()")
public void after() {
    System.out.println("method after");
} 
@AfterReturning("execution(* com.story.*.service..*Service.*(..))")
public void AfterReturning() {
    System.out.println("method AfterReturning");
} 
@AfterThrowing("execution(* com.story.*.service..*Service.*(..))")
public void AfterThrowing() {
    System.out.println("method AfterThrowing");
} 

}
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
 default-lazy-init="true">
   
<!-- enable autowire -->
<!-- Activates scanning of @Autowired -->
<context:annotation-config />

<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="com.story">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

<aop:aspectj-autoproxy/>
</beans>

自动注入

这个不在文件中加注解,个人比较喜欢

<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"
 default-lazy-init="true">
   
<!-- enable autowire -->
<!-- Activates scanning of @Autowired -->
<context:annotation-config />

<!-- enable component scanning (beware that this does not enable mapper scanning!) -->
<context:component-scan base-package="com.yoxnet">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" />
</context:component-scan>

<aop:aspectj-autoproxy/>

<bean id="logsAop" class="com.story.aop.log"></bean>
<aop:config>
    <aop:pointcut expression="execution(* com.story.*.service.*Service*.*(..))" 
    id="servicePointcut"/>
    <aop:aspect id="logAspect" ref="logsAop">
        <aop:before method="before"  pointcut-ref="servicePointcut" />
    </aop:aspect>

</aop:config>
</beans>

这种方式不需要在类中加任何注解,只需要正确指定bean的类名即可。

需要注意的问题

1、AOP原理是基于java的方法代理实现的,类似于OC语言中的通知,就是在指定类的执行前、后或者返回、异常的情况下发出通知,去通知到指定类的指定方法。
2、如果定义的AOP是多个,则会根据定义在xml文件中顺序去通知。
3、如果工程框架中是多个context,就是大家说的双亲上下文,在扫描完service后,又去扫描servlet,则需要分别指定需要扫描的注解,即在扫描Controller时,不要扫描Service,否则会覆盖掉service中的aop,使用其不生效。或者将aop配置放到Controller文件中。
4、通知只有在类调用其它类的public方法才会发出,调用自己的方法不会发出通知。所以可以将方法私有化来避免类中某些方法被代理。

相关文章

网友评论

      本文标题:Spring AOP

      本文链接:https://www.haomeiwen.com/subject/dyecpttx.html