首先对于面向切面编程的两个点要清楚:
1.切入点:类中实际被增强的方法称为切入点。
2.通知/增强:增强的逻辑。(比如扩展日志功能)
--通知:前置增强,后置增强,环绕增强
3.切面:把“通知/增强”用到“切入点”当中。
//下面的代码有两个类:Book, MyBook。通过MyBook类的方法增强Book类,Book是切入点。下面首先是不用注解版的代码,手工配置:
package cn.itcast.aop;
public class Book {
public void add(){
System.out.println("book.......");
}
}
package cn.itcast.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyBook {
public void before1(){
System.out.println("前置增强.....");
}
public void after1(){
System.out.println("后置增强.....");
}
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("方法之前");
proceedingJoinPoint.proceed();
System.out.println("方法之后");
}
}
package cn.itcast.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyBook {
public void before1(){
System.out.println("前置增强.....");
}
public void after1(){
System.out.println("后置增强.....");
}
public void around1(ProceedingJoinPoint proceedingJoinPoint) throws Throwable{
System.out.println("方法之前");
proceedingJoinPoint.proceed();
System.out.println("方法之后");
}
}
<?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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- 配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="mybook" class="cn.itcast.aop.MyBook"></bean>
<!-- 配置aop的操作 -->
<aop:config>
<!-- 1.配置切入点(被增强的方法) -->
<aop:pointcut expression="execution(* cn.itcast.aop.Book.*(..))" id="pointcut1"/>
<!-- 2.配置切面
把增强用到具体方法上 -->
<aop:aspect ref="mybook"><!-- 指定增强类 -->
<!-- 配置增强类型
method增强类里面那个方法作为前置增强 -->
<aop:before method="before1" pointcut-ref="pointcut1"/><!-- 将增强中的方法用到指定切入点上 -->
<aop:after method="after1" pointcut-ref="pointcut1"/>
<aop:around method="around1" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>
项目目录截图:
####################################################################################################
//以下是通过注解完成以上同样内容的代码:
package cn.itcast.aop;
public class Book {
public void add(){
System.out.println("book.......");
}
}
package cn.itcast.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class MyBook {
//在方法上面使用注解完成加强
@Before(value="execution(* cn.itcast.aop.Book.*(..))")
public void before1(){
System.out.println("before1....");
}
}
package cn.itcast.aop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestAop {
public static void TestAop(){
ApplicationContext context =
new ClassPathXmlApplicationContext("bean1.xml");
Book book = (Book)context.getBean("book");
book.add();
}
public static void main(String[] args){
TestAop.TestAop();
}
}
<?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" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here -->
<!-- 开启aop的扫描 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置对象 -->
<bean id="book" class="cn.itcast.aop.Book"></bean>
<bean id="mybook" class="cn.itcast.aop.MyBook"></bean>
</beans>
项目截图: