一个 bean 就是一个实例化对象
User user = new User()
上面这行代码中的 user, 就是 User 类的实例化对象,即一个 bean(User Bean)
Inversion of Control 控制反转(反转对 bean 的控制)
spring ioc 是一个 bean 容器,我们可以将需要的 bean 放入其中
我在 UserController 中 new UserService() ,我调用 userService 中的方法
她为我提供一个 new 好的 UserService 对象,我调用 userService 中的方法
我无需在意何时创建对象,何时使用对象,何时销毁对象(她来管理对象的生命周期)
我无需使用构造器 new 对象,不用关注构造器需要的参数(当构造器参数变化时,我也无需知道,我只是调用已经创建好的对象中的方法而已)
注册 bean 的方式分为 2 大类 5 小类
| 大类 | 小类 | 介绍 |
| XML 注册 | class /> | 直接类名注册 |
| class fatory-method /> | 静态方法注册 | |
| factory-bean fatory-method /> | 实例方法注册 | |
| @ 注册 | @Configuration @Bean | Java-Based 注册 允许以编程方式配置 bean |
| @Component @Controller @Service @Repository | Annotation-Based 注册 将使用这类注解的类 对应的实例化对象 注册进 IOC 容器 |
直接类名注册
- <bean id="userService"
- class="com.syt.UserService"
- />
静态工厂方法注册
- <bean id="staticUserService"
- class="com.syt.UserServiceStaticFactory"
- factory-method="staticUserService"
- />
- public class UserStaticFactory {
- public static UserService staticUserService() {
- return new UserService();
- }
- }
实例工厂方法注册
- <bean id="userServiceIntanceFactory"
- class="com.syt.UserServiceIntanceFactory"
- />
-
- <bean id="instanceUserService"
- factory-bean="userServiceIntanceFactory"
- factory-method="instanceUserService"
- />
- public class UserServiceIntanceFactory {
- public User intanceUserService() {
- return new UserService();
- }
- }
属性初始值设置
- <bean>
- <property
- name
- value
- />
- bean>
Annotation-Based
- @Component
- public class UserService {
-
- }
Java-Based
- @Configuration
- public class UserServiceConfiguration {
- @Bean
- public UserService userService() {
- // 这里可以对 UserService
- // 进行配置后
- // 再返回配置好的 UserService 对象
- return new UserService();
- }
- }
使用 spring 进行 @ 注册 需要在 beans 配置中配置自动包扫描路径
- <context:component-scan
- base-pakage
- />
Dependency Injection 依赖注入(将 bean 注入进依赖 bean 使用 bean 的类)
获取 IOC 容器中的 bean 的方式
注入 bean 的方式分为 3 大类 5 小类
| 大类 | 小类 | 介绍 |
| 属性注入 | @Autowired / @Resouce | 在代码中添加对应 bean 属性 在 bean 属性上添加这类注解 |
| Setter 注入 | @Autowired / @Resouce | 在代码中添加对应 bean 属性 并且实现对应 setter 方法 在 setter 方法上添加这类注解 |
| class > name ref /> | 使用 XML 在注册的同时注入 ,也需要注册类中有 bean 属性 与 对应 setter 方法 | |
| 构造方法注入 | @Autowired / @Resouce | 在代码中添加对应 bean 属性 并且实现对应构造方法 在构造方法上添加这类注解 |
| class > name ref /> | 使用 XML 在注册的同时注入 ,也需要注册类中有 bean 属性 与 对应 构造方法 |
- public class UserController {
- @Autowired
- private UserService userService;
- }
- public class UserController {
-
- private UserService userService;
-
- @Autowired
- public void SetUserService(UserService userService) {
- this.userService = userService;
- }
- }
- <bean id="propertyUserService"
- class="com.syt.service.UserService"
- >
- <property
- name="userDao"
- ref="userDao"
- />
- bean>
- public class UserController {
-
- private UserService userService;
-
- @Autowired
- public UserController (UserService userService) {
- this.userService = userService;
- }
- }
- <bean id="constructorUserService"
- class="com.syt.service.UserService">
- <constructor-arg
- name="userDao"
- ref="userDao"
- />
- bean>
位置:
一般位于 resources 下,命名为 spring-config.xml 或 applicationContext.xml

基本内容
- "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>
我用的 JDK 为 1.8
- <dependency>
- <groupId>org.springframeworkgroupId>
- <artifactId>spring-contextartifactId>
- <version>5.3.30version>
- dependency>