<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
</dependencies>
public class Log {
public void before(){
System.out.println("aop before");
}
}
<bean id="hello" class="org.spring.Hello"></bean>
<bean id="log" class="org.spring.Log"></bean>
<aop:config>
<aop:aspect id="log" ref="log">
<aop:before method="before" pointcut="execution(public void org.spring.Hello.aop())"></aop:before>
</aop:aspect>
</aop:config>
解释
每次执行Hello类中的aop方法都会触发切面Log类中的before方法先执行
@Pointcut("execution(* com.cjm.model..*.*(..))")
切面方法
package org.spring;
public class Log {
public void before(){
System.out.println("aop before");
}
public void after(){
System.out.println("aop after");
}
public void exp(){
System.out.println("aop ex");
}
public void fina(){
System.out.println("aop fina");
}
}
<aop:aspect id="log" ref="log">
<aop:before method="before" pointcut="execution(public void org.spring.Hello.aop())"></aop:before>
<aop:after-returning method="after" pointcut="execution(public void org.spring.Hello.*(..))"></aop:after-returning>
<aop:after-throwing method="exp" pointcut="execution(public void org.spring.Hello.*(..))"></aop:after-throwing>
<aop:after method="fina" pointcut="execution(public void org.spring.Hello.*(..))"></aop:after>
</aop:aspect>
网友评论