需要的jar包:
xml配置文件方式实现aop操作
①创建被增强接口ThinkDao.java及接口实现ThinkDaoImpl.java(切入点)
1 package com.xml.dao; 2 3 /** 4 *被增强接口 5 */ 6 public interface ThinkDao { 7 8 /** 9 * 被增强方法 10 */ 11 public void thinkDao(); 12 } 13 14 15 16 package com.xml.dao.impl; 17 18 import com.xml.dao.ThinkDao; 19 20 /** 21 *被增强方法实现类 22 */ 23 public class ThinkDaoImpl implements ThinkDao { 24 25 @Override 26 public void thinkDao() { 27 System.out.println("我是被增强方法。。。"); 28 } 29 }
②创建切面类Real.java(切面)
1 package com.xml.dao; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 5 /** 6 * 增强类(通知) 7 * @author bianxc 8 */ 9 public class Real { 10 11 public void before() { 12 System.out.println("前置通知。。。"); 13 } 14 15 public void after() { 16 System.out.println("后置通知。。。"); 17 } 18 19 /** 20 * 环绕增强 21 * @param proceedingJoinPoint 22 * @throws Throwable 23 */ 24 public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 25 System.out.println("方法之前。。。"); 26 proceedingJoinPoint.proceed(); 27 System.out.println("方法之后。。。"); 28 } 29 }
③核心配置文件xmlContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xsi:schemaLocation=" 6 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd "> 8 9 <!-- 实例化ThinkDao类 --> 10 <bean id="thinkDao" class="com.xml.dao.impl.ThinkDaoImpl"/> 11 <!-- 增强类对象 --> 12 <bean id="real" class="com.xml.dao.Real"/> 13 14 <!-- aop操作 --> 15 <aop:config> 16 <!-- 切入点 --> 17 <aop:pointcut expression="execution(* com.xml.dao.impl.ThinkDaoImpl.*(..))" id="pc"/> 18 <!-- 切面 --> 19 <aop:aspect ref="real"> 20 <!-- 前置通知 --> 21 <aop:before method="before" pointcut-ref="pc"/> 22 <!-- 后置通知 --> 23 <aop:after method="after" pointcut-ref="pc"/> 24 <!-- 环绕通知 --> 25 <aop:around method="around" pointcut-ref="pc"/> 26 </aop:aspect> 27 </aop:config> 28 </beans>
④创建测试类TestXml.java
1 package com.xml.test; 2 3 import org.junit.jupiter.api.Test; 4 import org.springframework.context.ApplicationContext; 5 import org.springframework.context.support.ClassPathXmlApplicationContext; 6 7 import com.xml.dao.ThinkDao; 8 9 public class TestXml { 10 11 @Test 12 public void testXml() { 13 ApplicationContext context = new ClassPathXmlApplicationContext("xmlContext.xml"); 14 ThinkDao thinkDao = (ThinkDao) context.getBean("thinkDao"); 15 thinkDao.thinkDao(); 16 } 17 }
注解方式实现aop操作
注:使用注解方式实现aop操作较xm配置文件方式代码简化了很多,减少了配置文件中繁琐的配置;
只需要在配置文件中开启aop的自动代理,和注解的自动扫描就可以了,剩下的全部由注解来实现
①创建被增强接口ThinkDao.java及接口实现ThinkDaoImpl.java(切入点)
1 package com.anno.dao; 2 3 /** 4 *被增强接口 5 */ 6 public interface ThinkDao { 7 8 /** 9 * 被增强方法 10 */ 11 public void thinkDao(); 12 } 13 14 15 16 package com.anno.dao.impl; 17 18 import org.springframework.stereotype.Component; 19 20 import com.anno.dao.ThinkDao; 21 22 /** 23 * 被增强方法实现类 24 */ 25 @Component("thinkDao") 26 public class ThinkDaoImpl implements ThinkDao { 27 28 @Override 29 public void thinkDao() { 30 System.out.println("我是被增强方法。。。"); 31 } 32 33 }
②创建切面类Real.java(切面)
1 package com.anno.dao.impl; 2 3 import org.aspectj.lang.ProceedingJoinPoint; 4 import org.aspectj.lang.annotation.After; 5 import org.aspectj.lang.annotation.Around; 6 import org.aspectj.lang.annotation.Aspect; 7 import org.aspectj.lang.annotation.Before; 8 import org.springframework.stereotype.Component; 9 10 /** 11 * 增强类(通知) 12 * @author bianxc 13 */ 14 @Component("real") 15 @Aspect 16 public class Real { 17 18 // 前置通知 19 @Before("execution(* com.anno.dao.impl.ThinkDaoImpl.*(..))") 20 public void before() { 21 System.out.println("前置通知。。。"); 22 } 23 24 // 后置增强 25 @After("execution(* com.anno.dao.impl.ThinkDaoImpl.*(..))") 26 public void after() { 27 System.out.println("后置通知。。。"); 28 } 29 30 // 环绕增强 31 @Around("execution(* com.anno.dao.impl.ThinkDaoImpl.*(..))") 32 public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable { 33 System.out.println("方法之前。。。"); 34 proceedingJoinPoint.proceed(); 35 System.out.println("方法之后。。。"); 36 } 37 }
③核心配置文件annoContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation=" 7 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> 10 11 <!-- 开启注解扫描 --> 12 <context:component-scan base-package="com.anno.*"></context:component-scan> 13 <!-- 开启自动代理 --> 14 <aop:aspectj-autoproxy/> 15 </beans>
④创建测试类TestAnno.java
1 package com.anno.test; 2 3 import org.junit.Test; 4 import org.junit.runner.RunWith; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.test.context.ContextConfiguration; 7 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 8 9 import com.anno.dao.ThinkDao; 10 11 @ContextConfiguration("classpath:annoContext.xml") 12 @RunWith(SpringJUnit4ClassRunner.class) 13 public class TestAnno { 14 15 @Autowired 16 private ThinkDao thinkDao; 17 18 @Test 19 public void testAnno() { 20 thinkDao.thinkDao(); 21 } 22 }
执行结果:
总结:注解的方式实现aop简化了xml配置文件中臃肿的代码,在企业开发中通常以注解的方式进行开发!