• 第4章 基于注解开发Spring AOP


    目录

    4-1 基于注解开发Spring AOP

    4-3 自由编程


    ​​​​​​​4-1 基于注解开发Spring AOP

     实例

     这个训练素材基础上加入注解

    设置依赖

     打开xml增加注解设置

     

     这样就不用zaixml中在进行配置了

    此注解说明当前对象用于持久化

     

     

     

     因为service依赖于dao要对其进行注入

    两种注入方式

    会将private改为public直接对其赋值

    或者采用set方法对这个属性进行设置

     userservice同上

     新增方法切面类

     

     方法同,如何配置切面类呢?

     这样ioc就会对她进行实例化和管理

     

     配置完成

    创建springApplication

     睡眠三秒

    调用

     

     说明aop配置已生效

    4-3 自由编程

     

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    3. xmlns:context="http://www.springframework.org/schema/context"
    4. xmlns:aop="http://www.springframework.org/schema/aop"
    5. xmlns="http://www.springframework.org/schema/beans"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans
    7. http://www.springframework.org/schema/beans/spring-beans.xsd
    8. http://www.springframework.org/schema/context
    9. http://www.springframework.org/schema/context/spring-context.xsd
    10. http://www.springframework.org/schema/aop
    11. http://www.springframework.org/schema/aop/spring-aop.xsd">
    12. <context:component-scan base-package="com.imooc"/>
    13. <aop:aspectj-autoproxy/>
    14. beans>
    15. package com.imooc.spring.aop.dao;
    16. import org.springframework.stereotype.Repository;
    17. /**
    18. * 书店表dao
    19. */
    20. @Repository
    21. public class BookShop {
    22. public void sellingBooks(){
    23. System.out.println("卖出一本java基础书籍");
    24. }
    25. }
    26. package com.imooc.spring.aop.aspect;
    27. import org.aspectj.lang.annotation.After;
    28. import org.aspectj.lang.annotation.Aspect;
    29. import org.aspectj.lang.annotation.Before;
    30. import org.springframework.stereotype.Component;
    31. @Component
    32. @Aspect
    33. public class MethodPro {
    34. @Before("execution(* com.imooc..*.*(..))")
    35. public void preSales(){
    36. System.out.println("=========售前服务=========");
    37. }
    38. @After(("execution(* com.imooc..*.*(..))"))
    39. public void afterSale(){
    40. System.out.println("=========售后服务==========");
    41. }
    42. }
    43. package com.imooc.spring.aop;
    44. import com.imooc.spring.aop.dao.BookShop;
    45. import org.springframework.context.ApplicationContext;
    46. import org.springframework.context.support.ClassPathXmlApplicationContext;
    47. public class SpringApplication {
    48. public static void main(String[] args) {
    49. ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    50. BookShop bookShop = context.getBean("bookShop", BookShop.class);
    51. bookShop.sellingBooks();
    52. }
    53. }

  • 相关阅读:
    mysql的存储过程
    开启Windows11 PC无线热点功能
    Requests爬虫方法
    CVE-2019-1388 UAC提权实战
    SAP UI5 应用开发教程之一百零二 - SAP UI5 应用的打印(Print)功能实现详解试读版
    题目 1071:阶乘公式求值
    PostgreSQL Array 数组类型与 FreeSql 打出一套【组合拳】
    科技资讯|Vision Pro头显无损音频仅限USB-C AirPods Pro 2耳机
    AMBA总线协议之AHB学习记录(2)—ahb_bus的测试(附testbench代码)
    【Java面试】第三章:P6级面试
  • 原文地址:https://blog.csdn.net/lonelyneet/article/details/125909250