Spring IoC笔记

embedded/2024/10/4 15:29:09/

目录

1.什么是 IoC?

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

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

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

spring%E7%AE%A1%E7%90%86%E7%9A%84%E6%9D%A1%E4%BB%B6%E6%98%AF%EF%BC%9F-toc" style="margin-left:80px;">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有自己的特殊性。

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

spring%E7%AE%A1%E7%90%86%E7%9A%84%E6%9D%A1%E4%BB%B6%E6%98%AF%EF%BC%9F">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 {@Beanpublic Student StudentInfo() {return new Student("wh",01);}@Beanpublic 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.ppmy.cn/embedded/121938.html

相关文章

工具模块及项目整体模块框架

文章目录 工具模块logger.hpphelper.hppthreadpool.hpp 核心概念核心API交换机类型持久化⽹络通信消息应答持久化数据管理中心模块虚拟机管理模块交换路由模块消费者管理模块信道管理模块连接管理模块Broker服务器模块消费者管理信道请求模块通信连接模块项⽬模块关系图 工具模…

【QT】亲测有效:“生成的目标文件包含了过多的段,超出了编译器或链接器允许的最大数量”错误的解决方案

在使用dlib开发人脸对齐功能时&#xff0c;出现了”生成的目标文件包含了过多的段&#xff0c;超出了编译器或链接器允许的最大数量的错误“。 主要功能代码如下&#xff1a; #include <QApplication> #include <QImage> #include <QDebug>#include <dlib…

PHP中常用的字符串函数详解

文章目录 PHP中常用的字符串函数详解一、引言二、字符串连接1、连接字符串 三、字符串长度1、获取字符串长度2、获取UTF-8编码下字符串长度 四、字符串截取1、截取字符串2、截取UTF-8编码下字符串 五、字符串替换1、替换字符串 六、字符串分割1、分割字符串 七、字符串大小写转…

Python批量处理客户明细表格数据,挖掘更大价值

批量处理 .xls 数据并进行归类分析以挖掘内在价值&#xff0c;通常涉及以下步骤&#xff1a; 读取数据&#xff1a;使用 pandas 库读取 .xls 文件。数据清洗&#xff1a;处理缺失值、异常值、重复值等。数据转换&#xff1a;对数据进行必要的转换&#xff0c;如日期格式统一、…

vscode安装及c++配置编译

1、VScode下载 VS Code官网下载地址&#xff1a;Visual Studio Code - Code Editing. Redefined。 2、安装中文插件 搜索chinese&#xff0c;点击install下载安装中文插件。 3、VS Code配置C/C开发环境 3.1、MinGW-w64下载 VS Code是一个高级的编辑器&#xff0c;只能用来写代…

GPT与大模型行业落地实践探索

简介 本课程探讨GPT和大模型技术在行业中的实际应用和发展。课程将涵盖GPT的基础知识、原理、及其在行业中的应用案例&#xff0c;如财报分析和客服机器人。重点在于结合实际案例中的使用效果&#xff0c;讲解如何利用GPT的API开发企业级应用以及利用更高级的功能构造AI Agent。…

面试-2024年6月19号

面试-2024年6月19号 linux中grep的作用是什么&#xff0c;怎么使用的。k8s中&#xff0c;在pod中对svc的访问异常时&#xff0c;排查思路及解决方案。k8s中&#xff0c;网络问题的排查思路、定位过程及解决办法。在Deployment和StatefulSet的yaml编写上&#xff0c;有什么不同。…

php函数积累

对称函数 isset 判断数组arr中是否存在键key 返回值true/false isset(name,$arr) unset 删除数组中的键 需存在key不然抛出异常 unset($arr[name]) json_encode 数据转json格式 json_encode($arr) 一般形式 指定字符编码形式 json_decode json格式转原有数据格式 json_d…