BeanFactory: 提供一种延迟加载思想来创建对象。bean对象什么时候使用什么时候创建
ApplicationContext: 提供一种立即加载思想来创建对象。只要解析完配置文件,就立马创建bean对象。
bean对象: Car.java
package com.zj;
public class Car {
private String brand;
private double price;
public Car(){
System.out.println("Car对象创建了 ");
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
配置文件:applicationContext.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/context http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="car" class="com.zj.Car">
<property name="brand" value="Audo"></property>
<property name="price" value="300000"></property>
</bean>
</beans>
测试主函数(使用ApplicationContext时):Main.java
package com.zj;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.sql.SQLException;
public class Main {
public static void main(String args[]) throws SQLException {
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
}
}
显示结果:
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Car对象创建了
Process finished with exit code 0
使用BeanFactory,未使用对象时:
package com.zj;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.sql.SQLException;
public class Main {
public static void main(String args[]) throws SQLException {
// ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Car car = (Car) factory.getBean("car");
System.out.println(car);
}
}
显示结果:
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Process finished with exit code 0
使用BeanFactory,使用对象时:
package com.zj;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import java.sql.SQLException;
public class Main {
public static void main(String args[]) throws SQLException {
// ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
Car car = (Car) factory.getBean("car");
System.out.println(car);
}
}
显示结果:
信息: Loading XML bean definitions from class path resource [applicationContext.xml]
Car对象创建了
Car{brand='Audo', price=300000.0}
Process finished with exit code 0