---恢復內容開始---
註解配置AOP
項目路徑:E:\JavaWebSrc\FirstSpringAOP
1:接口代碼
接口爲 IPerson ,接口不需要寫註釋
public interface IPerson { public void sayNmae(); public void introduceOneSelf(); }
2:實體類代碼
student繼承了IPerson接口
//將student實例化。裝配到spring容器中
@Component("student") public class Student implements IPerson { @Value(value = "1") private int id; @Value(value = "余文輝") private String name; @Value(value = "男") private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } }
3:切面類
這裏使用的註解需要導入(aopaliance.jar和 aspectjweaver.jar 兩個jar包)
//切面聲明 @Aspect //裝配到spring容器中 @Component public class LoggingAspect { // 聲明切入點 聲明切入點表達式 @Pointcut("execution(* com.yuwenhui.annotation.Student.* (..))") public void JoinPointExpecssion(){}; //前置通知 @Before("JoinPointExpecssion()") public void beforMethod(JoinPoint joinPoint){ // 獲得方法名 String name = joinPoint.getSignature().getName(); // 獲得參數列表,再將參數數組轉換成List集合 List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("前置通知"); System.out.println("參數:"+args); } //後置通知 @After("JoinPointExpecssion()") public void afterMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("後置通知"); System.out.println("參數:"+args); } // 返回通知,在方法正常結束的時候返回,且 必須要有一個返回值 @AfterReturning(value = "JoinPointExpecssion()",returning = "result") public void afterMethodRetuning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("返回通知"+ result); } // 異常通知 ,將在方法拋出異常時觸 @AfterThrowing(value = "JoinPointExpecssion()",throwing ="e") public void afterThrowing(JoinPoint joinPoint,Exception e){ String name = joinPoint.getSignature().getName(); System.out.println("返回通知"); System.out.println("異常信息"+e); } }
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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 掃描指定包下注釋 -->
<context:component-scan base-package="com.yuwenhui.annotation"></context:component-scan>
<!-- 聲明Aspect配置 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
測試類
public class TestAop { @Test public void testAop(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); IPerson student= (IPerson) applicationContext.getBean("student"); student.sayNmae(); student.introduceOneSelf(); } }
XML配置AOP
1:接口配置
不管是XML配置還是annotation配置,接口類都不需要改變
public interface IPerson { public void sayNmae(); public void introduceOneSelf(); }
2:實體類配置
public class Student implements IPerson { private int id; private String name; private String sex; @Override public void sayNmae() { System.out.println(this.name); } @Override public void introduceOneSelf() { System.out.println(this.toString()); } @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", sex='" + sex + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } }
3:切面類
public class LoggingAspect { public void JoinPointExpecssion(){}; public void beforMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("前置通知"); System.out.println("參數:"+args); } public void afterMethod(JoinPoint joinPoint){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("後置通知"); System.out.println("參數:"+args); } public void afterMethodRetuning(JoinPoint joinPoint,Object result){ String name = joinPoint.getSignature().getName(); List<Object> args = Arrays.asList(joinPoint.getArgs()); System.out.println("返回通知"+ result); } public void afterThrowing(JoinPoint joinPoint,Exception e){ String name = joinPoint.getSignature().getName(); System.out.println("返回通知"); System.out.println("異常信息"+e); } }
4:測試類
public class TestXml { @Test public void testAop(){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-beanxml.xml"); IPerson student= (IPerson) applicationContext.getBean("student"); student.sayNmae(); student.introduceOneSelf(); } }
5:配置文件
需要格外注意
<?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-4.1.xsd"> <!-- 需要注意xml頭,不是IDEA自動生成的 --> <!-- 配置實體類,併爲相關字段初始化 --> <bean id="student" class="com.yuwenhui.xml.Student"> <property name="name" value="余文輝"></property> <property name="id" value="1"></property> <property name="sex" value="男"></property> </bean> <!-- 聲明切面類,這裏只是聲明爲普通的bean類 --> <bean id="loggingAspect" class="com.yuwenhui.xml.LoggingAspect"></bean> <!-- 配置aop --> <aop:config> <!-- 配置切點表達式 --> <aop:pointcut id="pointcut" expression="execution(* com.yuwenhui.xml.Student.* (..))"/> <!-- 這裏纔是真正的聲明切面 --> <aop:aspect ref="loggingAspect"> <!--前置通知--> <aop:before method="beforMethod" pointcut-ref="pointcut"/> <!--後置通知--> <aop:after method="afterMethod" pointcut-ref="pointcut"/> <!--返回通知--> <aop:after-returning method="afterMethodRetuning" pointcut-ref="pointcut" returning="result"/> <!--異常通知--> <aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/> </aop:aspect> </aop:config> </beans>
---恢復內容結束---