AOP使用
在不影響業(yè)務(wù)層邏輯的情況下,植入其它代碼,比如事務(wù)、日志、埋點(diǎn)統(tǒng)計(jì)之類的。另外需要引用 aspectJ 的 jar 包: aspectjweaver.jar aspectjrt.jar
使用注解形式
1.使用注解@Aspect來(lái)定義一個(gè)切面,在切面中定義切入點(diǎn)(@Pointcut),通知類型(@Before, @AfterReturning,@After,@AfterThrowing,@Around).
2.開(kāi)發(fā)需要被攔截的類。
3.將切面配置到xml中,當(dāng)然,我們也可以使用自動(dòng)掃描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>
自動(dòng)注入
這個(gè)不在文件中加注解,個(gè)人比較喜歡
<?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的類名即可。
需要注意的問(wèn)題
1、AOP原理是基于java的方法代理實(shí)現(xiàn)的,類似于OC語(yǔ)言中的通知,就是在指定類的執(zhí)行前、后或者返回、異常的情況下發(fā)出通知,去通知到指定類的指定方法。
2、如果定義的AOP是多個(gè),則會(huì)根據(jù)定義在xml文件中順序去通知。
3、如果工程框架中是多個(gè)context,就是大家說(shuō)的雙親上下文,在掃描完service后,又去掃描servlet,則需要分別指定需要掃描的注解,即在掃描Controller時(shí),不要掃描Service,否則會(huì)覆蓋掉service中的aop,使用其不生效?;蛘邔op配置放到Controller文件中。
4、通知只有在類調(diào)用其它類的public方法才會(huì)發(fā)出,調(diào)用自己的方法不會(huì)發(fā)出通知。所以可以將方法私有化來(lái)避免類中某些方法被代理。