1. 程式人生 > >萬用字元的匹配很全面, 但無法找到元素 'aop:aspectj-autoproxy' 的宣告

萬用字元的匹配很全面, 但無法找到元素 'aop:aspectj-autoproxy' 的宣告

因為專案需要,想使用spring的註解方法的AOP切面程式設計,在xml配置檔案中加入<aop:aspectj-autoproxy proxy-target-class="true"/> 之後,專案啟動報文章標題的錯誤。

檢查原因是忘記引用AOP的檔案。

xml檔案的 標頭檔案中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

兩段紅色文字,加上即可。

附屬:aop想要切面controller,必須在掃描controller的xml中,引入<aop:aspectj-autoproxy proxy-target-class="true"/>

通常大家會把 controllers 定義在dispatch-servlet.xml 或者 xxx-servlet.xml 這樣的配置檔案中,但是把自定義的切面放在spring的主配置檔案 applicationContext.xml中。這樣子導致controller和你的切面不在同一個context中,從而你的切面類邏輯不會攔截對應的controller