<bean id="..." [1] class="..." [2]>
<!-- collaborators and configuration for this bean go here -->
</bean>
<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
https://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="..." [1] class="..." [2]>
bean>
<bean id="..." class="...">
bean>
beans>
Spring IoC 容器管理一个或多个组件。这些 组件是使用你提供给容器的配置元数据(例如,以 XML
定义的形式)创建的。
id
属性是标识单个 Bean 定义的字符串。class
属性定义 Bean 的类型并使用完全限定的类名。ApplicationContext
构造函数的位置路径是资源字符串地址,允许容器从各种外部资源(如本地文件系统、Java CLASSPATH
等)加载配置元数据。//实例化ioc容器,读取外部配置文件,最终会在容器内进行ioc和di动作
ApplicationContext context =
new ClassPathXmlApplicationContext("services.xml", "daos.xml");
//创建ioc容器对象,指定配置文件,ioc也开始实例组件对象
ApplicationContext context = new ClassPathXmlApplicationContext("services.xml", "daos.xml");
//获取ioc容器的组件对象
PetStoreService service = context.getBean("petStore", PetStoreService.class);
//使用组件对象
List<String> userList = service.getUsernameList();
定义的形式)。<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>6.0.6version>
dependency>
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiter-apiartifactId>
<version>5.3.1version>
dependency>
dependencies>
package com.alex.ioc;
public class HappyComponent {
//默认包含无参数构造函数
public void doWork() {
System.out.println("HappyComponent.doWork");
}
}
<!-- 实验一 [重要]创建bean -->
<bean id="happyComponent" class="com.alex.ioc.HappyComponent"/>
public class ClientService {
private static ClientService clientService = new ClientService();
private ClientService() {}
public static ClientService createInstance() {
return clientService;
}
}
<bean id="clientService"
class="examples.ClientService"
factory-method="createInstance"/>
public class DefaultServiceLocator {
private static ClientServiceImplclientService = new ClientServiceImpl();
public ClientService createClientServiceInstance() {
return clientService;
}
}
<bean id="serviceLocator" class="examples.DefaultServiceLocator">
bean>
<bean id="clientService"
factory-bean="serviceLocator"
factory-method="createClientServiceInstance"/>
public class UserDao {
}
public class UserService {
private UserDao userDao;
public UserService(UserDao userDao) {
this.userDao = userDao;
}
}
<beans>
<bean id="userService" class="x.y.UserService">
<constructor-arg ref="userDao"/>
bean>
<bean id="userDao" class="x.y.UserDao"/>
beans>
public class UserDao {
}
public class UserService {
private UserDao userDao;
private int age;
private String name;
public UserService(int age , String name ,UserDao userDao) {
this.userDao = userDao;
this.age = age;
this.name = name;
}
}
<beans>
<bean id="userService" class="x.y.UserService">
<constructor-arg value="18"/>
<constructor-arg value="赵伟风"/>
<constructor-arg ref="userDao"/>
bean>
<bean id="userDao" class="x.y.UserDao"/>
beans>
<beans>
<bean id="userService" class="x.y.UserService">
<constructor-arg name="name" value="赵伟风"/>
<constructor-arg name="userDao" ref="userDao"/>
<constructor-arg name="age" value="18"/>
bean>
<bean id="userDao" class="x.y.UserDao"/>
beans>
<beans>
<bean id="userService" class="x.y.UserService">
<constructor-arg index="1" value="赵伟风"/>
<constructor-arg index="2" ref="userDao"/>
<constructor-arg index="0" value="18"/>
bean>
<bean id="userDao" class="x.y.UserDao"/>
beans>
public Class MovieFinder{
}
public class SimpleMovieLister {
private MovieFinder movieFinder;
private String movieName;
public void setMovieFinder(MovieFinder movieFinder) {
this.movieFinder = movieFinder;
}
public void setMovieName(String movieName){
this.movieName = movieName;
}
// business logic that actually uses the injected MovieFinder is omitted...
}
<bean id="simpleMovieLister" class="examples.SimpleMovieLister">
<property name="movieFinder" ref="movieFinder" />
<property name="movieName" value="消失的她"/>
bean>
<bean id="movieFinder" class="examples.MovieFinder"/>
总结:
依赖注入(DI)包含引用类型和基本数据类型,同时注入的方式也有多种!主流的注入方式为setter方法注入和构造函数注入!
需要特别注意:引用其他bean,使用ref属性。直接注入基本类型值,使用value属性。
//方式1:实例化并且指定配置文件
//参数:String...locations 传入一个或者多个配置文件
ApplicationContext context =
new ClassPathXmlApplicationContext("services.xml", "daos.xml");
//方式2:先实例化,再指定配置文件,最后刷新容器触发Bean实例化动作 [springmvc源码和contextLoadListener源码方式]
ApplicationContext context =
new ClassPathXmlApplicationContext();
//设置配置配置文件,方法参数为可变参数,可以设置一个或者多个配置
context.setConfigLocations("services.xml", "daos.xml");
//后配置的文件,需要调用refresh方法,触发刷新配置
context.refresh();
//方式1: 根据id获取
//没有指定类型,返回为Object,需要类型转化!
HappyComponent happyComponent =
(HappyComponent) iocContainer.getBean("bean的id标识");
//使用组件对象
happyComponent.doWork();
//方式2: 根据类型获取
//根据类型获取,但是要求,同类型(当前类,或者之类,或者接口的实现类)只能有一个对象交给IoC容器管理
//配置两个或者以上出现: org.springframework.beans.factory.NoUniqueBeanDefinitionException 问题
HappyComponent happyComponent = iocContainer.getBean(HappyComponent.class);
happyComponent.doWork();
//方式3: 根据id和类型获取
HappyComponent happyComponent = iocContainer.getBean("bean的id标识", HappyComponent.class);
happyComponent.doWork();
// 根据类型来获取bean时,在满足bean唯一性的前提下,其实只是看:『对象 instanceof 指定的类型』的返回结果,
// 只要返回的是true就可以认定为和类型匹配,能够获取到。
public class BeanOne {
//周期方法要求: 方法命名随意,但是要求方法必须是 public void 无形参列表
public void init() {
// 初始化逻辑
}
}
public class BeanTwo {
public void cleanup() {
// 释放资源逻辑
}
}
<beans>
<bean id="beanOne" class="examples.BeanOne" init-method="init" />
<bean id="beanTwo" class="examples.BeanTwo" destroy-method="cleanup" />
beans>
标签声明Bean,只是将Bean的信息配置给SpringIoC容器!
标签对应的信息转成Spring内部 BeanDefinition
对象,BeanDefinition
对象内,包含定义的信息(id,class,属性等等)!BeanDefinition
与类
概念一样,SpringIoC容器可以根据BeanDefinition
对象反射创建多个Bean对象实例。取值 | 含义 | 创建对象的时机 | 默认值 |
---|---|---|---|
singleton | 在 IOC 容器中,这个 bean 的对象始终为单实例 | IOC 容器初始化时 | 是 |
prototype | 这个 bean 在 IOC 容器中有多个实例 | 获取 bean 时 | 否 |
取值 | 含义 | 创建对象的时机 | 默认值 |
---|---|---|---|
request | 请求范围内有效的实例 | 每次请求 | 否 |
session | 会话范围内有效的实例 | 每次会话 | 否 |
<bean id="happyMachine8" scope="prototype" class="com.alex.ioc.HappyMachine">
<property name="machineName" value="happyMachine"/>
bean>
<bean id="happyComponent8" scope="singleton" class="com.alex.ioc.HappyComponent">
<property name="componentName" value="happyComponent"/>
bean>
@Test
public void testExperiment08() {
ApplicationContext iocContainer = new ClassPathXmlApplicationContext("配置文件名");
HappyMachine bean = iocContainer.getBean(HappyMachine.class);
HappyMachine bean1 = iocContainer.getBean(HappyMachine.class);
//多例对比 false
System.out.println(bean == bean1);
HappyComponent bean2 = iocContainer.getBean(HappyComponent.class);
HappyComponent bean3 = iocContainer.getBean(HappyComponent.class);
//单例对比 true
System.out.println(bean2 == bean3);
}