IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> Java知识库 -> Spring IoC 和 AOP -> 正文阅读

[Java知识库]Spring IoC 和 AOP

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.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://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 {
          @Bean
          public 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;
          @Autowired
          private 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要做的就是将日志代码全部抽象出去统一进行处理,计算器方法中只保留核心的业务代码。

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

image-20221004182330377
  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{
        @Override
        public int add(int num1, int num2) {
            int result = num1 + num2;
            return result;
        }
    
        @Override
        public int sub(int num1, int num2) {
            int result = num1 - num2;
            return result;
        }
    
        @Override
        public int mul(int num1, int num2) {
            int result = num1 * num2;
            return result;
        }
    
        @Override
        public 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.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    		http://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));
        }
    }
    
  Java知识库 最新文章
计算距离春节还有多长时间
系统开发系列 之WebService(spring框架+ma
springBoot+Cache(自定义有效时间配置)
SpringBoot整合mybatis实现增删改查、分页查
spring教程
SpringBoot+Vue实现美食交流网站的设计与实
虚拟机内存结构以及虚拟机中销毁和新建对象
SpringMVC---原理
小李同学: Java如何按多个字段分组
打印票据--java
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:27:04  更:2022-10-08 20:30:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2025年3日历 -2025/3/10 15:59:00-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码