• 使用Schema配置切面


    除了只用接口和注解定义增强处理,还可以在Spring配置文件中通过aop命名空间将一个普通的JavaBean中的方法声明为增强处理

    1.UserBizLogger

     1 //使用Schema 不用注解,不用实现接口
     2 public class UserBizLogger {
     3     private static final Logger log = Logger.getLogger(UserBizLogger.class);
     4     //前置增强 
     5     public void before(JoinPoint jp){
     6         log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+
     7                 "方法,方法的参数是:"+Arrays.toString(jp.getArgs()));
     8     }
     9     //后置增强
    10     public void afterReturing(JoinPoint jp,Object returnValue){
    11         log.info("调用"+jp.getTarget()+"的"+jp.getSignature().getName()+
    12                 "方法,方法的返回值是:"+returnValue);
    13     }
    14 }

    2.spring配置文件,引入aop命名空间

     1     <bean id="dao" class="com.dao.impl.IUserDaoImpl"></bean>
     2     <bean id="biz" class="com.biz.impl.IUserBizImpl">
     3         <property name="dao" ref="dao"></property>
     4     </bean>
     5     <!-- 声明增强方法所在的类 -->
     6     <bean id="thelogger" class="com.aop.UserBizLogger"></bean>
     7     <!-- 配置切面 -->
     8     <aop:config>
     9     <!-- 定义切入点 -->
    10         <aop:pointcut expression="execution(* com.biz.IUserBiz.* (..))" id="pointcut"/>
    11         <!-- 引入包含增强的bean -->
    12         <aop:aspect ref="thelogger">
    13         <!-- 将before方法定义为前置增强并引入切入点 -->
    14             <aop:before method="before" pointcut-ref="pointcut"/>
    15         <!-- 将afterReturing方法定义为后置增强并引入切入点 -->
    16             <!-- 操作返回值 -->
    17             <aop:after-returning method="afterReturing" pointcut-ref="pointcut" returning="returnValue"/>
    18         </aop:aspect>
    19     </aop:config>

    3.从测试类

     1 /**
     2  * 
     3  * @author Mr
     4  * aop测试类
     5  */
     6 public class Test {
     7 
     8     public static void main(String[] args) {
     9         //解析配置文件
    10         ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
    11         
    12         IUserBiz biz = (IUserBiz) ac.getBean("biz");
    13         User user = new User();
    14         user.setUname("小老虎");
    15         biz.save(user);
    16     }
    17 
    18 }

    4.测试效果

  • 相关阅读:
    Spring Boot----freemark使用
    vue----解决跨域问题
    CSS----精灵图
    Spring MVC----文件上传
    Mybatis----Mybatis使用
    Django url引用异常
    Django ORM异常
    django sqlite3 报错问题
    爬虫 requests的超时和重试
    python 异常 NameError: name 'ModuleNotFoundError' is not defined
  • 原文地址:https://www.cnblogs.com/myhzb/p/7538292.html
Copyright © 2020-2023  润新知