1AOP基础知识
2AOP底层实现 JDK动态代理
2AOP底层实现 CGLIB生成代理
SpringAOP 生成代理
再Maven pom.xml中加入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
SpringAOP 一般切面代码
在pom中引入Spring-test
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.1.RELEASE</version>
</dependency>
1.applicationContext.xml配置
<!--配置目标类=======================-->
<bean id="studentDao" class="AOP.demo1.demo3.StudentDaoImpl"/>
<!--前置通知类型=====================-->
<bean id="myBeforeAdvice" class="AOP.demo1.demo3.MyBeforeAdvice"/>
<!--Spring的AOP 产生代理对象-->
<bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!--配置目标类-->
<property name="target" ref="studentDao"/>
<!--实现的接口-->
<property name="proxyInterfaces" value="AOP.demo1.demo3.StudentDao"/>
<!--采用拦截的名称-->
<property name="interceptorNames" value="myBeforeAdvice"/>
<property name="optimize" value="true"></property>
</bean>
2.Test
package AOP.demo1.demo3;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class SpringDemo3 {
// @Resource(name="studentDao")
@Resource(name="studentDaoProxy")
private StudentDao studentDao;
@Test
public void demo1(){
studentDao.find();
studentDao.save();
studentDao.update();
studentDao.delete();
}
}
3.MyBeforeAdvice 前置 通知
package AOP.demo1.demo3;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("前置增强======================");
}
}
4实体类分为 接口Dao 和实现 类 StudentImpl
public interface StudentDao {
public void find();
public void save();
public void update();
public void delete();
}
public class StudentDaoImpl implements StudentDao {
public void find() {
System.out.println("学生查询...");
}
public void save() {
System.out.println("学生保存...");
}
public void update() {
System.out.println("学生修改...");
}
public void delete() {
System.out.println("学生删除...");
}
}
**补充**
默认是JDK动态代理 在applicationContext.xml的
Spring的AOP 产生代理对象 添加
<!-- 强制使用CGLIB-->
<property name="optimize" value="true"></property>
带有切入点的切面
## 环绕切面
自动代理
BeanNameAutoProxyCreator
benaNames value 为上面类的bean 配置 名*xx 就value=“xx”
以前是 装配完再使用 现在 是给类 对象的上面就是原本的 类bean定义 不在Proxy 在下面的自动给代理了
public class SpringDemo5 {
@Resource(name="studentBean")
private StudentDao studentDao;
@Resource(name="customerBean")
private CustomerDao customerDao;
@Test
public void demo1(){
studentDao.find();
studentDao.save();
studentDao.update();
studentDao.delete();
customerDao.find();
customerDao.save();
customerDao.update();
customerDao.delete();
}
}
DefaultAdvisorAutoProxyCreator
1.xml