Spring框架

Spring框架

SSM:Spring,Spring MVC,MyBatis

Spring MVC替代Servlet

Spring现在已经是一堆框架的集合体,但是当提到Spring的时候,还是指其中的Spring框架

Spring的核心功能DI(IOC),AOP

软件工程的基本准则:高类聚,低耦合

**高类聚:**一个模块的功能应该尽量紧凑,不可拆分

**低耦合:**模块和模块之间的联系应该尽量松散

Spring中的DI(依赖注入)这个模块只要帮我们解决耦合性高的问题

DI(IoC)

概述

Spring管理所有的类

**DI:**依赖注入

A对象需要B对象,但是A又不能newB(因为这样的话AheB联系就会过于紧密)

此时必须依赖第三方将这个B对象送给A(注入),这个第三方角色由Spring担任

**IoC:**控制反转

没有Spring之前,A需要B的对象都是自己new,此时的决定权在自己手里,在Spring之后,控制权发生了转移,所以就是控制反转

使用

  • Spring配置

  • 每一个类上面要加入@Component注解,表示这个类被Spring管理

  • 装配,在需要Spring提供对象时加入@Autowried注解

  • 测试

隐式装配和显示装配
  1. 隐式装配:最好,能用尽量用 @Component

  2. 显示装配:当一个类不能加入@Component注解的时候,这个类的对象又需要被Spring管理,此时只能显示装配。

@Configuration
@ComponentScan
public class RootConfig {
    @Bean //显示装配
    public LocalDateTime localDateTime() {
        return LocalDateTime.of(1000,1,1,1,1,1);
    }
}

public class Person {
    @Autowired
    private Axe axe;

    @Autowired
    private LocalDateTime localDateTime;

    public void chop() {
        axe.chop();
        System.out.println(localDateTime);
    }

}

AOP

概述

面向切面编程:是OOP的一个有益补充,绝不是用来代替OOP的

@Component
@Aspect // 这个注解表示这个类是一个切面
public class Audience {

    @Pointcut("execution(* com.example.spring.aop.Performance.perform(..)) && args(i)")
    public void performance(int i) {}

    @Around("performance(i)")
    public void watchPerformance(ProceedingJoinPoint jp, int i) {
        try{
            System.out.println("Silencing cell phones");
            System.out.println("The audience is taking their seats.");
            jp.proceed(); //代替了表演这个方法的调用
            System.out.println("CLAP CLAP CLAP CLAP CLAP");

        } catch (Throwable e) {
            System.out.println("Demanding a refund");
        }
    }


    @Before("performance(i)")
    // before的含义是将其注解的方法中的内容放入表达式所标记方法的前面执行
    // ()里面的是AspectJ框架的语法,×表示这个方法返回值任意,..表示这个方法的参数是任意的
    public void silenceCellPhones(int i) {
        System.out.println("Silencing cell phones");
    }

    @Before("performance(i)")
    public void takeSeats(int i) {
        System.out.println("The audience is taking their seats.");
    }
    @AfterReturning("performance(i)")
    public void applaud(int i) {
        System.out.println("CLAP CLAP CLAP CLAP CLAP");
    }

    @AfterThrowing("performance(i)")
    public void demandRefund(int i) {
        System.out.println("Boo! We want our money back!");
    }

通知(Advice)

切面存在一定的目标,在AOP中,切面的工作被称为通知

  • Before通知:在目标方法被调用之前调用通知功能
  • After通知:在目标方法完成之后调用的通知
  • After-Returning 通知:在目标方法成功执行后调用通知
  • After-Throwing 通知:在目标方法抛出异常后调用通知
  • Around 通知:通知包起来被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为

链接点(Join Point)

在应用执行过程中能够在插入切面的一个点,这个点是可以方法调用时,抛出异常时,甚至是修改字段时

切点(Point Cut)

是在执行过程中能够插入的切面的一个点

切面(Aspect)

切面是切点和通知的结合

引入(Introduction)

引入允许向我们现有的类中添加新方法和属性

织入(Weaving)

把切面应用到目标对象并被创建新的代理对象的过程

  • 编译器:切面在目标类编译时被织入
  • 类加载期:切面在目标类加载到JVM时被织入
  • 运行时:切面之前应用运行是的某个时刻被织入
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值