javaweb中声明式 spring- AOP

AOP的相关的文章介绍:
javaweb中Aop(jdk动态代理)https://blog.csdn.net/weixin_43319279/article/details/103125051
javaweb中CGLIB动态代理
https://blog.csdn.net/weixin_43319279/article/details/103135053

spring-AOP是由spring中的
org.springframework.aop.framework.ProxyFactoryBean包来实现的。

创建接口类:

 package com.springAoptest;
public interface CustomerDao {

	void add();

	void update();

	void delete();

	void find();

}

接口实现类:

package com.springAoptest;

public class CustomeDaoImpl  implements CustomerDao{
	public void add(){
		System.out.println("添加");		
	}
	public void update(){
		System.out.println("更新");		
	}public void delete(){
		System.out.println("删除");		
	}public void find(){
		System.out.println("查找");		
	}
}

切面类:

package com.springAoptest;

public class CustomeDaoImpl  implements CustomerDao{
	public void add(){
		System.out.println("添加");		
	}
	public void update(){
		System.out.println("更新");		
	}public void delete(){
		System.out.println("删除");		
	}public void find(){
		System.out.println("查找");		
	}
}

创建配置spring-aop-test.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"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
	<bean id="customerDao"  class="com.springAoptest.CustomeDaoImpl"/>
	<bean id="myAspect" class="com.springAoptest.MyAspect" />
	<bean id="customerDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
	
	<property  name="proxyInterfaces"  value="com.springAoptest.CustomerDao" />
	
	<property name="target"  ref="customerDao"/> 
	
	<property name="interceptorNames" value="myAspect"/>
	
	<property name="proxyTargetClass" value="true"/>
	</bean>
</beans>

配置属性解析:

<property  name="proxyInterfaces"  value="com.springAoptest.CustomerDao" />
proxyInterfaces代理要实现的接口
<property name="target"  ref="customerDao"/> 、
target属性:代理的目标对象
<property name="interceptorNames" value="myAspect"/>` 
interceptorNames需要植入目标的advice。

测试类:

package com.springAoptest;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Demo {
	public static void main(String[] args) {
		String xmlPath = "spring-aop-test.xml";
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
		CustomerDao customerDao = (CustomerDao) applicationContext.getBean("customerDaoProxy");
		customerDao.add();
		customerDao.update();
		customerDao.delete();
		customerDao.find();
	}
}

运行的结果:
在这里插入图片描述

发布了80 篇原创文章 · 获赞 15 · 访问量 1867

猜你喜欢

转载自blog.csdn.net/weixin_43319279/article/details/103138788