Spring IOC容器对Bean管理

一.bean实例化

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

2.通过工厂方法

3.通过静态工厂方法

<?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">

  <!--===========================beans的实例化==============================-->
  <!--===========================beans的构造方法==============================-->
  <bean id="student" class="com.pojo.Student"></bean>
  <!--===========================beans的工厂方法==============================-->
  <bean id="student" class="com.pojo.Student" factory-bean="factory" factory-method="createstu"></bean>
  <bean id="factory" class="com.Factory.BeansFactory"></bean>
  <!--===========================beans的工厂方法==============================-->
  <bean id="student" class="com.Factory.StaticBeansFactory" factory-method="createstu" ></bean>
  
</beans>
public class BeansFactory {
    public Student createstu(){
        System.out.println("===>student的普通工厂方法");
        return new Student();
    }
}
public class StaticBeansFactory {
    public static Student createstu(){
        System.out.println("===>student的静态工厂方法");
        return new Student();
    }
}
public class Student {
    public Student() {
        System.out.println("===>student构造方法");
    }
}
二.bean作用域

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

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

属性值:

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

prototype=====>多例

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

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

<?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">
  
  <!--===========================beans的作用域================================-->
  <bean id="student" class="com.Factory.StaticBeansFactory" factory-method="createstu" scope="prototype"></bean>

</beans>
public class test01 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");
        Student student1 = (Student)applicationContext.getBean("student");
        System.out.println(student1);

        Student student2 = (Student)applicationContext.getBean("student");
        System.out.println(student2);

        System.out.println(student1==student2);
    }
}
三.bean生命周期
1.实例化
2.属性赋值(DI)
3.初始化

3.1接口         InitializingBean

3.2属性         init-method=“”

4.操作使用
5.销毁了

5.1接口         DisposableBean

5.2属性         destory-method=“”

<!--===========================beans的生命周期================================-->
  <bean id="teacher" class="com.pojo.Teacher" init-method="doinit" destroy-method="dodel">
    <property name="name" value="张三"></property>
  </bean>
public class Teacher implements InitializingBean, DisposableBean {
    public Teacher() {
        System.out.println("===>teacher的实例化");
    }

    public String name;

    public void setName(String name) {
        System.out.println("===>teacher的属性赋值");
        this.name = name;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("===>teacher的初始化(接口)");
    }
    public void doinit(){
        System.out.println("===>teacher的初始化(属性)");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("===>teacher的销毁(接口)");
    }
    public void dodel(){
        System.out.println("===>teacher的销毁(属性)");
    }
}
public class test02 {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationcontext.xml");
        Teacher teacher = applicationContext.getBean(Teacher.class);

        System.out.println(teacher);

        applicationContext.close();
    }
}

输出结果:

===>teacher的实例化

===>teacher的属性赋值

===>teacher的初始化(接口)

===>teacher的初始化(属性)

com.pojo.Teacher@6536e911

===>teacher的销毁(接口)

===>teacher的销毁(属性)

  • 17
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值