Spring完整

本文详细介绍了Spring框架的核心概念,包括IOC(控制反转)、DI(依赖注入)以及AOP(面向切面编程)。通过实例展示了如何使用Spring的注解进行配置,讲解了单例、多例、懒加载机制以及Spring容器的工作原理。同时,深入探讨了AOP的各个通知类型、切入点表达式和动态代理机制,提供了完整的代码示例,帮助读者全面理解和掌握Spring框架。
摘要由CSDN通过智能技术生成

目录

1.Spring

1.1 介绍

1.2 传统代码弊端

1.3面向接口编程

1.4.IOC

1.4.1 IOC 介绍

测试

1.4.2 spring容器说明

1.5 spring注解开发

1.5.1 配置类

1.6 单例/多例

1.6.1 说明

1.6.2 测试

1.7 懒加载机制

1.8 Spring生命周期

2.依赖注入(Dependency Injection,简称DI)

2.1 依赖注入

2.2 MVC设计思想

2.2.1 MVC设计思想说明

 2.3 “三层”代码结构

2.4 “三层”代码结构实现

2.4.1 代码结构说明

2.4.2 “三层”代码结构和MVC的关系

2.5 动态为属性赋值操作

2.5.1 @Value注解说明

2.5.2 编辑properties配置文件

2.5.3 实现属性注入操作

3.spring中的AOP

3.1 AOP介绍

3.2 动态代理机制-JDK

 3.2.1 动态代理的说明

3.3 动态代理机制-CGLIB

3.4 Spring AOP

3.4.1 AOP介绍

3.4.2 AOP中专业术语

3.4.3 通知类型

3.4.4 切入点表达式

3.4.5 引入AOP jar包文件

3.4.6 定义切面类

3.4.7 让AOP生效

3.4.8 编辑测试类

3.4.9 AOP形象化比喻

3.5 关于切入点表达式解析

3.5.1 bean标签写法

3.5.2 within表达式

3.5.3 execution表达式

 3.5.4 注解

3.6 通知方法 

3.6.1 关于通知方法解析

3.6.2 前置通知案例

3.6.3 后置通知案例

3.6.4 异常通知案例

3.6.5 最终通知

3.6.6 环绕通知

3.6.7 AOP执行顺序说明

3.7 Spring中的通知总结

Spring总结

常用注解


1.Spring

1.1 介绍

Spring框架是一个开放源代码的J2EE应用程序框架,由Rod Johnson发起,是针对bean的生命周期进行管理的轻量级容器(lightweight container)。

1.2 传统代码弊端

总结:使用一个new一个对象,耦合度高

准确:1.如果类中的属性通过new的方式直接绑定,则类与属性的耦合性高

           2.如果需要修改属性类型,则需要修改代码,后期扩展不方便

1.3面向接口编程

总结:好处:将两个对象的共同方法抽取到接口中,实现代码的解耦

           弊端:根源问题仍然没有解决,属性与类仍然绑定

准确:使用接口开发,可以在一定程度上降低类与对象的耦合性,但是属性与类绑定的事实并没              有解决

 修改后:

在这里插入图片描述

1.4.IOC

1.4.1 IOC 介绍

IOC全称Inversion of Control,即“控制反转”,这是一种设计思想。对象创建的权利由Spring框架完成,由容器管理对象的生命周期。

IOC理论提出的观点大体是:借助于“第三方”实现具有依赖关系的对象之间的解耦

详细内容在:http://IOC的理解

测试

(1)定义Student类

public class student {
    public void grade(){
        System.out.println("成绩合格!");
    }
}

(2)在resource的目录下创建spring.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--将Dog对象交给Spring容器管理
        1.属性id是bean的唯一标识符. 一般类名首字母小写
        2.属性class 表示被管理的类
    -->
    <bean id="student" class="cn.tedu.test4.student"></bean>
    <!--<bean id="cat" class="com.jt.demo2.Cat"></bean>-->

</beans>

(3)编辑测试类

public class test {
    public static void main(String[] args) {
        String resource="spring.xml";
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource);
        student student=applicationContext.getBean(student.class);
        student.grade();
        
    }

(4)结果

成绩合格

1.4.2 spring容器说明

解释: Spring容器的数据结构是Map集合,Map<key,value>,

         key是bean中id值(id的首字母都是小写), value是通过反射机制实例化的对象.

数据结构:Map<key,value>  即:Map<student,student对象>

public class test {
    public static void main(String[] args) {
//        String resource="spring.xml";
//        ApplicationContext applicationContext=new ClassPathXmlApplicationContext(resource);
//        student student=applicationContext.getBean(student.class);
//        student.grade();
        getStudent();
    }
    public static void getStudent(){
        try {
            student student=(student) Class.forName("cn.tedu.test4.student").newInstance();
            student.grade();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

结果:与上面结果一样,即spring容器是通过反射机制创建对象

1.5 spring注解开发

1.5.1 配置类

注解1:@Configuration        标识这个一个配置类

注解2:@ComponentScan("路径")       注解包扫描,使得注解有效

注解3:@Bean       标识该方法的返回值交给spring容器管理

@Configuration
@ComponentScan("cn.tedu.test4")
public class Config {
    /*@Bean注解:将User对象交给Spring容器管理
    key:  id就是方法名  user
    value: 就是返回值类型*/
    @Bean
    public student student(){
        return new student();
    }
}

1.6 单例/多例

1.6.1 说明

单例模式:Spring容器中管理对象,在内存中只有一份

多例模式:Spring容器中管理对象,在内存中有多份

1.6.2 测试

属性值:

注解4:@Scope    控制单例和多例

@Scope("singleton")     默认值 单例模式

@Scope("prototype")     多例模式

@Bean
//    @Scope("singleton")    //单例 
    @Scope("prototype")      //多例
    public student student(){
        return new student();
    }
public static void main(String[] args) {
        ApplicationContext context=new AnnotationConfigApplicationContext(Config.class);
        student student=context.getBean(student.class);
        student student1=context.getBean(student.class);
        System.out.println(student==student1);  //单例模式  true;多例模式  false
    }

1.7 懒加载机制

注解5:@Lazy

关于多例模式和懒加载说明:

@Lazy 只能控制单例模式,多例模式都是懒加载

1.8 Spring生命周期

阶段:对象创建   初始化   业务调用   对象销毁

注解6:@PostConstruct

注解7:@PreDestroy

@Bean

public Person person(){return new Person;}

在配置类中通过@Bean将Person交给容器管理

或者直接在Person类中加注解:@Component        交给Spring容器管理

@Component
public class Person {
    public Person() {
        System.out.println("我出生了");
    }
    @PostConstruct
    public void init(){
        System.out.println("我长大了");
    }
    public void hello(){
        System.out.println("赚钱");
    }
    @PreDestroy
    public void destroy(){
        System.out.println("老了");
    }
}

测试类:

接口中没有提供close的方法,需要使用实现类进行操作<

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值