1.通過ProxyFactoryBean配置AOP存在的問題
a)這種配置方式是一個過渡方式, 爲了讓大家理解AOP就是對動態代理的具體應用.
b)這種配置方式需要一個切點一個切點的配置, 不靈活, 配置信息越寫越多.
c)使用AspectJ方式進行配置, 可以解決上述問題.
2.AspectJ方式配置AOP
a)AspectJ是一個插件, 需要額外導包: aspectjweaver.jar
b)特點是可以支持通配符配置, *作爲通配符, 可以代表包, 類, 方法…
c)只需要配置一次, 後續所有匹配的位置都將變成切點.
d)需要遵循特定的規範.
3.AspectJ配置步驟
a)導包: aspectjweaver.jar
b)在spring配置文件中引入aop命名空間
c)通過指定的標籤進行aop配置
<?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">
<!--管理service對象-->
<bean id="userService" class="com.bjsxt.service.impl.UserServiceImpl" />
<!--管理通知對象-->
<bean id="txAdvice" class="com.bjsxt.advice.TxAdvice" />
<!--配置aop-->
<aop:config>
<!--配置切點-->
<!--
id: 唯一標識切點
expression: 切點表達式, 讓spring通過這個表達式定位切點
execution(), 固定語法
* 任意類型的方法返回值
包名.*.* 指定包下的任意類的任意方法
(..) 任意數量,任意類型的參數列表
-->
<aop:pointcut id="pc" expression="execution(* com.bjsxt.service.*.*(..))"/>
<!--配置通知-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pc" />
</aop:config>
</beans>