• Spring 02


    AOP 操作的准备
    1,Spring框架一般都是基于AspectJ实现AOP操作
    什么是AspectJ?
    AspectJ 不是Spring组成部分,独立Aop框架,一般把AspectJ 和Spring 框架一起使用,进行AOP操作
    基于AspectJ实现AOP操作
    1,基于xml配置文件实现
    2,基于注解方式实现
    3,在项目工程里引入AOP相关依赖
    4,切入点表达式
    1,切入点表达式作用?
    知道对哪个类里的哪个方法进行增强
    2,语法结构?
    execution([权限修饰符][返回类型][类全路径][方法名称]([参数列表]))

    进行通知的配置
    1,在SPring 配置文件中,开启扫描注解

    1. "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:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    9. <context:component-scan base-package="test2">context:component-scan>
    10. <aop:aspectj-autoproxy>aop:aspectj-autoproxy>
    11. beans>


    2,使用注解创建User 和UserProxy对象
    3,在增强类上面添加注解@Aspect

    1. package test2;
    2. import org.springframework.stereotype.Component;
    3. @Component
    4. public class User {
    5. public void add(){
    6. System.out.println("原方法");
    7. int a = 10/0;
    8. }
    9. }
    10. =========================================
    11. package test2;
    12. import org.aspectj.lang.ProceedingJoinPoint;
    13. import org.aspectj.lang.annotation.*;
    14. import org.springframework.stereotype.Component;
    15. @Component
    16. @Aspect //生成代理对象
    17. public class UserProxy {
    18. //添加通知类型注解
    19. @Before(value = "execution(* test2.User.add(..))")
    20. public void before(){
    21. System.out.println("原方法之前的功能 before");
    22. }
    23. // 不管有没有异常都执行
    24. @After(value = "execution(* test2.User.add(..))")
    25. public void after(){
    26. System.out.println("原方法后的功能 after");
    27. }
    28. //在方法返回值之后执行
    29. // 有异常就不执行
    30. @AfterReturning(value = "execution(* test2.User.add(..))")
    31. public void AfterReturning(){
    32. System.out.println("AfterReturning");
    33. }
    34. //异常通知
    35. @AfterThrowing(value = "execution(* test2.User.add(..))")
    36. public void AfterThrowing(){
    37. System.out.println("原方法出现异常时的功能 AfterThrowing");
    38. }
    39. //在原方法之前之后都执行
    40. @Around(value = "execution(* test2.User.add(..))")
    41. public void Around(ProceedingJoinPoint prceedingJoinPoint) throws Throwable {
    42. System.out.println("环绕之前 Around");
    43. prceedingJoinPoint.proceed();
    44. System.out.println("环绕之后 Around");
    45. }
    46. }
    47. ======================================================


    4, 在 Spring 配置文件中开启生成代理对象
     

    1. package test2;
    2. import org.junit.Test;
    3. import org.springframework.context.ApplicationContext;
    4. import org.springframework.context.support.ClassPathXmlApplicationContext;
    5. public class TestAop {
    6. // @Test
    7. // public void test1(){
    8. // ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    9. // User user = context.getBean("user",User.class);
    10. // user.add();
    11. // }
    12. public static void main(String[] args) {
    13. ApplicationContext context = new ClassPathXmlApplicationContext("bean3.xml");
    14. User user = context.getBean("user",User.class);
    15. user.add();
    16. }
    17. }
    18. /*
    19. * 无异常情况
    20. 环绕之前 Around
    21. 原方法之前的功能 before
    22. 原方法
    23. 环绕之后 Around
    24. 原方法后的功能 after
    25. AfterReturning
    26. */
    27. /*
    28. * 有异常情况
    29. 环绕之前 Around
    30. 原方法之前的功能 before
    31. 原方法
    32. 原方法后的功能 after
    33. 原方法出现异常时的功能 AfterThrowing
    34. */

    相同的切入点抽取 

    1. @Pointcut(value = "execution(* test2.User.add(..))")
    2. public void pointdemo(){}
    3. @Before(value = "pointdemo()")
    4. public void before(){
    5. System.out.println("原方法之前的功能 before");
    6. }

    有多个增强类对同一个方法进行增强,设置增强类优先级
    在增强类上面添加注解@order(数字类型值) ,数字类型值越小优先级越高

    1. @Component
    2. @Aspect
    3. @Order(1)
    4. public class User2Proxy {
    5. @Before(value = "execution(* test2.User.add(..))")
    6. public void before(){
    7. System.out.println("order=1 before");
    8. }
    9. }
    10. ========================
    11. @Component
    12. @Aspect //生成代理对象
    13. @Order(3)
    14. public class UserProxy {
    15. @Pointcut(value = "execution(* test2.User.add(..))")
    16. public void pointdemo(){}
    17. @Before(value = "pointdemo()")
    18. public void before(){
    19. System.out.println("原方法之前的功能 before");
    20. }

    基于配置方式:

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    9. "user" class="test3.User">
    10. "userProxy" class="test3.UserProxy">
    11. "p" expression="execution(* test3.User.buy(..))"/>
    12. "userProxy">
    13. "before" pointcut-ref="p"/>

    完全使用注解开发

     jdbcTemplate
    1,Spring 框架对jdbc进行封装,使用jdbcTemplate方便实现对数据库操作
    2,准备工作
    引入相关jar包
    在spring配置文件配置数据库连接池

    3,配置jdbcTemplate对象,注入DataSource

    1. "1.0" encoding="UTF-8"?>
    2. "http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:aop="http://www.springframework.org/schema/aop"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    7. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    8. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    9. package="test3">
    10. "dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    11. "driverClassName" value="com.mysql.jdbc.Driver">
    12. "url" value="jdbc:mysql://localhost:3306/catering_management">
    13. "username" value="root">
    14. "password" value="root">
    15. "jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    16. "dataSource" ref="dataSource">


    4,创建service,dao类,在dao注入jdbcTemplate对象

     

    1. ** dao类
    2. =========================
    3. package test3.dao.daoImpl;
    4. import org.springframework.beans.factory.annotation.Autowired;
    5. import org.springframework.context.ApplicationContext;
    6. import org.springframework.context.support.ClassPathXmlApplicationContext;
    7. import org.springframework.jdbc.core.JdbcTemplate;
    8. import org.springframework.stereotype.Repository;
    9. import test3.entity.Grocers;
    10. @Repository
    11. public class GrocersDaoImpl {
    12. @Autowired
    13. private JdbcTemplate jdbcTemplate;
    14. public void add(Grocers grocers){
    15. String sql = "INSERT INTO grocers VALUES (?,?,?,?,?)";
    16. jdbcTemplate.update(sql,grocers.getMerchants_id(),grocers.getMerchants_name(),grocers.getLegalperson_name(),grocers.getLegalperson_phone(),grocers.getState());
    17. }
    18. }
    19. =====================
    20. ** service 类
    21. =====================
    22. package test3.service;
    23. import org.springframework.beans.factory.annotation.Autowired;
    24. import org.springframework.stereotype.Service;
    25. import test3.dao.daoImpl.GrocersDaoImpl;
    26. import test3.entity.Grocers;
    27. @Service
    28. public class GrocersService {
    29. @Autowired
    30. private GrocersDaoImpl grocersDao;
    31. public void add(Grocers grocers){
    32. grocersDao.add(grocers);
    33. }
    34. }
    35. ===============================
    36. ** 测试类
    37. ==================
    38. package test3.test.Ch1;
    39. import org.springframework.context.ApplicationContext;
    40. import org.springframework.context.support.ClassPathXmlApplicationContext;
    41. import test3.dao.daoImpl.GrocersDaoImpl;
    42. import test3.entity.Grocers;
    43. public class Ch1 {
    44. public static void main(String[] args) {
    45. Grocers grocers = new Grocers(6,"2","4","5","2");
    46. ApplicationContext context = new ClassPathXmlApplicationContext("bean5.xml");
    47. //名称首字母小写
    48. GrocersDaoImpl grocersDaoImpl = context.getBean("grocersDaoImpl", GrocersDaoImpl.class);
    49. grocersDaoImpl.add(grocers);
    50. }
    51. }
    52. ======================================
  • 相关阅读:
    文心一言 VS 讯飞星火 VS chatgpt (111)-- 算法导论10.2 2题
    LLM-阿里云 DashVector + ModelScope 多模态向量化实时文本搜图实战总结
    CSS 背景
    C++20 实现字符串类型的转换操作
    QDir(目录)
    数据结构 红黄树 学习笔记
    硬件知识积累 PCIE 接口
    第一章:IDEA使用介绍
    JUC常用
    逆向-破零
  • 原文地址:https://blog.csdn.net/qq_56951318/article/details/126812515