是Spring啊!
阿里云国际版折扣https://www.yundadi.com |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
一.概念
- spring概念
一个包含了众多工具方法的 IoC 容器
okk~~分析一下这句话意思,众多方法,IoC 是形容词,容器是名词 ->
众多方法:比如一个类里有许多方法, 容器:存储的东西 重点就是IoC是什么?
- Ioc
2.1解释
IoC -> Inversion of Control 控制反转 -> 对象的生命周期 -> 控制权: 由程序猿 变为 Spring IoC
简单来说 Java里的对象的创造和销毁都是被程序猿掌控的, 但是IoC的话 就变了, 掌控这些的不是程序猿了,而是Spring
2.2 意义
所以,把对象的生命周期的控制权交出去了,有什么好处?
~解耦合!!!!
每个对象都要程序猿创建,如果1的创建需要2,2的创建需要3..... 那么当代码出现异常,76出现了bug - > 前面的1到75就会都有问题产生 -> 所以这个时候,好处显而易见
~并且,对象的生命周期交给IoC维护, 作为程序猿无需关注
- DI
其实IoC只是一个思想, 那么具体的实现其实是 DI
DI -> Dependency Injection 依赖注入
IoC DI 从不同的维度描述了同一件事 比如->我的计划和实际操作(我要学会一门乐器->钢琴十级了现在)
二.创建一个Spring项目
1.复杂的创建
1.1.创建
1.1.1创建一个maven项目
1.1.2添加spring配置文件
![](https://img-blog.csdnimg.cn/img_convert/8cf18e943be75f1bdd5d656dfceea921.png)
![](https://img-blog.csdnimg.cn/img_convert/ef7a31733fb26081b8b9025b952c956d.png)
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">
</beans>
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
1.1.3创建spring对象
![](https://img-blog.csdnimg.cn/img_convert/83f39e37dbb992f1549fa4ad3bb3ced1.png)
1.1.4将对象配置到spring中
![](https://img-blog.csdnimg.cn/img_convert/3c40492fe1f27ef8dd6e52bdf9e0fdaa.png)
1.1.5得到spring对象
1.1.6获取spring对象
1.1.7使用一下
![](https://img-blog.csdnimg.cn/img_convert/af7aee3f41885efc86988e196adee371.png)
1.2. 注意事项
![](https://img-blog.csdnimg.cn/img_convert/afcd7c5f204e393096b963b3bd86d1cf.png)
1.3. ApplicationContext BeanFactory区别
ApplicationContext BeanFactory 区别:
* 相同:
都可以实现从容器中获取 bean, 都提供了 getBean的方法
* 不相同:
* 1.ApplicationContext 是 BeanFactory 的子类,
* BeanFactory只提供了基础访问 Bean的方法
* ApplicationContext 除了拥有 BeanFactory的所有功能外,还提供了更多的方法,对国际化的支持,资源访问支持...
* 2.性能方面
* BeanFactory 按需加载 Bean
* ApplicationContext饿汉模式, 创建的时候所有 Bean都加载了
2.简单的创建
2.1创建 - 5大类注解
![](https://img-blog.csdnimg.cn/img_convert/4c1a0f908a4df98669311e1557ccf7b4.png)
![](https://img-blog.csdnimg.cn/img_convert/d14b96cd01c5e87f6499e96d33c43145.png)
![](https://img-blog.csdnimg.cn/img_convert/74524b6c714f4cfbf09d0401a56da17a.png)
![](https://img-blog.csdnimg.cn/img_convert/5d97375f11b0ce96731dc6f6575a81bf.png)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.2.3.RELEASE</version>
</dependency>
</dependencies>
<?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:content="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 所有beans存放的位置-->
<content:component-scan base-package="beans"></content:component-scan>
</beans>
2.1.2 5大类注解
2.1.2.1 5大类注解
@Controller 控制器 业务逻辑层
@Service 服务 服务层
@Repository 仓库 持久层
@Configuration 配置 配置层
@Component 组件
2.1.2.2 5大类注解的意义
@Configuration -> @Controller 控制器 (前端参数的校验) -> @Service(数据的组装,接口的调用) -> @Repository(和数据库进行交互)
可以让代码可读性提高, 让程序员快速知道当前类的用途
比如 Java里的 @Override 看到就知道被标注的方法重载了父类的方法
2.1.2.3 5大类注解的关系
其他四个类本身是属于 @Component 的“⼦类”--看源码
![](https://img-blog.csdnimg.cn/img_convert/5cbf9fc97cf77e53584e6ed1f9e3517c.png)
2.1.3 解释
![](https://img-blog.csdnimg.cn/img_convert/33f6283ebf5ba4828a6a400d02c35e87.png)
![](https://img-blog.csdnimg.cn/img_convert/376f125a0db75b4101b8c8ce98533afb.png)
![](https://img-blog.csdnimg.cn/img_convert/9acc32809b892712c4add7f18df04349.png)
public static void main(String[] args) {
String name1 = "user";
String name2 = "USER";
System.out.println(Introspector.decapitalize(name1));
System.out.println(Introspector.decapitalize(name2));
}
2.2创建-使用方法注解@Bean
![](https://img-blog.csdnimg.cn/img_convert/7e163ce12ecdf2535be047cf929f5c02.png)
![](https://img-blog.csdnimg.cn/img_convert/18a1a552721bd4256a1ee8c2b6d2b0af.png)
![](https://img-blog.csdnimg.cn/img_convert/70abc4392ae88f17a7050d310f8152ec.png)