11.1Spring基础(核心概念,创建和使用,简单读取)

一.Spring概念:

1.Spring就是包含了众多工具方法的IoC容器.

2.IoC容器:控制反转,指的是对象的生命周期,将对象的生命周期(什么时候创建,销毁)交给Spring进行管理.

1a54a4a9c86d4421a312b0cf958575ea.png

在传统开发中,如果A类依赖B类,会在A类中创建B类的实例,如果B类增加一个属性,那么使用B类的构造方法需要修改代码,如果使用IoC的观念,类的实例的创建全部在app类的方法中(由Spring完成),这样B类发生改变,A类不需要修改代码.

3.为什么要使用IoC容器:为了解耦合.

4.IoC容器的两个基本功能是将对象存入容器和从容器中取出容器.

5.DI:依赖注入,在程序运行期间,动态的将某个对象传入到当前类中的行为.

6(重要).IoC和DI的区别:IoC是一种思想,DI是一种具体的实现技术.

二.Spring创建:

1.步骤:

d112f5f2d18b404ea0578a9c710af8c8.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>

dbebe6a64fc14ebe854e2fa4010a1f92.png

2.Spring对象的存储:

a.创建Bean对象,随便写一个类.

b.将Bean对象存到Spring当中(使用xml):resourses目录下创建Spring配置文件(命名无要求,这里我用spring-config.xml),然后将Bean配置进去,使用下面第二段代码.

<?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"><bean id="user" class="User"></bean>
</beans>

3.从Spring中读取到Bean对象:

 a.先得到Spring的上下文.

ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

 b.得到Bean对象(三种方法,推荐第三种).

        //User user = (User)context.getBean("user"); // 这里需要和xml文件中的bean标签的id名称对应,如果是空指针,强转会出错//User user = context.getBean(User.class); // 这种方法不推荐,如果同一个类注入多次就会发生问题User user = context.getBean("user", User.class); // 推荐使用这种方法,不用强制,不会出错

注意起名规范(xml中bean标签id不重复,class用域名(如果没有可省略)+类名),名字对应. 

 c.使用Bean(可选).

System.out.println(user.hello());

d.使用BeanFactory方式.

BeanFactory context = new XmlBeanFactory(new ClassPathResource("spring-config.xml")); // 古老写法

ApplicationContext和BeanFactory区别:

1)相同点

a)都可以得到Spring上下文对象.

b)都是来着Spring的顶级接口.

2)不同的

a) ApplicationContext是BeanFactory的子类,前者功能更多(国际化支持,资源方法,事件传播).

b)性能区别: ApplicationContext是饿汉模式,BeanFactory是懒汉模式,前者快,后者慢.

三.Spring更简单地读取和存储对象

1.依靠注解,先配置扫描路径

<?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"><content:component-scan base-package="com"></content:component-scan>
</beans>

2.注解

b5ae08743d1b406ea672287b61315934.png

a.使用类注解

注意:默认使用类名的首字母改小写作为id.

f480464c42f44fc2b63fcc7a62f3f813.png

5891c8909ef540d78e7b0752dfc0bf5c.png 254af3f3bf1e45ab8708640a15b5b52e.png

关于这个通配符(**),首先一定是两个(*),然后,ApplicationContext的对象不能使用,BeanFactory对象可以使用,但是效率很低,因为要扫描所有的路径,建议创建合适的目录。

Demo1 demo1 = context.getBean("demo1", Demo1.class);

b.使用方法注解 

1)需要重新写一个类,通过编写多个不同的方法,返回多个同一个类的不同对象.

2)@Bean注解可以给一个对象起多个(包括一个)名字,起了名字之后就不能使用默认的名字了.

3)ApplicationContext的对象可以使用,BeanFactory对象不能使用.

4)代码

public class Student {public void hello() {System.out.println("hello");}
}
@Controller
public class StudentBeans {@Bean(name = {"s1", "s2"})public Student student() {Student student = new Student();return student;}}
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");Student student = context.getBean("s2", Student.class);System.out.println(student);}
}

