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 是面向切面编程,抽象化的面向对象

1、打印日志

2、事务

3、权限处理

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

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

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

1、pom.xml
 

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

基于 XML 和基于注解

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

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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-4.3.xsd">

    <bean class="com.southwind.ioc.DataConfig" id="config">
        <property name="driverName" value="Driver"></property>
        <property name="url" value="localhost: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 中配置的内容放到配置类中。

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(value = "config")
    public DataConfig dataConfig(){
        DataConfig dataConfig = new DataConfig();
        dataConfig.setDriverName("Driver");
        dataConfig.setUrl("localhost:3306/dbname");
        dataConfig.setUsername("root");
        dataConfig.setPassword("root");
        return dataConfig;
    }
}
ApplicationContext context = new AnnotationConfigApplicationContext(BeanConfiguration.class);
System.out.println(context.getBean("config"));

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;
}
ApplicationContext context = new AnnotationConfigApplicationContext("com.southwind.ioc");
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.ioc;

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 config;
}
package com.southwind.ioc;

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Data
@Component("config")
public class DataConfig {
    @Value("localhost:3306")
    private String url;
    @Value("Driver")
    private String driverName;
    @Value("root")
    private String username;
    @Value("root")
    private String password;
}

AOP

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

这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程

		面向切面编程(AOP是Aspect Oriented Program的首字母缩写),我们知道,面向对象的特点是继承、多态和封装。而封装就要求将功能分散到不同的对象中去,这在软件设计中往往称为职责分配。实际上也就是说,让不同的类设计不同的方法。这样代码就分散到一个个的类中去了。这样做的好处是降低了代码的复杂程度,使类可重用
		但是人们也发现,在分散代码的同时,也增加了代码的重复性。什么意思呢?比如说,我们在两个类中,可能都需要在每个方法中做日志。按面向对象的设计方法,我们就必须在两个类的方法中都加入日志的内容。也许他们是完全相同的,但就是因为面向对象的设计让类与类之间无法联系,而不能将这些重复的代码统一起来
		也许有人会说,那好办啊,我们可以将这段代码写在一个独立的类独立的方法里,然后再在这两个类中调用。但是,这样一来,这两个类跟我们上面提到的独立的类就有耦合了,它的改变会影响这两个类。那么,有没有什么办法,能让我们在需要的时候,随意地加入代码呢?这种在运行时,动态地将代码切入到类的指定方法、指定位置上的编程思想就是面向切面的编程		一般而言,我们管切入到指定类指定方法的代码片段称为切面,而切入到哪些类、哪些方法则叫切入点。有了AOP,我们就可以把几个类共有的代码,抽取到一个切片中,等到需要时再切入对象中去,从而改变其原有的行为。这样看来,AOP其实只是OOP的补充而已。OOP从横向上区分出一个个的类来,而AOP则从纵向上向对象中加入特定的代码。有了AOP,OOP变得立体了。如果加上时间维度,AOP使OOP由原来的二维变为三维了,由平面变成立体了。从技术上来说,AOP基本上是通过代理机制实现的
		AOP在编程历史上可以说是里程碑式的,对OOP编程是一种十分有益的补充

1、打印日志

2、事务

3、权限处理

打印日志

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-UVN2ilrj-1659516763946)(png/image-20220327235846771.png)]

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

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-DbyfDN1G-1659516763949)(png/image-20220327235915247.png)]

1、创建切面类
 

package com.southwind.aop;

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:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"
       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-4.3.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));
    }
}

AOP核心概念
在上面介绍AOP的工作流程中,我们提到了两个核心概念,分别是:

目标对象(Target):原始功能去掉共性功能对应的类产生的对象,这种对象是无法直接完成最终工作的
代理(Proxy):目标对象无法直接完成工作,需要对其进行功能回填,通过原始对象的代理对象实现
上面这两个概念比较抽象,简单来说,

目标对象就是要增强的类[如:BookServiceImpl类]对应的对象,也叫原始对象,不能说它不能运行,只能说它在运行的过程中对于要增强的内容是缺失的。

SpringAOP是在不改变原有设计(代码)的前提下对其进行增强的,它的底层采用的是代理模式实现的,所以要对原始对象进行增强,就需要对原始对象创建代理对象,在代理对象中的方法把通知[如:MyAdvice中的method方法]内容加进去,就实现了增强,这就是我们所说的代理(Proxy)
DI

基于 field 的注入

所谓基于 field 的注入,就是在变量上使用 @Autowired 注解进行依赖注入。这是我们最熟悉的一种方式,同时,也正是 Spring 团队所不推荐的方式。它用起来就像这样:

@Autowired
private DependencyClass aDependency;

基于 setter 方法的注入

通过 setter() 方法,以及在方法上加入 @Autowired 注解,来完成的依赖注入,就是基于 setter 方法的注入。它用起来就像这样:

private DependencyClass aDependency;

@Autowired
public void setADependency(DependencyClass aDependency) {
    this.aDependency = aDependency;
}

注:在 Spring 4.3 及以后的版本中,setter 上面的 @Autowired 注解是可以不写的。

基于构造方法的注入

将各个必需的依赖全部放在带有 @Autowired 注解构造方法的参数中,并在构造方法中完成对应变量的初始化,这种方式,就是基于构造方法的注入。它用起来就像这样:

public class AClass {
    // 这里 final 修饰符并不是必须的,但是我喜欢这么做
    // 因为这样不仅可以在代码上防止 aDependency 被修改
    // 在语义上也可以表明 aDependency 是不应该被修改的
    private final DependencyClass aDependency;

    @Autowired
    public AClass(DependencyClass aDependency) {
        this.aDependency = aDependency;
    }
}

注:在 Spring 4.3 及以后的版本中,如果这个类只有一个构造方法,那么这个构造方法上面也可以不写 @Autowired 注解

基于 field 的注入有什么问题

基于 field 的注入,虽然不是绝对禁止使用,但是它可能会带来一些隐含的问题。比如,在这篇博客)中,作者给出了这样的一个代码

@Autowired
private User user;

private String school;

public UserAccountServiceImpl(){
    this.school = user.getSchool();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值