spring -aop

    xiaoxiao2021-03-25  94

    ---------------------------------------------------------------方式------------------------------------------------------------.AOP(Aspect Oriented Programming)

    两种方式    ①.注解 步骤:1.引入相关jar包 commons-logging-1.0.4.jar spring-aop-4.0.1.RELEASE.jar spring-beans-4.0.1.RELEASE.jar spring-context-4.0.1.RELEASE.jar spring-core-4.0.1.RELEASE.jar spring-expression-4.0.1.RELEASE.jar aspectjrt.jar aspectjweaver-1.7.4.jar com.springsource.org.aopalliance-1.0.0.jar      2.定义切面类(该类要能被context:component-scan扫描到才行) 切面类中添加@Aspect,@Component注解标识符      3.在切面类中添加织入的方法(切点) A:@Before("execution(public void com.communityhealth.service..*.save(*))")//@Before在指定方法之前执行 public void beforeSave() { //要织入的逻辑 System.out.println("serviceImpl 即将调用 save 方法。。。"); } B:@Pointcut("execution(public void com.communityhealth.service..*.save(*))")           public void savePointcut() {}   @Before("savePointcut()") public void beforeSave() { //........ System.out.println("serviceImpl 即将调用 save 方法。。。"); }     ②.XML配置 步骤:1.引入相关jar包 commons-logging-1.0.4.jar spring-aop-4.0.1.RELEASE.jar spring-beans-4.0.1.RELEASE.jar spring-context-4.0.1.RELEASE.jar spring-core-4.0.1.RELEASE.jar spring-expression-4.0.1.RELEASE.jar aspectjrt.jar aspectjweaver-1.7.4.jar com.springsource.org.aopalliance-1.0.0.jar      2.定义切面类,以及待织入的方法beforeSave      3.在beans.xml配置定义好的切面类(实例化) <bean id="b" class="com.communityhealth.interceptor.SaveInterceptor"></bean> -->      4.配置切面 <aop:config> <!-- <aop:pointcut expression="execution(public void com.communityhealth.service..*.save(*))"  id="apointcut"/>定义切点 --> <aop:aspect ref="b"> <aop:before method="beforeSave" pointcut="execution(public void com.communityhealth.service..*.save(*))"/><!-- pointcut-ref="apointcut" --> </aop:aspect> </aop:config> --------------------------------------------------------------------------------------代码-----------------------------------------------------------------------------------

    package com.communityhealth.interceptor; import org.aspectj.lang.annotation.AfterReturning; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.springframework.stereotype.Component; @Aspect @Component public class SaveInterceptor { /** * aop注解的第二种形式 */ @Pointcut("execution(public void com.communityhealth.service..*.save(*))") public void savePointcut() {} @Before("savePointcut()") public void beforeSave() { //........ System.out.println("serviceImpl 即将调用 save 方法。。。"); } @Before("savePointcut()") public void beforeDaoSave() { System.out.println("UserDaoImpl 即将调用save 方法"); }

    ------------配置src 下面的beans-----------

     <?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-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd         http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  <!-- 识别我们的注解标示 --> <context:component-scan base-package="com.communityhealth"></context:component-scan> <aop:aspectj-autoproxy/> </beans>

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- public class SaveInterceptor { /** * aop注解的第一种形式 */ // @Before("execution(public void com.communityhealth.service..*.save(*))") // public void beforeSave() { // System.out.println("serviceImpl 即将调用 save 方法。。。"); // } // // @AfterReturning("execution(public void com.communityhealth.service.impl.UserServiceImpl.save(com.communityhealth.model.User))") // public void afterSave(){ // System.out.println("serviceImpl 调用 save 方法完成了!"); // } // // @Before("execution(public void com.communityhealth.dao.impl.UserDaoImpl.saveUser(com.communityhealth.model.User))") // public void beforeDaoSave() { // System.out.println("UserDaoImpl 即将调用saveUser 方法"); // } } ---------------------------配置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: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-4.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-4.0.xsd         http://www.springframework.org/schema/aop          http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">  <!-- 识别我们的注解标示 --> <context:component-scan base-package="com.communityhealth"></context:component-scan> <!-- 实例化切面类 <bean id="saveInterceptor" class="com.communityhealth.interceptor.SaveInterceptor"></bean> --> <aop:config> <!-- <aop:pointcut expression="execution(public void com.communityhealth.service..*.save(*))"  id="apointcut"/>定义切点 --> <aop:aspect ref="b"> <aop:before method="beforeSave" pointcut="execution(public void com.communityhealth.service..*.save(*))"/><!-- pointcut-ref="apointcut" --> </aop:aspect> </aop:config> </beans>

    转载请注明原文地址: https://ju.6miu.com/read-15856.html

    最新回复(0)