一. Spring概述
1、框架:半成品软件
高度抽取可重用代码的一种设计,高度通用
框架:多个可重用模块的集合,形成一个某个领域的整体解决方案;
2、Spring
容器(可以管理所有的组件(类))框架;
核心关注:IOC和AOP;
3、Spring(IOC和AOP)

Spring的模块划分图:

建议用哪个模块导入哪个包。
二. IOC 基础

IOC(控制反转)
Inversion(反转) Of Control)
控制:资源的获取方式
主动式:(要什么资源都自己创建)
BookServlet{
BookService bs = new BookService();
AirPlane ap = new AirPlane();
//复杂对象的创建是比较庞大的工程;
}
被动式:资源的获取不是我们自己创建,而是交给一个容器来创建和设置
BookServlet{
BookService bs;
public void test01(){
bs.checkout();
}
}
容器:管理所有的组件(有功能的类)。
假设,BookServlet 受容器管理,BookService 也受容器管理。容器可以自动的探查出哪些组件(类)需要用到另一写组件(类)。容器帮我们创建BookService对象,并把BookService对象赋值过去。
容器:从主动的 new 资源变为被动的接受资源
DI:(Dependency Injection)依赖注入
容器能知道哪个组件(类)运行的时候,需要另外一个类(组件),容器通过反射的形式,将容器中准备好的BookService对象注入(利用反射给属性赋值)到BookServlet中。
只要是 IOC 容器管理的组件,都能使用容器提供的强大功能。
以前是自己 new 对象,现在所有的对象交给容器创建。给容器中注册组件。
举例示范:
1.先在 IDEA 中创建 Mawen 工程,并在 pom.xml 中更改配置(spring所需的依赖)
核心包:
spring-core-5.2.5.RELEASE.jar
spring-expression-5.2.5.RELEASE.jar
spring-beans-5.2.5.RELEASE.jar
spring-context-5.2.5.RELEASE.jar

2. 创建一个类
public class HelloSpring {
private String name;
private String age;
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public void sayHello(){
System.out.println("start....");
System.out.println("hello "+ name+ " "+age+"岁");
System.out.println("end...");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.在 resource 中创建一个 bean.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
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans ">
<!-- 注册一个对象,Spring会自动创建这个对象 -->
<!-- 一个bean标签可以注册一个组件(对象、类)-->
<!--配置bean
id:这个对象的唯一标识
class:注册组件的全类名
-->
<bean id="hello01" class="HelloSpring">
<!-- 使用 property 标签为对象的属性赋值 -->
<!-- name:属性名 value:属性值-->
<property name="name" value="jess"></property>
<property name="age" value="18"></property>
</bean>
</beans>
Bean 其实就是一个new好的对象
4.再写一个测试类
对象由容器创建
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
/*
* 从容器中拿到组件
*/
//ApplicationContext代表 IOC 容器
//ClassPathXmlApplicationContext:当前应用配置文件在 ClassPath下
//根据spring配置文件得到IOC容器对象
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
HelloSpring bean = (HelloSpring)applicationContext.getBean("hello01");
bean.sayHello();
System.out.println(bean);
}
}
执行结果:

new ClassPathXmlApplicationContext("bean.xml"):IOC 容器的配置文件在类路径下(src是类路径的开始)
new FileSystemXmlApplicationContext("D://bean.xml");:IOC 容器的配置文件在磁盘路径下
注意:
- 容器中对象的创建在容器创建完成时就已经创建好了。
- 同一个组件在 IOC 容器中是单实例的(多次获取得到相同的组件),容器启动完成前就创建好了。
- 若容器中没有这个组件(如 HelloSpring02),获取组件声明异常:NoSuchBeanDefinitionException:No bean named “HelloSpring02” is defined
- property: IOC 容器在创建这个组件对象的时候,会利用 setter 方法为 Javabean 的属性进行赋值
- Javabean 的属性名是由 getter/setter 方法决定的。set 去掉后面的一串,首字母小写就是属性名。(一定要使用代码自动生成,以防出错)
本文深入探讨Spring框架的核心概念,包括IOC(控制反转)和DI(依赖注入)原理,详细解析了如何通过Spring容器管理和创建对象,以及如何在实际项目中配置和使用这些功能。
849

被折叠的 条评论
为什么被折叠?



