spring中bean创建的两种规则:BeanFactory和ApplicationContext

 

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值