添加Maven依赖:
<!-- aspectj包的依赖 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>1.9.1</version> </dependency> <!-- aspectjrtweaver包的依赖 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.6</version> </dependency>
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" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注册bean --> <bean name="userDao" class="com.xiaobiti.dao.impl.UserDaoImpl"/> <bean name="xmlAdvice" class="com.xiaobiti.demo.xmlAdvice"/> <!-- 配置Spring AOP --> <aop:config> <!-- 指定切入点 --> <aop:pointcut id="pointcut" expression="execution(* com.xiaobiti.dao.impl.UserDaoImpl.*(..))"/> <!-- 指定切面 --> <aop:aspect ref="xmlAdvice"> <!-- 前置通知 --> <aop:before method="before" pointcut-ref="pointcut"/> <!-- 返回通知 --> <aop:after-returning method="afterReturning" pointcut-ref="pointcut"/> <!-- 环绕通知 --> <aop:around method="around" pointcut-ref="pointcut"/> <!-- 异常通知 --> <aop:after-throwing method="afterException" pointcut-ref="pointcut"/> <!-- 后置通知 --> <aop:after method="after" pointcut-ref="pointcut"/> </aop:aspect> </aop:config> </beans>
UserDao.java
package com.xiaobiti.dao; public interface UserDao { public boolean login(String username,String password); public void save(); public void insert(); public void delete(); public void update(); public void select(); }
UserDaoImpl.java
package com.xiaobiti.dao.impl; import com.xiaobiti.dao.UserDao; import com.xiaobiti.entity.User; import org.springframework.stereotype.Repository; import javax.annotation.Resource; @Repository("userDao") public class UserDaoImpl implements UserDao { @Resource(name = "user") private User user; @Override public boolean login(String username, String password) { if (username.equals("admin") && password.equals("1234")){ return true; } return false; } @Override public void save() { System.out.println("执行dao中的方法"); System.out.println(user); } @Override public void insert() { System.out.println("添加用户信息"); } @Override public void delete() { System.out.println("删除用户信息"); } @Override public void update() { System.out.println("修改用户信息"); } @Override public void select() { System.out.println("查询用户信息"); } }
xmlAdvice.java
package com.xiaobiti.demo; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; public class xmlAdvice { //前置通知 public void before(JoinPoint joinpoint){ System.out.println("这是前置通知..."); System.out.println("目标类是:"+joinpoint.getTarget()); System.out.println(",被织入增强的目标的方法:"+joinpoint.getSignature().getName()); } //返回通知 public void afterReturning(JoinPoint joinpoint){ System.out.println("这是返回通知(不出现异常时候调用)..."); System.out.println(",被织入增强的目标的方法:"+joinpoint.getSignature().getName()); } //环绕通知 public Object around(ProceedingJoinPoint point) throws Throwable { System.out.println("这是环绕通知之前的部分..."); //调用目标方法 Object object = point.proceed(); System.out.println("这是环绕通知之后的部分..."); return object; } //异常通知 public void afterException(JoinPoint joinpoint){ System.out.println("这是异常通知..."); } //后置通知 public void after(JoinPoint joinpoint){ System.out.println("这是后置通知..."); } }
运行测试代码:
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); UserDao userDao = applicationContext.getBean("userDao",UserDao.class); userDao.delete(); System.out.println("======================================"); userDao.insert(); System.out.println("======================================"); userDao.update(); System.out.println("======================================"); userDao.select(); }
运行结果:
标签:userDao,Spring,void,System,AOP,println,public,out From: https://www.cnblogs.com/xiaobiti/p/17282499.html