SpringIOC容器对Bean管理

一、bean实例化

1.通过构造方法(默认)

2.通过工厂方法 

3.通过静态工厂方法

项目结构: 

 

1. 通过构造方法(默认)
1.1 pojo实体类:

Student:

public class Student {
    public Student() {
        System.out.println("===》执行无参构造方法");
    }
}
1.2 applicationContext.xml:
<bean id="student" class="com.zad.pojo.Student"></bean>-->
1.3 Test01:
public class Test01 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) applicationContext.getBean("student");
        System.out.println(student);

    }
}
 1.4 结果:
===》执行无参构造方法
com.zad.pojo.Student@72d818d1
2.通过工厂方法 
2.1 pojo实体类:

Student同上

2.2 applicationContext.xml:
<bean id="student" class="com.zad.pojo.Student" factory-bean="factory" factory-method="createStu"></bean>
<bean id="factory" class="com.zad.factory.BeansFactory"/>
2.3  BeansFactory:
public class BeansFactory {
    public Student createStu(){
        System.out.println("===> 执行普通工厂");
        return new Student();
    }
}
2.4 结果:
===> 执行普通工厂
===》执行无参构造方法
com.zad.pojo.Student@5025a98f
3.通过静态工厂方法

3.1 applicationContext.xml:

 <bean id="student" class="com.zad.factory.StaticBeansFactory" factory-method="createSte">

3.2 StaticBeansFactory:

public class StaticBeansFactory {
    public  static Student createSte(){
        System.out.println("===>执行static工厂");
        return new Student();
    }
}

 3.3 结果:

===>执行static工厂
===》执行无参构造方法
com.zad.pojo.Student@5649fd9b

二、bean作用域

含义:spring对于创建javaBean实例的方式

语法:<bean scope="属性值"></bean>

属性值:

         singleton=====>单例(默认)

        prototype=====>多例

        request=======>一个请求创建一个

        session=======>一个会话创建一个

1.实体类:Teacher
public class Teacher {
}
 2.applicationContext.xml:
    <bean id="teacher" class="com.zad.pojo.Teacher" scope="singleton"></bean>
3.Test01:
public class Test01 {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");


        Teacher teacher1 = (Teacher) applicationContext.getBean("teacher");
        Teacher teacher2 = (Teacher) applicationContext.getBean("teacher");
        System.out.println(teacher1==teacher2);
    }
}
4.结果:
true

三、bean生命周期

1.实例化

2.属性赋值(DI)

3.初始化

        3.1接口 InitializingBean

        3.2属性 init-method=“”

4.操作使用

5.销毁了

        5.1接口 DisposableBean

        5.2属性 destory-method=“”

 1.实体类:User
public class User implements InitializingBean {
    private String uname;

    public void setUname(String uname) {
        System.out.println("bean生命周期===》 属性赋值");
        this.uname = uname;
    }

    public User() {
        System.out.println("bean生命周期===》 实例化");
    }

    //接口
    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("bean生命周期===》 初始化(接口)");
    }
    //属性
    public void doinit(){
        System.out.println("bean生命周期===》 初始化(属性)");
    }
    //属性
    public void doDestory(){
        System.out.println("bean生命周期===》 销毁(属性)");
    }
}
2.applicationContext.xml:
 <bean id="user" class="com.zad.pojo.User" init-method="doinit" destroy-method="doDestory"></bean>
3.Test02
public class Test02 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        User user = applicationContext.getBean(User.class);
        System.out.println(user);

        applicationContext.close();
    }
}
4.结果:
bean生命周期===》 实例化
bean生命周期===》 初始化(接口)
bean生命周期===》 初始化(属性)
com.zad.pojo.User@4566e5bd
bean生命周期===》 销毁(属性)

四、总结

SpringIOC容器对Bean管理
    一.bean实例化
        1.通过构造方法(默认)
        2.通过工厂方法
        3.通过静态工厂方法

    二.bean作用域
        含义:spring对于创建javaBean实例的方式
        语法:<bean scope="属性值"></bean>
        属性值:
            singleton=====>单例(默认)
            prototype=====>多例
            request=======>一个请求创建一个
            session=======>一个会话创建一个

    三.bean生命周期
        1.实例化
        2.属性赋值(DI)
        3.初始化
            3.1接口   InitializingBean
            3.2属性   init-method=“”
        4.操作使用
        5.销毁了
            5.1接口   DisposableBean
            5.2属性   destory-method=“”
  • 14
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值