Spring IoC 和 AOP

spring框架java开发的行业标准。

spring全家桶。

Web:Spring Web MVC/Spring MVC,Spring Web Flux

持久层:Spring Data/Spring Data JPA,Spring Data Redis,Spring Data MongoDB

安全校验:Spring Security

构建工程脚手架:Spring Boot

微服务:Spring Cloud

IoC是spring全家桶各个功能模块的基础,创建对象的容器

AOP也是以IoC为基础,AOP是面向切面编程,抽象化的面向对象。

AOP可以打印日志,做事务,权限处理。

1.1 IoC

控制反转,将对象的创建进行反转,常规情况下,对象都是开发者手动创建的;使用IoC开发者不再需要创建对象,而是由IoC容器根据需求自动创建项目所需要的对象。

​ 不用IoC:所有对象开发者自己创建

​ 使用IoC:对象不用开发者创建,而是交给spring框架来完成

  1. pom.xml

        <dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.1.9.RELEASE</version></dependency>
    

    基于XML和基于注解

    基于XML:开发者把需要的对象在XML中进行配置,Spring框架读取这个配置文件,根据配置文件的内容来创建对象

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/tx/spring-tx.xsd"><bean class="com.southwind.ioc.DataConfig" id="config"><property name="driverName" value="Driver"></property><property name="url" value="loaclhost:8080"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean>
    </beans>
    
    package com.southwind.ioc;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {/*        DataConfig dataConfig=new DataConfig();dataConfig.setDriverName("Driver");dataConfig.setUrl("localhost:3306/dbname");dataConfig.setUsername("root");dataConfig.setPassword("root");*/ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");System.out.println(context.getBean("config"));}
    }
    

    基于注解

    1. 配置类

      用一个java类来替代XML文件,把在XML中配置的内容放到XML中

      package com.southwind.configuration;import com.southwind.ioc.DataConfig;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;@Configuration
      public class BeanConfiguration {@Beanpublic DataConfig dataConfig(){DataConfig dataConfig=new DataConfig();dataConfig.setDriverName("Driver");dataConfig.setUrl("localhost:3306/dbname");dataConfig.setUsername("root");dataConfig.setPassword("root");return dataConfig;}
      }
      
      package com.southwind.ioc;import com.southwind.configuration.BeanConfiguration;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);System.out.println(context.getBean(DataConfig.class));}
      }
      
    2. 扫包+注解

      更简单,不再需要依赖于XML文件和配置类,而是直接将bean的创建交给目标类,在目标类添加注解来创建

      package com.southwind.ioc;import lombok.Data;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;@Data
      @Component
      public class DataConfig {@Value("localhost:3306")private String url;@Value("Driver")private String driverName;@Value("root")private String username;@Value("root")private String password;
      }
      
      package com.southwind.ioc;import com.southwind.configuration.BeanConfiguration;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.annotation.AnnotationConfigApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context= new AnnotationConfigApplicationContext("com.southwind.configuration");System.out.println(context.getBean(DataConfig.class));}
      }
      

      自动创建对象,完成依赖注入。

      package com.southwind.ioc;import lombok.Data;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;@Data
      @Component
      public class GlobalConfig {@Value("8080")private String port;@Value("/")private String path;@Autowiredprivate DataConfig dataConfig;
      }
      

      @Autowired通过类型进行注入,如果需要通过名称取值,通过@Qualifier()注解完成名称的映射

      package com.southwind.configuration;import com.southwind.ioc.DataConfig;
      import lombok.Data;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.beans.factory.annotation.Qualifier;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.stereotype.Component;@Data
      @Component
      public class GlobalConfig {@Value("8080")private String port;@Value("/")private String path;@Autowired@Qualifier("config")private DataConfig dataConfig;
      }
      

1.2AOP

面向切面编程,它是一种抽象化的面向对象编程,对面向对象编程的一种补充,底层使用动态代理机制来实现

打印日志

业务代码和打印日志耦合起来

计算器方法中,日志和业务混合在一起,AOP要做的就是将日志代码全部抽象出去统一进行处理,计算器方法中只保留核心的业务代码。

做到核心业务和非业务代码的解耦合

  1. 创建切面类

    package com.southwind.aop;import org.aopalliance.intercept.Joinpoint;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.AfterReturning;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Before;
    import org.springframework.stereotype.Component;
    import java.util.Arrays;@Component
    @Aspect
    public class LoggerAspect {@Before("execution(public int com.southwind.aop.CalImpl.*(..))")public void before(JoinPoint joinPoint) {String name = joinPoint.getSignature().getName();System.out.println(name + "方法的参数是" + Arrays.toString(joinPoint.getArgs()));}@AfterReturning(value="execution(public int com.southwind.aop.CalImpl.*(..))",returning ="result")public void afterReturning(JoinPoint joinPoint,Object result){String name = joinPoint.getSignature().getName();System.out.println(name + "方法的结果是" + result);}
    }
    
  2. 实现类添加@Component注解

    package com.southwind.aop;import org.springframework.stereotype.Component;@Component
    public class CalImpl implements cal{@Overridepublic int add(int num1, int num2) {int result = num1 + num2;return result;}@Overridepublic int sub(int num1, int num2) {int result = num1 - num2;return result;}@Overridepublic int mul(int num1, int num2) {int result = num1 * num2;return result;}@Overridepublic int div(int num1, int num2) {int result = num1 / num2;return result;}
    }
    
  3. 配置扫包,开启自动生成代理对象

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/beans"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/tx/spring-tx.xsd"><!--自动扫包--><context:component-scan base-package="com.southwind.aop"></context:component-scan><!--开启自动生成代理--><aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    </beans>
    
  4. 使用

    package com.southwind.aop;import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");cal bean = context.getBean(cal.class);System.out.println(bean.add(9, 8));System.out.println(bean.sub(9, 8));System.out.println(bean.mul(9, 8));System.out.println(bean.div(9, 8));}
    }
    

