spring中Bean的实例化方式

本文详细介绍了Spring框架中Bean的三种实例化方式:无参数构造、静态工厂方法和实例工厂方法,并提供了具体的代码示例和配置说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 无参数构造
    对于这种方式,注意Bean类中必须提供无参数构造。

创建bean类

package com.itcast.bean;

public class Bean1 {
    public Bean1(){
        System.out.println("bean1的无参构造");
    }
    public void show(){
        System.out.println("bean1 show...");
    }
}

在applicationContext.xml配置文件中添加属性

 <bean id="bean1" class="com.itcast.bean.Bean1">

测试:
三种方式均可获得bean实例
注意:FileSystemXmlApplicationContext相比ClassPathXmlApplicationContext加载配置文件时要添加src路径

public class BeanTest {
    @Test
    public void test1() {
        //使用beanFactory获取bean1实例
        Resource resource = new ClassPathResource("applicationContext.xml");
        BeanFactory factory = new XmlBeanFactory(resource);
        Bean1 bean1 = (Bean1) factory.getBean("bean1");
        bean1.show();
    }
        @Test
    public void Test2() {
     //使用ApplicationContext 获取bean1实例
        ApplicationContext ApplicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean1 bean1 = (Bean1) ApplicationContext.getBean("bean1");
        bean1.show();
    }

    @Test
    public void test3() {
        ApplicationContext ApplicationContext = new FileSystemXmlApplicationContext("src/applicationContext.xml");
        Bean1 bean1 = (Bean1) ApplicationContext.getBean("bean1");
        bean1.show();
    }
   }
  1. 静态工厂方法
    需要创建一个工厂类,在工厂类中提供一个static返回bean对象的方法就可以。
    创建bean2类
package com.itcast.bean2;

public class Bean2 {
    public void show(){
        System.out.println("hello bean2....");
    }
}

创建bean2Factory类

package com.itcast.bean2;

public class Bean2Factory {
    public static Bean2 createBean2(){
        return new Bean2();
    }
}

在applicationContext.xml配置文件中添加属性

    <bean id="Bean2Factory" class="com.itcast.bean2.Bean2Factory" factory-method="createBean2"></bean>

测试:

    @Test
    public void test4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean2 Bean2 = (Bean2) applicationContext.getBean("Bean2Factory");
        Bean2.show();
    }
  1. 实例工厂方法
    需要创建一个工厂类,在工厂类中提供一个非static的创建bean对象的方法,在配置文件中需要将工厂配置,还需要配置bean
    创建bean3类
package com.itcast.bean3;

public class Bean3 {
    public void show(){
        System.out.println("hello bean3...");
    }
}

创建Bean3Factory类

package com.itcast.bean3;

public class Bean3Factory {
    public Bean3 createBean3(){
        return new Bean3();
    }
}

在applicationContext.xml配置文件中添加属性

    <bean name="Bean3Factory" class="com.itcast.bean3.Bean3Factory"></bean>
    <bean name="Bean3" factory-bean="Bean3Factory" factory-method="createBean3"></bean>

测试:

    @Test
    public void test5(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Bean3 bean3 = (Bean3) applicationContext.getBean("Bean3");
        bean3.show();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值