目录
[1.1 概述](#1.1 概述)
[1.2 优点](#1.2 优点)
[二、Spring IOC容器](#二、Spring IOC容器)
[2.1 关键组件解析](#2.1 关键组件解析)
[2.2 IOC(接口)](#2.2 IOC(接口))
[3.1 Bean管理以及操作的两种方式](#3.1 Bean管理以及操作的两种方式)
[3.2 基于xml配置文件的方式实现Bean管理和注入属性](#3.2 基于xml配置文件的方式实现Bean管理和注入属性)
[3.3 基于注解的方式实现Bean管理和注入属性](#3.3 基于注解的方式实现Bean管理和注入属性)
一、Spring框架
1.1 概述
Spring 是一个开放源代码的设计层面框架 ,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。
它是为了解决企业应用开发的复杂性 而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。
Spring的核心是控制反转(IOC)和面向切面(AOP)。简单来说,Spring是一个分层的JavaSE/EEfull-stack(一站式) 轻量级开源框架。

1.2 优点
- 方便解耦,简化开发,Spring就是一个大工厂,可以将所有对象创建和依赖关系维护,交给Spring管理,这也是IOC的作用。
- AOP编程的支持,Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能。
- 声明式事务的支持,只需要通过配置就可以完成对事务的管理,而无需手动编程。
- 方便程序的测试,Spring对Junit4支持,可以通过注解方便的测试Spring程序。
- 方便集成各种优秀框架,Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts2、Hibernate、MyBatis等)的直接支持。
- 降低JavaEE API的使用难度,Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail等),都提供了封装,使这些API应用难度大大降低。
二、Spring IOC容器
IOC(Inversion of Control)控制反转,是一种设计思想。Spring 通过 IoC 容器实现对象创建、依赖管理和生命周期控制,可以用来减低计算机代码之间的耦合度。
2.1 关键组件解析
- Bean 定义 :XML 中
<bean>
标签或注解如@Component
定义的类 - Bean 工厂 :
BeanFactory
接口,负责 Bean 的创建 - 应用上下文 :
ApplicationContext
接口,提供高级容器功能
java
// 通过 ApplicationContext 获取 Bean
ApplicationContext ac = new ClassPathXmlApplicationContext("Spring.xml");
User user = ac.getBean("user", User.class);
2.2 IOC(接口)
-
IOC思想是基于IOC容器 完成的,IOC的底层就是对象工厂
-
Spring里边提供了IOC容器的实现的两种方式
(1) BeanFactroy:IOC容器是Spring内部的使用接口,不提供给开发人员使用
(2)ApplicationContext:BeanFactory接口的子接口,提供了更多更强大的功能,一般由开发人员进行使用
java
public class Atest {
@Test
public void a(){
//BeanFactory:加载配置文件的时候不会去创建对象,在使用对象的时候才会去创建对象
BeanFactory factory=new ClassPathXmlApplicationContext("spring.xml");
Animal animal=(Animal) factory.getBean("animal");
//ApplicationContext:BeanFactory接口的子接口,加载配置文件的时候会把对象创建
ApplicationContext ac=new ClassPathXmlApplicationContext("Spring.xml");
Animal animal=(Animal) ac.getBean("animal");
}
}
三、Spring框架的Bean管理
3.1 Bean管理以及操作的两种方式

3.2 基于xml配置文件的方式实现Bean管理和注入属性
- 基于xml方式创建对象

2.基于xml方式注入属性
(1)依赖注入的概述
IOC:Inverse of Control,控制反转,将对象的创建权反转给Spring
DI:Dependency Injection,依赖注入,就是注入属性
(2)属性的set方法注入值
编写属性,提供该属性对应的set方法,编写配置文件完成属性值的注入
java
public class User {
// 编写成员属性,一定需要提供该属性的set方法
//IOC容器底层就通过属性的set方法方式注入值
private int age;
private String name;
private Animal animal;
public User(int age, String name, Animal animal) {
this.age = age;
this.name = name;
this.animal = animal;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Animal getAnimal() {
return animal;
}
public void setAnimal(Animal animal) {
this.animal = animal;
}
@Override
public String toString() {
return "User{" +
"age=" + age +
", name='" + name + '\'' +
", animal=" + animal +
'}';
}
}
XML
<!‐‐DI:依赖注入‐‐>
<bean id="user" class="com.qcby.entity.User" >
<!--使用property完成属性注入
name:类里面属性名称
value:向属性注入值
ref:对象映射-->
<property name="age" value="18"></property>
<property name="name" value="张三"></property>
<property name="animal" ref="animal"></property>
</bean>
<bean id="animal" class="com.qcby.entity.Animal" />
java
@Test
public void run1(){
//创建spring工厂,加载配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
//获取bean对象
User user = ac.getBean("user", User.class);
System.out.println(user.toString());
}
(3)数组,集合(List,Set,Map)等的set注入
java
public class Animal {
private String [] strs;
private List<String> list;
private Map<String,String> map;
@Override
public String toString() {
return "Animal{" +
"strs=" + Arrays.toString(strs) +
", list=" + list +
", map=" + map +
'}';
}
public Animal(String[] strs, List<String> list, Map<String, String> map) {
this.strs = strs;
this.list = list;
this.map = map;
}
public String[] getStrs() {
return strs;
}
public void setStrs(String[] strs) {
this.strs = strs;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
public Map<String, String> getMap() {
return map;
}
public void setMap(Map<String, String> map) {
this.map = map;
}
}
XML
<!‐‐给集合属性注入值‐‐>
<bean id="animal" class="com.qcby.entity.Animal">
<property name="strs">
<array>
<value>美美</value>
<value>小凤</value>
</array>
</property>
<property name="list">
<list>
<value>熊大</value>
<value>熊二</value>
</list>
</property>
<property name="map">
<map>
<entry key="aaa" value="老王"/>
<entry key="bbb" value="小王"/>
</map>
</property>
</bean>
3.3 基于注解的方式实现Bean管理和注入属性
注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值...) ;使用注解时,注解作用在类/方法/属性上面;使用注解的目的是为了简化XML配置。
- Spring针对Bean管理中创建对象提供的注解
- @Component 普通的类
- @Controller 表现层
- @Service 业务层
- @Repository 持久层
- 用注解的方式创建对象
(1)在需要管理的类上添加@Component注解
java
@Component(value = "user")
public class User {
public void run(){
System.out.println("人跑得很快-------");
}
}
(2)编写配置文件,开启注解扫描
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"
xmlns:context="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 http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解扫描 com.qcby所有的包中的所有的类-->
<context:component-scan base-package="com.qcby.entity"/>
</beans>
(3)编写测试方法
java
public class Atest {
@Test
public void a(){
ApplicationContext ac=new ClassPathXmlApplicationContext("Spring.xml");
Animal animal=(Animal) ac.getBean("animal");
animal.fly();
}
}
- 用注解的方实现属性注入
- @Value 用于注入普通类型(String,int,double等类型)
- @Autowired 默认按类型进行自动装配(引用类型)
- @Qualifier 不能单独使用必须和@Autowired一起使用,强制使用名称注入
- @Resource Java提供的注解,也被支持。使用name属性,按名称注入
java
package com.qcby.entity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component(value = "animal")
public class Animal {
// 注解注入值,属性set方法是可以省略不写的。
// 只有一个属性,属性的名称是value,value是可以省略不写的
@Value("大奔2")
private String cname;
@Value(value = "400000")
private Double money;
// 也不用提供set方法
// 按类型自动装配的注解,和id名称没有关系
@Autowired
// 按id的名称注入,Qualifier不能单独使用,需要Autowired一起使用。
// @Qualifier(value = "person")
// @Resource Java提供的注解,按名称注入对象,属性名称是name
// @Resource(name = "person")
private User user;
@Override
public String toString() {
return "Animal{" +
"cname='" + cname + '\'' +
", money=" + money +
", user=" + user +
'}';
}
}
java
public class Atest {
@Test
public void a(){
ApplicationContext ac=new ClassPathXmlApplicationContext("Spring.xml");
Animal animal=(Animal) ac.getBean("animal");
System.out.println(animal.toString());
}
}
运行结果:

四、总结
Spring IOC作为Spring框架的核心,经历了从XML配置到注解驱动的演进,如今在Spring Boot中达到新的高度。其核心价值在于:
- 解耦性:提升系统可维护性和扩展性
- 灵活性:支持模块化开发和环境适配
- 可测试性:轻松构建隔离的测试环境
随着云原生和微服务架构的普及,IOC容器在服务发现、配置中心等场景发挥着越来越重要的作用。理解IOC原理并合理运用,将帮助开发者构建更健壮、灵活的现代应用。