spring ioc的xml形式实现
导入依赖(idea需要配置 JDK 17或者降低spring-context版本)
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.0.6</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
</dependency>
</dependencies>
一、无参构造方法实现ioc
-
待注入容器的bean定义
public class JavaBean implements JavaBeanInteface{
public void doWork(){
System.out.println("JavaBean.doWork");
}
} -
实现的接口定义
public interface JavaBeanInteface {
} -
.xml文件(放在resouces目录下)定义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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="javaBean" class="com.binbin.ioc.JavaBean"></bean>
</beans> -
获取和使用bean
-
方式一:通过id获取bean
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-01.xml");
JavaBean javaBean = (JavaBean) applicationContext.getBean("javaBean"); // 1.通过id 获取bean对象
javaBean.doWork(); -
方式二:通过bean的类型获取bean
JavaBean javaBean1 = applicationContext.getBean(JavaBean.class);
-
方式三:通过bean的id和类型获取bean
JavaBean javaBean2 = applicationContext.getBean("javaBean",JavaBean.class);
-
方式四:通过bean实现的接口的类型获取bean(确保接口只有一个子类,否则容器不知道获取哪一个)
JavaBean javaBean3 = (JavaBean) applicationContext.getBean(JavaBeanInteface.class);
二、通过静态工厂实现ioc
-
待注入容器的bean定义
public class Car {
private String make;
private String model;// 带参数的构造函数 public Car(String make, String model) { this.make = make; this.model = model; } // getters and setters public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; }
}
-
静态工厂类定义
public class CarFactory {
// 静态工厂方法
public static Car createCar(String make, String model) {
return new Car(make, model);
}
} -
.xml文件(放在resouces目录下)定义bean信息
<bean id="car" class="com.binbin.ioc.CarFactory" factory-method="createCar"> <constructor-arg name="make" value="奔驰"></constructor-arg> <constructor-arg name="model" value="模型"></constructor-arg> </bean> -
获取bean和使用bean
-
通过id获取
-
通过bean类型获取
Car car = (Car) applicationContext.getBean("car");
Car car = applicationContext.getBean(Car.class)
- 注:静态工厂本身不会被实例化,不会存入ioc容器
三、通过实例工厂实现ioc
-
待注入容器的bean定义
public class Car {
private String make;
private String model;// 带参数的构造函数 public Car(String make, String model) { this.make = make; this.model = model; } // getters and setters public String getMake() { return make; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; }
}
-
工厂类定义
public class CarFactory {
public Car createCarByInstantiation(String make, String model){
return new Car(make, model);
}
} -
.xml文件(放在resouces目录下)定义bean信息
-
工厂本身也会被实例化注入容器
-
指定factory-bean和factory-method
-
构造函数有参数指定参数即可
<?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">
<bean id="carFactory1" class="com.binbin.ioc.CarFactory">
</bean>
<bean id="car1" factory-bean="carFactory1" factory-method="createCarByInstantiation">
<constructor-arg name="make" value="Toyota" />
<constructor-arg name="model" value="Camry" />
</bean>
</beans>
- 获取bean和使用bean
-
通过id获取bean
Car car1 = (Car) applicationContext.getBean("car1");
-
通过bean的类型获取bean
Car car1 = applicationContext.getBean(Car.class);
-
通过id和bean类型获取bean
Car car1 = (Car) applicationContext.getBean("car1",Car.class);
小结
总的来说,大概几个步骤,搭建maven工程、引入依赖、定义类、xml定义类信息、实例化容器ClassPathXmlApplicationContext、通过bean的id、类型获取到bean