3.更加简单的获取Bean对象的方法(常用)

a.属性注入(不建议使用,但是目前最常用)

注意:不能在main方法中使用. 

代码实现:

@Service
public class Student {public void hello() {System.out.println("hello");}
}
@Controller
public class StudentBeans {@Autowiredprivate Student student;public void print1() {student.hello();}public void hi() {System.out.println("hi");}
}
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");StudentBeans sc = context.getBean("studentBeans", StudentBeans.class);sc.print1();}
}

缺点: 

b.set注入(很危险,不推荐使用)

代码实现:

@Service
public class Student {public void hello() {System.out.println("hello");}
}
@Controller
public class StudentBeans {private Student student;@Autowiredpublic void setStudent(Student student) {this.student = student;}public void print1() {student.hello();}public void hi() {System.out.println("hi");}
}
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");StudentBeans sc = context.getBean("studentBeans", StudentBeans.class);sc.print1();}
}

优点:相较于属性注入,更符合单一设计原则. 

缺点: 

c.构造方法注入(推荐使用)

 

 注意:

 代码:

@Service
public class Student {public void hello() {System.out.println("hello");}
}
@Controller
public class StudentBeans {private Student student;//@Autowiredpublic StudentBeans(Student student) {this.student = student;}public void print1() {student.hello();}public void hi() {System.out.println("hi");}
}
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");StudentBeans sc = context.getBean("studentBeans", StudentBeans.class);sc.print1();}
}

 优点:

 缺点:没有属性注入简单.

d.

如果使用@Bean注解,可以注入一个类的多个对象,那么@Resource和@Autowired需要根据对象名(@Bean注解的方法名) 来获取指定对象.

1)@Resource需要设置参数(@Resource(name = "?")).

2)@Autowired有两种方法

a)对象名和@Bean注解的方法名相同,但是不推荐使用,如果@Bean注解的方法是别人写的,这样会沿用别人的名字,可能会发生命名不规范.

b)同时使用@Qualifier("??").

代码

public class Student {private String hello;public Student(String hello) {this.hello = hello;}public void hello() {System.out.println(hello);}
}
@Component
public class StudentBeans {@Beanpublic Student student1() {Student student = new Student("1");return student;}@Beanpublic Student student2() {Student student = new Student("2");return student;}}

 

@Controller
public class Test {
//    @Autowired
//    private Student student1;
//    public void print1() {
//        student1.hello();
//    }//    @Autowired
//    @Qualifier("student2")
//    private Student student;
//        public void print1() {
//        student.hello();
//    }@Resource(name = "student2")private Student student;public void print1() {student.hello();}
}
public class App {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");Test test1 = context.getBean("test", Test.class);test1.print1();}
}

四.五大类注解(重点)

1.作用

2.区别:@Component是其它四个类注解的父类.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/120954.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

基于微信小程序的电动车智能充电系统设计与实现(源码+lw+部署文档+讲解等)

文章目录 前言运行环境说明用户的主要功能有&#xff1a;管理员的主要功能有&#xff1a;具体实现截图详细视频演示为什么选择我自己的网站自己的小程序&#xff08;小蔡coding&#xff09;有保障的售后福利 代码参考论文参考源码获取 前言 &#x1f497;博主介绍&#xff1a;✌…

jvm内存分配与回收策略

自动内存管理 解决两个问题 自动给对象分配内存 对象一般堆上分配&#xff08;而实际上也有可能经过即时编译后被拆散为标量类型并间接地在栈上分配&#xff09; 新生对象通常会分配在新生代&#xff0c;少数情况下&#xff08;例如对象大小超过一定阈值&#xff09;也可能…

C/C++跨平台构建工具CMake入门

文章目录 1.概述2.环境准备2.1 安装编译工具2.2 安装CMake 3.编译一个示例程序总结 1.概述 本人一直对OpenGL的3d渲染很感兴趣&#xff0c;但是苦于自己一直是Android开发&#xff0c;没有机会接触这方面的知识。就在最近吗&#xff0c;机会来了&#xff0c;以前一个做3D渲染的…

