导包和约束


基于配置文件的AOP操作
//Book.java
package cn.cstor;
public class Book {
public void show(){
System.out.println("come from Book.show()...");
//int i = 1/0;
}
}
//EnhancedBook.java
package cn.cstor;
import org.aspectj.lang.ProceedingJoinPoint;
public class EnhancedBook {
public void before() {
System.out.println("come from EnhancedBook.before()..");
}
public void after() {
System.out.println("come from EnhancedBook.after()..");
}
public void afterReturnling() {
System.out.println("come from EnhancedBook.afterReturnling()..");
}
public void throwing() {
System.out.println("come from EnhancedBook.throwing()..");
}
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("come from EnhancedBook.around() start...");
joinPoint.proceed();
System.out.println("come from EnhancedBook.around() end...");
}
}
<!-- applicationContext.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<!-- 创建Bean对象 -->
<bean id="book" class="cn.cstor.Book"/>
<bean id="enhancedBook" class="cn.cstor.EnhancedBook"/>
<!-- AOP配置-->
<aop:config>
<!-- 配置切入点 -->
<aop:pointcut expression="execution(* cn.cstor.Book.*(..))" id="pointcut1"/>
<!-- 配置切面 -->
<aop:aspect ref="enhancedBook">
<aop:before method="before" pointcut-ref="pointcut1"/>
<aop:after method="after" pointcut-ref="pointcut1"/>
<aop:after-returning method="afterReturnling" pointcut-ref="pointcut1"/>
<aop:after-throwing method="throwing" pointcut-ref="pointcut1"/>
<aop:around method="around" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
</beans>
//BookTest.java
package cn.cstor;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class BookTest {
@Test
public void f(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
((Book) ac.getBean("book")).show();
}
}
#基于注解的AOP操作
<!-- applicationContext.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="book" class="cn.cstor.Book"/>
<bean id="enhancedBook" class="cn.cstor.EnhancedBook"/>
<!-- 开启aop操作 -->
<aop:aspectj-autoproxy/>
</beans>
//EnhancedBook.java
package cn.cstor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class EnhancedBook {
@Before("execution(* cn.cstor.Book.*(..))")
public void before() {
System.out.println("come from EnhancedBook.before()..");
}
@After("execution(* cn.cstor.Book.*(..))")
public void after() {
System.out.println("come from EnhancedBook.after()..");
}
@AfterReturning("execution(* cn.cstor.Book.*(..))")
public void afterReturnling() {
System.out.println("come from EnhancedBook.afterReturnling()..");
}
@AfterThrowing("execution(* cn.cstor.Book.*(..))")
public void throwing() {
System.out.println("come from EnhancedBook.throwing()..");
}
@Around("execution(* cn.cstor.Book.*(..))")
public void around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("come from EnhancedBook.around() start...");
joinPoint.proceed();
System.out.println("come from EnhancedBook.around() end...");
}
}
本文介绍如何使用Spring框架实现面向切面编程(AOP),包括基于XML配置和注解的方式,展示了如何定义切面、切入点及各类通知类型。
1084

被折叠的 条评论
为什么被折叠?



