spring中的动态代理和aop的自定义通知

目录

1.spring中的动态代理 

2.aop的自定义通知和配置(before,after,afterReturning,afterThrowing,around)

 

1.spring中的动态代理 

spring中的动态代理和aop的自定义通知_第1张图片

UserService.java:

package com.sikiedu.service;

public interface UserService {

	 void save();
	 void delete();
	 void update();
	 void find();
	
}

UserServicelmpl.java:

package com.sikiedu.service;

public class UserServicelmpl implements UserService {

	@Override
	public void save() {
		System.out.println("save");
	}

	@Override
	public void delete() {
		System.out.println("delete");
	}

	@Override
	public void update() {
		System.out.println("update");
	}

	@Override
	public void find() {
		System.out.println("find");
	}

}

UserServiceProxy.java:

package com.sikiedu.test;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import com.sikiedu.service.UserService;
import com.sikiedu.service.UserServicelmpl;

public class UserServiceProxy {

	public UserService getUserServiceProxy(UserService us){
		
		return (UserService)Proxy.newProxyInstance(UserService.class.getClassLoader(),
				UserServicelmpl.class.getInterfaces(),
				new InvocationHandler() {
					
					@Override
					public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
						System.out.println("开启事务");
						Object invoke=method.invoke(us, args);
						System.out.println("提交/回滚");
						return invoke;
						
					}
				});
	}
}

AopTest.java:

package com.sikiedu.test;

import org.junit.Test;

import com.sikiedu.service.UserService;
import com.sikiedu.service.UserServicelmpl;

public class AopTest {

	@Test
	public void Test(){
		UserServiceProxy usProxy=new UserServiceProxy();
		UserService us=new UserServicelmpl();
		UserService us_PowerUp= usProxy.getUserServiceProxy(us);
		us_PowerUp.find();
	}
}

运行结果:

2.aop的自定义通知和配置(before,after,afterReturning,afterThrowing,around)

导入包:

spring中的动态代理和aop的自定义通知_第2张图片

导入aop约束:

spring中的动态代理和aop的自定义通知_第3张图片

新建applicationContext.xml,并右键点击:【openwith】-【other...】-【spring config editor】-【namespace】-【勾选aop,beans】

spring中的动态代理和aop的自定义通知_第4张图片

然后applicationContext.xml就有如下约束了:



spring中的动态代理和aop的自定义通知_第5张图片

MyAdvice.java:

package ssm.sikiedu.aop;

import org.aspectj.lang.ProceedingJoinPoint;

public class MyAdvice {

	//before 前置通知 在目标方法前调用
	public void before(){
		System.out.println("before");
	}
	
	//after 最终通知(后置通知) 在目标方法后调用,无论是否出现异常都会执行 类似finally
	public void after(){
		System.out.println("after");
	}
	
	//afterReturning 成功通知(后置通知) 在目标方法执行后,并且执行成功才调用,如果方法出现异常则不调用
	public void afterReturning(){
		System.out.println("afterReturning");
	}
	
	//afterThrowing 异常通知(后置通知) 在目标方法执行出现异常的时候才会调用
	public void afterThrowing(){
		System.out.println("afterThrowing");
	}
	
	//环绕通知
	public Object around(ProceedingJoinPoint pjp) throws Throwable{
		
		System.out.println("around before");
		Object proceed = pjp.proceed();
		System.out.println("around after");
		return proceed;
	}
}

applicationContext.xml:





	
	

	
	
	




		
		
		

		

			
			

			
			

			
			

			
			

			
		
	

AopTest.java:

package com.sikiedu.test;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sikiedu.service.UserService;
import com.sikiedu.service.UserServicelmpl;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class AopTest {

	@Resource(name="userService")
	UserService us;
	@Test
	public void Test2(){
		us.delete();
	}
}
	

运行结果:(下面是正常运行的结果,但是我并没有运行成功

spring中的动态代理和aop的自定义通知_第6张图片

你可能感兴趣的:(Spring)