Spring:Spring-IOC实例化bean的常用三种方式

Posted 怒吼的萝卜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring:Spring-IOC实例化bean的常用三种方式相关的知识,希望对你有一定的参考价值。

Spring容器提供了三种对bean的实例化方式:


1)构造器实例化

public class Demo {
    private String name;
    //getter和setter方法略
}
<?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">
    <!-- 构造器实例化 -->
    <bean id="demo1" class="com.oak.junit.day01.Demo"></bean>
</beans>

测试:

@Test
    public void testCtx() {
        //实例化Spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); 
        //取出demo1
        Demo demo1=ctx.getBean("demo1",Demo.class);
        System.out.println(demo1);
    }

 

2)静态工厂方法实例化

public class DemoBeanFactory {
    //静态工厂方法-返回一个Demo实例
    public static Demo staticDemoBean(){
        return new Demo();
    }
}
<?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">
    <!-- 构造器实例化 -->
    <bean id="demo1" class="com.oak.junit.day01.Demo"></bean>
    
    <!-- 静态工厂实例化 class为工厂类 factory-method为静态工厂方法  -->
    <bean id="demo2" class="com.oak.junit.day01.DemoBeanFactory" 
    factory-method="staticDemoBean"></bean>
</beans>

测试:

    @Test
    public void testCtx() {
        //实例化Spring容器
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); 
        //取出demo1
        Demo demo1=ctx.getBean("demo1",Demo.class);
        System.out.println(demo1);
        
        //取出demo2
        Demo demo2=ctx.getBean("demo2",Demo.class);
        System.out.println(demo2);
    }

 

3)实例工厂方法是实例化

工厂类中加入实例工厂方法:

public class Demo {
    private String name;
    //getter和setter方法略
}
public class DemoBeanFactory {
    //静态工厂方法-返回一个Demo实例
    public static Demo staticDemoBean(){
        return new Demo();
    }
    //实例工厂方法-返回一个Demo实例
    public Demo constructorDemoBean(){
        return new Demo();
    }
}
<?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">
    <!-- 构造器实例化 -->
    <bean id="demo1" class="com.oak.junit.day01.Demo"></bean>
    
    <!-- 静态工厂实例化 class为工厂类 factory-method为静态工厂方法  -->
    <bean id="demo2" class="com.oak.junit.day01.DemoBeanFactory" 
    factory-method="staticDemoBean"></bean>
    
    <!-- 先实例化工厂类 -->
    <bean id="demoBeanFactory" class="com.oak.junit.day01.DemoBeanFactory"></bean>
    <!-- 实例工厂实例化 class为工厂类 factory-bean指定工厂类  factory-method为实例工厂方法  -->
    <bean id="demo3" factory-bean="demoBeanFactory" 
    factory-method="constructorDemoBean"></bean>
</beans>

测试:

@Test
        public void testCtx() {
            //实例化Spring容器
            ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml"); 
            //取出demo1
            Demo demo1=ctx.getBean("demo1",Demo.class);
            System.out.println(demo1);
            
            //取出demo2
            Demo demo2=ctx.getBean("demo2",Demo.class);
            System.out.println(demo2);
            
            //取出demo2
            Demo demo3=ctx.getBean("demo3",Demo.class);
            System.out.println(demo3);
        }

 

文章转载至:https://blog.csdn.net/qq_34598667/article/details/83246492

 

以上是关于Spring:Spring-IOC实例化bean的常用三种方式的主要内容,如果未能解决你的问题,请参考以下文章

Java Spring-IOC和DI

spring-ioc

Spring-IOC源码解读3-依赖注入

Spring-IOC学习笔记-05bean的属性

Spring-IOC学习笔记-02基于XML创建Bean

二:Spring-IOC源码