Oracle的递归公共表表达式

查询节点id为2的所有子节点的数据&#xff0c;包括向下级联 WITH T1 (id, parent_id, data) AS (SELECT id, parent_id, dataFROM nodesWHERE id 2UNION ALLSELECT t.id, t.parent_id, t.dataFROM nodes tJOIN T1 n ON t.parent_id n.id ) SELECT * FROM T1; --建表语句 C…

系统集成|第十二章(笔记)

目录 第十二章 沟通管理12.1 沟通的基本概念12.2 主要过程12.2.1 规划沟通管理12.2.2 管理沟通12.2.3 控制沟通 12.3 常见问题 上篇&#xff1a;第十一章、项目人力资源管理 下篇&#xff1a;第十三章、干系人管理 第十二章 沟通管理 沟通管理在项目计划、执行、监控过程中具有…

小米云原生文件存储平台化实践:支撑 AI 训练、大模型、容器平台多项业务

小米作为全球知名的科技巨头公司&#xff0c;已经在数百款产品中广泛应用了 AI 技术&#xff0c;这些产品包括手机、电视、智能音箱、儿童手表和翻译机等。这些 AI 应用主要都是通过小米的深度学习训练平台完成的。 在训练平台的存储方案中&#xff0c;小米曾尝试了多种不同的…

《PPT 自我介绍》:一本让你的职场表现更加出色的秘籍?

这里提供一个2000字左右的PPT自我介绍模板制作指南&#xff1a; 自我介绍是面试或工作中常见的情况&#xff0c;利用PPT可以给人留下更深刻的印象。但如何快速且专业地制作一个自我介绍PPT呢?这里给大家介绍几点技巧&#xff1a; 1. 选择一个简洁大方的PPT模板 首先要选择一…

智慧燃气平台的总体架构到底应怎样设计?

关键词&#xff1a;智慧燃气、智慧燃气平台、智能燃气、智能监控 智慧燃气平台功能设计的一些方向和思考&#xff1a; 1、资源统一&#xff0c;管理调度 城市燃气智慧调度运营管理平台收集并且整理出每个业务系统信息&#xff0c;并且根据所整理出的信息结果制定出标准规范&…

Java【手撕链表】LeetCode 143. “重排链表“, 图文详解思路分析 + 代码

文章目录 前言一、两数相加1, 题目2, 思路分析2,1 找到中间结点2.2, 逆序后半段链表2.3, 合并两个链表 3, 代码 前言 各位读者好, 我是小陈, 这是我的个人主页, 希望我的专栏能够帮助到你: &#x1f4d5; JavaSE基础: 基础语法, 类和对象, 封装继承多态, 接口, 综合小练习图书管…

TouchGFX之字体缓存

使用二进制字体需要将整个字体加载到存储器。 在某些情况下&#xff0c;如果字体很大&#xff0c;如大字号中文字体&#xff0c;则这样做可能不可取。 字体缓存使应用能够从外部存储器只能加载显示字符串所需的字母。 这意味着整个字体无需保存到在可寻址闪存或RAM上&#xff…

docker安装apisix全教程包含windows和linux

docker安装apisix 一、Windows安装1、首先需要安装docker和docker compose&#xff0c;如果直接安装docker desktop&#xff0c;会自动安装docker compose。2、重新启动电脑3、访问 Docker 的下载&#xff08;[https://www.docker.com/products/docker-desktop](https://www.do…

【Git】Deepin提示git remote: HTTP Basic: Access denied 错误解决办法

git remote: HTTP Basic: Access denied 错误解决办法 1.提交代码的时候提示2. 原因3.解决方案 1.提交代码的时候提示 git remote: HTTP Basic: Access denied 错误解决办法 2. 原因 本地git配置的用户名、密码与gitlabs上注册的用户名、密码不一致。 3.解决方案 如果账号…