Spring IoC 和 AOP相关推荐

  1. 自己动手实现的 Spring IOC 和 AOP - 下篇

    1. 背景 本文承接上文,来继续说说 IOC 和 AOP 的仿写.在上文中,我实现了一个很简单的 IOC 和 AOP 容器.上文实现的 IOC 和 AOP 功能很单一,且 IOC 和 AOP 两个模块 ...

  2. 自己动手实现的 Spring IOC 和 AOP - 上篇

    1. 背景 我在大四实习的时候开始接触 J2EE 方面的开发工作,也是在同时期接触并学习 Spring 框架,到现在也有快有两年的时间了.不过之前没有仿写过 Spring IOC 和 AOP,只是宏观 ...

  3. 【Spring 源码阅读】Spring IoC、AOP 原理小总结

    Spring IoC.AOP 原理小总结 前言 版本约定 正文 Spring BeanFactory 容器初始化过程 IoC 的过程 bean 完整的创建流程如下 AOP 的过程 Annotation ...

  4. 浅谈 Spring IOC和AOP

    浅谈 Spring IOC和AOP IOC 控制反转 以前创建对象的主动权和时机是由于自己把握的,现在将这种权利转移到Spring容器中,并且根据配置文件去创建对象管理对象 ioc的注入方式有三种:构 ...

  5. BeanPostProcessor —— 连接Spring IOC和AOP的桥梁

    之前都是从大Boss的视角,来介绍Spring,比如IOC.AOP. 今天换个视角,从一个小喽啰出发,来加深对Spring的理解. 这个小喽啰就是, BeanPostProcessor (下面简称 B ...

  6. Spring IOC 和 AOP 概览

    IOC(控制反转) IoC(Inversion of Control,控制倒转).所谓IoC,对于spring框架来说,就是由spring来负责控制对象的生命周期和对象间的关系. 在没有IOC时,我们 ...

  7. spring - ioc和aop

    1.程序中为什么会用到spring的ioc和aop 2.什么是IOC,AOP,以及使用它们的好处,即详细回答了第一个问题 3.原理 关于1: a:我们平常使用对象的时候,一般都是直接使用关键字类new ...

  8. 再品Spring Ioc 和 Aop

    文章目录 Spring好处 IOC 基于XML和基于注解开发 基于XML开发 基于注解开发 配置类 扫包+注解 依赖注入 AOP 写在前面,这篇文章写的时候我的SSM已经学过一遍了,回头来看真的受益匪 ...

  9. 吊打面试官系列之--吃透Spring ioc 和 aop (中)

    目录 Spring SpringBean的五个作用域 SpringBean的生命周期 创建过程 销毁过程 AOP的介绍和使用 AOP的介绍 AOP的三种织入方式 操作讲解 AOP的主要名词概念 Adv ...

最新文章

  1. PCL安装与环境变量配置(Win10)
  2. [zz]正则表达式使用详解
  3. Java GC日志查看和分析
  4. 概率论-4.2中心极限定理(待补充)
  5. MySQL高级 - 锁 - InnoDB行锁 - 基本演示
  6. 鹅厂设计师是如何做设计的?
  7. python一键取消注释_Python文件去除注释的方法
  8. 分布式搜索 Elasticsearch —— 删除索引
  9. Python——通过斐波那契数列来理解生成器
  10. 图说:为什么Java中的字符串被定义为不可变的
  11. TF-IDF + K-Means 中文聚类例子 - scala
  12. 360 php offer,审批终于通过了,从面试到拿到奇虎360的offer已经失…
  13. 样本分布不平衡,机器学习准确率高又有什么用?
  14. BMVC18|无监督深度关联学习大幅提高行人重识别性能(附Github地址)
  15. 3dsmax学校教室模型_从小学教室到大学教室的开放项目合作
  16. Intellij IDEA 安装lombok及使用详解
  17. 你最喜欢哪款游戏的界面风格,为什么?
  18. SpringBoot MongoDB批量插入数据
  19. win10恢复出厂设置_手机如何恢复出厂设置
  20. 六西格玛dfss_六西格玛设计(DFSS)的方法和知识

热门文章

  1. .vimrc 错误 E484:打不开syntax.vim E185:Cannot find color scheme
  2. 黑马头条项目-Vue-day9-文章详情模块、关注与取消关注,点赞和喜欢功能
  3. 有什么好的论文查重软件?两分钟让你知道
  4. C# lazy懒加载
  5. 按键精灵获取服务器信息,按键精灵获取窗口信息脚本源码
  6. 淘宝买二级c语言题库可以嘛,大学计算机二级考试(C语言)试题在哪可以买?...
  7. mysql5.5手册读书日记(2)
  8. 计算机网络(重点简单概括)
  9. el-table中使用el-popover点击取消按钮时popover框的显示与隐藏问题
  10. PromptBERT: Improving BERT Sentence Embeddings with Prompts