Spring實例化Bean的三種方式

1、簡介

  Spring 實例化 Bean有三種方式:構造函數實例化、工廠方法實例化以及靜態工廠方法實例化。兩種 Bean類型:一種是普通的Bean,另外一種則是工廠Bean。其中,工廠Bean中返回的不是指定的Class的實例,而是其內部方法 getObject 的返回對象。在Spring的內部實現中,存在不少的工廠方法bean。spring

2、實例化Bean的三種方式

  一、新建bean對象 Humanapache

package com.cfang.prebo.spring;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;

@Data
@NoArgsConstructor
@RequiredArgsConstructor
public class Human {

    @NonNull
    private Integer id;
    @NonNull
    private String name;
    private String text;
}

  二、新建工廠類 HumanFactoryapp

package com.cfang.prebo.spring;

public class HumanFactory {

    /**
     * 工廠方法實例化bean
     */
    public Human initHuman() {
        return new Human();
    }
    
    public Human initHuman(int id, String name) {
        return new Human(id, name);
    }
    
    /**
     * 靜態工廠方法實例化bean
     */
    public static Human staticInitHuman() {
        return new Human();
    }
}

  三、新建配置文件 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" 
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

        <!-- 無參構造 -->
        <bean id="human" class="com.cfang.prebo.spring.Human"></bean>
        <!-- 有參構造 -->
        <bean id="human2" class="com.cfang.prebo.spring.Human">
            <constructor-arg index="0" value="1"/>
            <constructor-arg index="1" value="huah"/>
        </bean>
        <!-- 工廠方法 -->
        <bean id="humanFactory" class="com.cfang.prebo.spring.HumanFactory"></bean>
        <bean id="human3" factory-bean="humanFactory" factory-method="initHuman"></bean>
        <!-- 靜態工廠方法 -->
        <bean id="human4" class="com.cfang.prebo.spring.HumanFactory" factory-method="staticInitHuman"></bean>
</beans>

  四、單元測試類 TestBeanLoad單元測試

package com.cfang.prebo.spring;

import org.apache.xbean.spring.context.ClassPathXmlApplicationContext;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestBeanLoad {
    
    private ApplicationContext context;
    
    @Before
    public void initXml() {
        log.info("test init...");
//        context = new FileSystemXmlApplicationContext("src/test/resources/applicationContext.xml");
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
    }
    
    @After
    public void closeXml() {
        log.info("test complete...");
    }
    
    /**
     *     無參構造實例化bean
     */
    @Test
    public void test1() {
        Human human = (Human) context.getBean("human");
        log.info("無參構造實例化bean : {}", human);
    }
    
    /**
     *     有參構造實例化bean
     */
    @Test
    public void test2() {
        Human human2 = (Human) context.getBean("human2");
        log.info("有參構造實例化bean : {}", human2);
    }
    
    /**
     *     工廠方法實例化bean
     */
    @Test
    public void test3() {
        Human human3 = (Human) context.getBean("human3");
        log.info("工廠方法實例化bean : {}", human3);
    }
    
    /**
     *     靜態工廠方法實例化bean
     */
    @Test
    public void test4() {
        Human human4 = (Human) context.getBean("human4");
        log.info("靜態工廠方法實例化bean : {}", human4);
    }
    
}
相關文章
相關標籤/搜索