Spring AOP Demo(注解和非注解配置) Spring版本5.1.5.RELEASE

2021-04-27 10:13:25 浏览数 (1)

1、引入spring core和spring aop和spring aspect包

代码语言:javascript复制
		5.1.5.RELEASE
	

	
		
		
		    org.springframework
		    spring-webmvc
		    ${spring.version}
		
		
		
		    org.springframework
		    spring-aspects
		    ${spring.version}

2、配置web.xml

代码语言:javascript复制
  springweb
  
  
  
 	 web/index.jsp
  
  
  	
	
		default
		*.js
	
	
		default
		*.css
	
	
		default
		*.html
	

	
  	
	
		contextConfigLocation
		classpath:spring-core.xml
	

	
	
		org.springframework.web.context.ContextLoaderListener
	
	
	
	
		encodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	
	
		encodingFilter
		/*
	

	
	
		dispatcherServlet
		org.springframework.web.servlet.DispatcherServlet
		
			contextConfigLocation
			classpath:spring-mvc.xml
		
		1
	
	
		dispatcherServlet
		/

3、配置spring xml文件 spring-core.xml和spring-aop.xml

spring-core.xml(扫描注解包和启动注解式的声明对AspectJ的支持)

代码语言:javascript复制

spring-aop.xml(非注解的xml配置)

代码语言:javascript复制

4、编写测试类 UserDao.java UserDao Imp.java

代码语言:javascript复制
package com.gxwz.dao;

public interface UserDao {

	public void addUser();
	
	public void deleteUser();
	
	public void inquireUser();
	
	public void updteUser();
	
	public void queryAll();
}
代码语言:javascript复制
package com.gxwz.dao.impl;

import org.springframework.stereotype.Repository;

import com.gxwz.dao.UserDao;

@Repository("userDao")
public class UserDaoImpl implements UserDao{

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

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

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

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

	@Override
	public void queryAll() {
		
	}

}

5、核心类:spring的切面类 MyAspect.java

代码语言:javascript复制
package com.gxwz.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Spring切面类:可以存在多个通知
 * @author com
 */
@Aspect
@Component
class MyAspect{

	
	//配置切入点
	@Pointcut("execution(* com.gxwz.*.*.*(..))")
	
	//使用一个返回值为void、方法体为空的方法命名切入点
	private void myPointCut() {}
	
	/**
	 * 前置通知
	 * @param joinPoint
	 */
	@Before("myPointCut()")
	public void myBefore(JoinPoint joinPoint) {
//		System.out.println("前置通知:模拟执行权限检查.....");
		System.out.println("springaop:aspectj 目标类:" joinPoint.getTarget() " 方法:" joinPoint.getSignature().getName() "()");
//		System.out.println("springaop:aspectj 被植入增强处理的目标方法为:" joinPoint.getSignature().getName());
	}
	
	/**
	 * 后置通知
	 * @param joinPoint
	 */
	@AfterReturning("myPointCut()")
	public void myAfterReturning(JoinPoint joinPoint) {
//		System.out.println("springaop:aspectj 后置通知:模拟记录日志....");
//		System.out.println("springaop:aspectj 被植入处理目标方法为:" joinPoint.getSignature().getName());
	}
	
	/**
	 * 环绕通知
	 */
	@Around("myPointCut()")
	public Object myAround(ProceedingJoinPoint proceedingJoinPoint) {
		System.out.println("springaop:aspectj 环绕开始:执行目标方法之前,模拟开启事务....");
		Object object = null;
		try {
			//执行当前目标方法
			object = proceedingJoinPoint.proceed();
		} catch (Throwable e) {
			e.printStackTrace();
		}
		System.out.println("springaop:aspectj 环绕结束:执行目标方法之后,模拟关闭事务...");
		return object;
	}
	
	/**
	 * 异常通知
	 * @param jointPoint
	 * @param e
	 */
	@AfterThrowing(value = "myPointCut()" , throwing = "e")
	public void myAfterThrowing(JoinPoint jointPoint, Throwable e) {
		System.out.println("springaop:aspectj 异常通知: " e.getMessage());
	}
	
	/**
	 * 最终通知
	 */
	@After("myPointCut()")
	public void myAfter() {
		System.out.println("springaop:aspectj 最终通知:模拟方法结束后释放资源的操作....");
	}
	
}

6、测试类 SpringTest.java

代码语言:javascript复制
package com.gxwz.test;

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

import com.gxwz.dao.UserDao;

public class SpringTest {

	
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("spring-core.xml");
//		ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
		UserDao userDao = (UserDao)context.getBean(UserDao.class);
		userDao.addUser();
//		userDao.deleteUser();
//		userDao.inquireUser();
//		userDao.queryAll();
		
	}
}

7、运行结果

0 人点赞