(1) jdk1.8.0_92 (2) IDEA-2019.3.4 (3) apache-maven-3.5.2
(1) log4j.properties(日志相关配置信息)
log4j.rootLogger=DEBUG,A1 log4j.logger.org.mybatis=DEBUG log4j.appender.A1=org.apache.log4j.ConsoleAppender log4j.appender.A1.layout=org.apache.log4j.PatternLayout log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n(2) applicationContext.xml(Spring实体bean配置文件)
<?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" 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/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 配置注解扫描,作用会自动扫描某包或子包下面含有@Component的类,并作为bean来管理- --> <context:component-scan base-package="com.sou"/> <!--开启aop注解代理机制作用,能自动扫描带有@Aspect的bean,将其作为增强aop配置,有点相当于: Saop:config>--> <aop:aspectj-autoproxy/> </beans>MyAspect.java
package com.sou.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.stereotype.Component; //通知类 @Component("myAspect") @Aspect //是一个切面类,相当于<aop:aspect ref="myAspect"> public class MyAspect { //单独定义切入点(方法名字就是切入点),优点:切入点表达式统一维护了 @Pointcut("bean(*Service)") private void myPointcut() { } // 前置通知 // 参数:连接点,就是可以拦截到的方法(方法和目标的包装类型) // 需求:权限控制(权限不足,抛出异常)、记录方法调用信息日志 @Before("bean(*Service)") //<aop:before method="before" pointcut-ref="myPointcut"/> public void before(JoinPoint joinPoint) { System.out.println("前置增强方法。。。"); // 每次运行的时候,都获取到其它信息 System.out.println("增强的对象:" + joinPoint.getTarget().getClass().getName()); // 获取连接点的签名:即方法 System.out.println("增强的方法:" + joinPoint.getSignature().getName()); if ("find".equals(joinPoint.getSignature().getName())) { //根据方法名字进行匹配,find方法与增强的方法名不一样的话,就没权访问这个方法 throw new RuntimeException("当前用户没有权限执行该方法"); } } //后置通知:会在方法执行之后拦截增强,本方法是增强莫个类的所有方法 //需求:与业务相关的,如网上营业厅查询余额后,自动下发短信。 //参数1:连接点,参数2:返回值类型object,参数名,“随意”,也不能太随意,配置的时候,还要用这个名字 @AfterReturning(value = "target(com.sou.service.impl.UserServiceImpl)", returning = "returnVal") public void afterReturning(JoinPoint joinPoint, Object returnVal) { System.out.println("后置增强方法。。。"); System.out.println("后置通知:系统日志:当前下发了短信的方法是:" + joinPoint.getSignature().getName()); //获取到查询的结果,然后调用发送短信的方法 System.out.println("开始发短信了,短信内容:客户你好,您的余额为:" + returnVal); } //环绕通知:在方法的前后拦截增 // 需求:日志、缓存、权限、性能监控、“事务管理” //有三个特点: //参数: ProceedingJoinPoint正在执行的连接点 //必须抛出一个异常Throwable // * com.sou..*.*(..) 任意子包(.)下面的任意类(. * )下面的任意方法(. * )下面的任意参数的方法(..) // @Around(value= "execution(* com.sou..*.*(..))")//增强任意返回类型的、 com.sou包及其子包的所有类的所有方法,任意参数的方法 // @Around("execution(* com..save(..))")//增强所有的save方法 // @Around("execution(* com..*.find*(..))")/ /增强所有的find开头的方法 //增强UserServiceImpl类中的所有方法。注意,*后面必须有空格,不然会报错 @Around("execution(* com.sou..UserServiceImpl+.*(..))") public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { //在方法之前开启事务 System.out.println("开启了事务。。。"); //目标方法的执行 Object object = proceedingJoinPoint.proceed(); //在目标对象方法执行之后,提交事务 System.out.println("提交了事务。。。"); //要将目标对象返回回去 return object; } //抛出通知:在目标对象的方法发生异常的情况下,拦截增强 //需求:处理异常(-般不可预知),记录日志,通知管理员(短信、邮件) //参数1:连接点信息,参数2:异常, Throwable类型,参数名称,“随意”,配置这个名字 //增强com下及其子包下面的所有bean的类型的类 @AfterThrowing(value = "within(com..*)", throwing = "ex") public void afterThrowing(JoinPoint joinPoint, Throwable ex) { //-旦发生异常,就将异常的信息打印,或发送给管理员 System.out.println("尊敬的管理员,发生了异常了,发生异常的类是: " + joinPoint.getTarget().getClass().getSimpleName() + "发生异常的方法是: " + joinPoint.getSignature().getName() + ",异常信息为: " + ex.getMessage()); } //最终通知 @After("bean(*com)") public void after(JoinPoint joinPoint) { System.out.println("这是最终通知。。。。。"); } }UserService.java
package com.sou.service; import com.sou.pojo.User; public interface UserService { public User login(); public void save(); }UserServiceImpl.java
package com.sou.service.impl; import com.sou.dao.UserDao; import com.sou.pojo.User; import com.sou.service.UserService; import org.springframework.stereotype.Service; import javax.annotation.Resource; @Service("userService") public class UserServiceImpl implements UserService { @Resource(name = "userDao") private UserDao userDao; @Override public User login() { System.out.println("这是UserServiceImpl的登录方法"); userDao.login(); return null; } @Override public void save() { System.out.println("业务层:添加商品异常"); //人为制造异常 int a = 1 / 0; } }UserDao.java
package com.sou.dao; import com.sou.pojo.User; public interface UserDao { public User login(); }UserDaoImpl.java
package com.sou.dao.impl; import com.sou.dao.UserDao; import com.sou.pojo.User; import org.springframework.stereotype.Repository; import javax.annotation.Resource; @Repository("userDao") public class UserDaoImpl implements UserDao { @Override public User login() { System.out.println("这是UserDaoImpl登录方法"); return null; } }######9、UserTest文件 SpringAopTest.java
import com.sou.service.UserService; 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(locations = "classpath:applicationContext.xml") public class SpringAopTest { @Resource(name = "userService") private UserService userService; @Test public void SpringAopTest() { userService.login(); userService.save(); } }