zoukankan      html  css  js  c++  java
  • Spring实例化Bean的三种方式

    一、简介

      Spring 实例化 Bean有三种方式:构造函数实例化、工厂方法实例化以及静态工厂方法实例化。两种 Bean类型:一种是普通的Bean,另一种则是工厂Bean。其中,工厂Bean中返回的不是指定的Class的实例,而是其内部方法 getObject 的返回对象。在Spring的内部实现中,存在很多的工厂方法bean。

    二、实例化Bean的三种方式

      1、新建bean对象 Human

    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;
    }

      2、新建工厂类 HumanFactory

    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();
        }
    }

      3、新建配置文件 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>

      4、单元测试类 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);
        }
        
    }
  • 相关阅读:
    [导入]【翻译】WF从入门到精通(第十三章):打造自定义活动
    [导入]关于网页标准与JAVAScript执行的问题
    html包含html文件的方法
    [导入]C#加密方法汇总
    8、步步为营VS 2008 + .NET 3.5(8) DLINQ(LINQ to SQL)之面向对象的添加、查询、更新和删除
    [导入]【翻译】WF从入门到精通(第十五章):工作流和事务
    [导入]存储过程得到某个表的所有字段信息
    1、步步为营VS 2008 + .NET 3.5(1) VS 2008新特性之Multi Targeting(多定向)、Web Designer and CSS(集成了CSS的web设计器)和Nested Master Page(嵌套母版页)
    [导入]vbs修改注册表
    正则表达式30分钟入门教程
  • 原文地址:https://www.cnblogs.com/eric-fang/p/11718672.html
Copyright © 2011-2022 走看看