Spring之aop实现方式

基于Schema-based方式实现

在这里插入图片描述

前置通知

创建一个接口

package com.zhouym.schema_based;

public interface ServiceBean {
   
	String doSome();
	String say();
}

创建目标实现类

package com.zhouym.schema_based;

public class ServiceBeanImpl implements ServiceBean {
   

	@Override
	public String doSome() {
   
		System.out.println("doSome方法");
		return "hello";
	}

	@Override
	public String say() {
   
		System.out.println("say方法");
		return null;
	}

}

创建切面类

package com.zhouym.schema_based;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
//定义一个切面类实现前置通知类
public class MyMethodBeforeAdvice implements MethodBeforeAdvice{
   

	@Override
	public void before(Method method, Object[] args, Object target) throws Throwable {
   
		System.out.println("前置通知方法!!!");
		
	}	
	
}

xml文件配置,需导入aop的jar包

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	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/context 
		http://www.springframework.org/schema/context/spring-context-4.3.xsd
		http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
		
		<!-- 注册目标类 -->
		<bean class="com.zhouym.schema_based.ServiceBeanImpl" id="serviceBeanImpl"></bean>
		<!-- 注册前置通知 -->
		<bean class="com.zhouym.schema_based.MyMethodBeforeAdvice" id="myMethodBeforeAdvice"></bean>
		
		<!-- 注册代理类 这里引入的classpath是ProxyFactoryBean,不要引入ProxyFactory,否则会create error bean -->
		<bean class="org.springframework.aop.framework.ProxyFactoryBean" id="proxyFactoryBean">
			<!-- 指定目标对象 -->
			<property name="target" ref="serviceBeanImpl"></property>
			<!-- 指定目标类实现的的所有接口 -->
			<property name="interfaces" value="com.zhouym.schema_based.ServiceBean"></property>
			<!-- 指定切面 -->
			<property name="interceptorNames">
				<list>
					<!-- 对应前置通知的id属性值 -->
					<value>myMethodBeforeAdvice</value>
				</list>
				
			</property>
		</bean>
		
</beans>

测试类

package com.zhouym.JunitTest;

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

import com.zhouym.schema_based.ServiceBean;

public class Schema_Based_Test {
   

	@Test
	public void test() {
   
		ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
		//指定代理类的id以及接口类对象
		ServiceBean sb = ac.getBean("proxyFactoryBean",ServiceBean.class);
		sb.doSome();
		sb.say();
		
	}

}

测试结果
在这里插入图片描述

后置通知

接口,以及实现类跟前置通知的一样,后置通知我们需要实现AfterReturningAdvice接口

package com.zhouym.schema_based;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class MyAfterReturningAdvice implements AfterReturningAdvice {
   

	@Override
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
   
		System.out.println("后置通知方法");

	}

}

xml配置文件
在这里插入图片描述
测试类
在这里插入图片描述
测试结果
在这里插入图片描述

环绕通知

接口和实现类跟上面的一样,实现MethodReturningAdvice接口

package com.zhouym.schema_based;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class 
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值