Spring面向切面编程(AOP)简单代码实现

#本文以前置通知(Advice)为例,给出一个spring面向编程(AOP)的简单代码实现。

案例虽简单,但是不简陋,言归正传!开始我们的Demo

##步骤一,创建一个StudentDao接口

###StudentDao.java

		package demo3;
		
		public interface StudentDao {
			    public void find();
			
			    public void delete();
			
			    public void update();
			
			    public void add();
			}

##步骤二,创建StudentDao的实现类StudentDaoImpl

###StudentDaoImpl.java

	package demo3;
	public class StudentDaoImpl implements StudentDao {
		    public void find() {
		        System.out.println("查找。。。");
		    }
		
		    public void delete() {
		        System.out.println("删除。。。");
		    }
		
		    public void update() {
		        System.out.println("更新。。。");
		    }
		
		    public void add() {
		        System.out.println("添加。。。");
		    }
		}

##步骤三,创建代理类MyBeforeAdvice,实现接口MethodBeforeAdvice,因为本文以前置增强方法为例进行说明,读者可以根据具体的逻辑实现不同的接口。

###MyBeforeAdvice.java

	package demo3;
	
	import org.springframework.aop.MethodBeforeAdvice;
	
	import java.lang.reflect.Method;
	
	public class MyBeforeAdvice implements MethodBeforeAdvice {
	
	    public void before(Method method, Object[] objects, Object o) throws Throwable {
	    	//此处是要增强的业务
	        System.out.println("前置增强。。。");
	    }
	}

##步骤四,配置ApplicationContext.xml文件。

###ApplicationContext.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"
	       xsi:schemaLocation="http://www.springframework.org/schema/beans 				  http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	    <!--配置目标类-->
	    <bean id="studentDao" class="demo3.StudentDaoImpl"></bean>
	
	    <!--前置通知类型-->
	    <bean id="mybeforeAdvice" class="demo3.MyBeforeAdvice"></bean>
	
	    <!--Spring的AOP产生代理对象-->
	    <bean id="studentDaoProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
	        <!--目标类-->
	        <property name="target" ref="studentDao"></property>
	
	        <!--实现的接口-->
	        <property name="proxyInterfaces" value="demo3.StudentDao"></property>
	
	        <!--采用拦截的名称-->
	        <property name="interceptorNames" value="mybeforeAdvice"></property>
	
	    </bean>
	</beans>

##步骤五,配置pom.xml文件

##pom.xml

	<dependencies>
    <!--dependency>
      <groupId>com.imooc</groupId>
      <artifactId>[the artifact id of the block to be mounted]</artifactId>
      <version>1.0-SNAPSHOT</version>
    </dependency-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>aopalliance</groupId>
      <artifactId>aopalliance</artifactId>
      <version>1.0</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.7.RELEASE</version>
    </dependency>
  </dependencies>

##步骤六,创建测试类SpringDemo3

##SpringDemo3.java

package 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 {

	//原始的studentDao,无业务增强
    //@Resource(name = "studentDao")
   	
   	//代理之后的studentDaoProxy,增强前置业务
    @Resource(name = "studentDaoProxy")
    private StudentDao studentDao;

    @Test
    public void demo1(){
        studentDao.add();
        studentDao.delete();
        studentDao.find();
        studentDao.update();
    }
}

##结果界面
在这里插入图片描述

##想了解JDK动态代理的可以点以下链接

JDK动态代理的底层实现:https://blog.csdn.net/weixin_44798153/article/details/101687438
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值