当前位置: 首页 > article >正文

Spring IoC笔记

目录

1.什么是 IoC?

2.IoC类注解(五大注解)

2.1那为什么要这么多类注解? 

2.2五大注解是不是可以混用?

2.3程序被spring管理的条件是?

3.bean对象

3.1Bean 命名约定

3.2获取bean对象

4.⽅法注解 @Bean


1.什么是 IoC?

IoC: Inversion of Control (控制反转),是Spring的核⼼思想
什么是控制反转呢?
获得依赖对象的过程被反转了
也就是说, 当需要某个对象时, 传统开发模式中需要⾃⼰通过 new 创建对象, 现在不需要再进⾏创 建, 把创建对象的任务交给容器, 程序中只需要依赖注⼊ (Dependency Injection,DI)就可以了. 这个容器称为:IoC容器. Spring是⼀个IoC容器, 所以有时Spring 也称为Spring 容器。
通过上述我们还可以知道,Spring 是什么?
Spring 是包含了众多⼯具⽅法的 IoC 容器。

2.IoC类注解(五大注解)

前提: 

1.五大类注解:@Controller、@Service、@Repository、@Component、@Configuration 
2.bean对象:在spring容器中存放的对象。
3.ApplicationContext: 翻译过来就是: Spring 上下⽂。这个上下⽂, 就是指当前的运⾏环境, 也可以看作是⼀个容器。故ApplicationContext的对象中存放了所有与当前的运⾏环境有关的内容,比如 spring容器中存放的bean对象。

 它们的功能都是把对象交给spring管理。

2.1那为什么要这么多类注解? 

与应⽤分层呼应,让程序员看到类注解之后,就能直接了解当前类的⽤途。

(1)@Controller:控制层, 接收请求, 对请求进⾏处理, 并进⾏响应.  

(2)@Servie:业务逻辑层, 处理具体的业务逻辑 

(3)@Repository:数据访问层,也称为持久层. 负责数据访问操作  

(4)@Configuration:配置层. 处理项⽬中的⼀些配置信息. 

(5)@Component 是⼀个元注解,也就是说可以注解其他类注解

@Controller , @Service , @Repository ,@Configuration.这些注解被称为 @Component 的衍⽣注解。因为这些注解⾥⾯都有⼀个注解 @Component。

2.2五大注解是不是可以混用?

答:可以,但不是完全可以。

功能上:@Service , @Repository ,@Configuration,@Component 可以完全混用,@Controller有自己的特殊性。

规范上:不可以混用。因为我们想要与应⽤分层呼应。

2.3程序被spring管理的条件是?

 1.要被spring扫描到(默认路径是启动类所在的目录,包括子目录)

手动设置:

@ComponentScan(basePackages = "~~~~~")

2.需要配置五大注解和@Bean


3.bean对象

 1.   Object getBean(String name) throws BeansException;

 2.   <T> T getBean(String name, Class<T> requiredType) throws BeansException;

 3.   Object getBean(String name, Object... args) throws BeansException;

 4.   <T> T getBean(Class<T> requiredType) throws BeansException;

 5.   <T> T getBean(Class<T> requiredType, Object... args) throws BeansException;

 常⽤的是上述1,2,4种, 这三种⽅式,获取到的bean是⼀样的

其中1,2种都涉及到根据名称来获取对象. bean的名称是什么呢?

3.1Bean 命名约定

程序开发⼈员不需要为bean指定名称(BeanId), 如果没有显式的提供名称(BeanId),Spring容器将为该 bean⽣成唯⼀的名称.

1.一般命名约定使⽤Java标准约定作为实例字段名. 也就是说,bean名称以⼩写字⺟开头,然后使⽤驼峰式 ⼤⼩写。

2.特殊情况,当有多个字符并且第⼀个和第⼆个字符都是⼤写时, 将保留原始的⼤⼩写. 这些规则 与java.beans.Introspector.decapitalize (Spring在这⾥使⽤的)定义的规则相同.

3.使用@Bean注解的话,对象名就是添加@Bean注解方法的名称。

3.2获取bean对象

 使⽤ @Controller 存储 bean 的代码如下:

@Controller
public class UserController {

    public void sayHi(){
        System.out.println("UserController Hi");
    }
}

启动类: 

import com.wh.ioc.Controller.UserController;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringIocApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        UserController bean1 = context.getBean(UserController.class);
        bean1.sayHi();
        UserController bean2 = (UserController) context.getBean("userController");
        bean2.sayHi();
        UserController bean3 = context.getBean("userController", UserController.class);
        bean3.sayHi();
    }

}

4.⽅法注解 @Bean

类注解是添加到某个类上的, 但是存在两个问题:
1.使⽤外部包⾥的类, 没办法添加类注解
2.⼀个类, 需要多个对象, ⽐如多个数据源

⽅法注解 @Bean很好的解决了这两点。 

⽅法注解要配合类注解使⽤
在 Spring 框架的设计中,⽅法注解 @Bean 要配合类注解才能将对象正常的存储到 Spring 容器中。
外部类:Student
import lombok.Data;

@Data
public class Student {
    private String name;
    private Integer id;

    public Student() {

    }

    public Student(String name) {
        this.name = name;
    }

    public Student(String name, Integer id) {
        this.name = name;
        this.id = id;
    }
}
BeanConfig类:
import com.wh.ioc.Model.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class BeanConfig {

    @Bean
    public Student StudentInfo() {
        return new Student("wh",01);
    }
    @Bean
    public Student StudentInfo2() {
        return new Student("Bob",02);
    }
}

启动类:

import com.wh.ioc.Controller.UserController;
import com.wh.ioc.Model.Student;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class SpringIocApplication {

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(SpringIocApplication.class, args);
        Student student = (Student) context.getBean("StudentInfo");
        System.out.println(student);
        Student student2 = (Student) context.getBean("StudentInfo2");
        System.out.println(student2);
    }
}

以上为我个人的小分享,如有问题,欢迎讨论!!! 

都看到这了,不如关注一下,给个免费的赞 


http://www.kler.cn/news/326902.html

相关文章:

  • 【Spring】lombok、dbUtil插件应用
  • 【SQL】筛选字符串与正则表达式
  • 07_矩形圆形绘制
  • 责任链模式优化 文章发布的接口(长度验证,敏感词验证,图片验证等环节) 代码,示例
  • Linux云计算 |【第四阶段】RDBMS1-DAY1
  • EZUIKit.js萤石云vue项目使用
  • Golang plugin包教程:创建与管理插
  • MacOS多桌面调度快捷键
  • 1.1.4 计算机网络的分类
  • 一篇文章快速学会docker容器技术
  • 强化学习-python案例
  • 实现简易 vuedraggable 的拖拽排序功能
  • Java入门3——操作符+String
  • 《论文阅读》 用于产生移情反应的迭代联想记忆模型 ACL2024
  • Vue 3 文件编译流程详解与 Babel 的使用
  • [Uninstall] 软件彻底卸载工具的下载及详细安装使用过程(附有下载文件)
  • C#和数据库高级:虚方法
  • 安卓13禁止待机 永不休眠 android13永不休眠
  • JVM基本组成
  • Redis的数据类型常用命令
  • Python 学习入门笔记
  • smartctl 命令:查看硬盘健康状态
  • 【低功耗防山火在线监测装置】
  • nginx相关操作
  • selenium模块入门
  • 【Rust练习】16.方法和关联函数
  • helm部署ingress-nginx
  • Docker-2.如何保存数据退出
  • 什么是文件完整性监控(FIM)
  • ComfyUI新版本快捷键大全,快速提升效率,建议收藏