AOP配置XML
一.概念
- 连接点:
如service中的方法就是连接点 - 切入点
a) Service方法被增强的就是切入点 - 通知
a)
b) 引介 - 目标对象——代理目标对象
- 织入——指的是代理新对象的过程
- 代理——一个类被AOP织入增强后,产生的代理类
- 切面——切入点和通知的结合
AOP的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: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">
<!-- an HTTP Session-scoped bean exposed as a proxy -->
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session">
<!-- instructs the container to proxy the surrounding bean -->
<aop:scoped-proxy/>
</bean>
<!-- a singleton-scoped bean injected with a proxy to the above bean -->
<bean id="userService" class="com.foo.SimpleUserService">
<!-- a reference to the proxied userPreferences bean -->
<property name="userPreferences" ref="userPreferences"/>
</bean>
</beans>
pom.xml配置:spring和aspectj
1. <aop:config >标签表示配置AOP
<aop:aspect > 配置切面
i. Id属性:给切面提供一个唯一标识
ii. ref属性:指定通知类bean的id
a. <aop:before >配置前置通知
-
method属性:通知的方法
-
Pointcut属性 :execution(切入点)
-
Pointcut-ref——应用Pointcut表达式
1.
2. -
通配写法 *表示一个参数 ;… 表示可有可无
1.
2.
3. iii. 数据类型java.lang.String 或int -
<aop: point> ——切入点表达式,改用全局的放到aspect
1.
2. 切面示例
通知使用示例
2.环绕通知
1.xml配置,在aspect里面
2. 方法使用
使用了 Aspectj 包