zoukankan      html  css  js  c++  java
  • 吴裕雄天生自然SPRINGSpring Bean

    Spring框架实例化Bean有三种方式:构造方法实例化、静态工厂实例化和实例工厂实例化(其中,最常用的实例方法是构造方法实例化)
    
    演示Bean的实例化过程,具体步骤如下。
    
    1.使用Eclipse创建Web应用并导入JAR包
    
    2.创建实例化Bean的类
    
    3.创建配置类
    
    4.创建测试类
    
    5.运行测试类
    package instance;
    
    public class BeanClass {
        public String message;
    
        public BeanClass() {
            message = "构造方法实例化Bean";
        }
    
        public BeanClass(String s) {
            message = s;
        }
    
    }
    package instance;
    
    public class BeanInstanceFactory {
        public BeanClass createBeanClassInstance() {
            return new BeanClass("调用实例工厂方法实例化Bean");
        }
    
    }
    package instance;
    
    public class BeanStaticFactory {
        private static BeanClass beanInstance = new BeanClass("调用静态工厂方法实例化Bean");
    
        public static BeanClass createInstance() {
            return beanInstance;
        }
    
    }
    package config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import instance.BeanClass;
    import instance.BeanInstanceFactory;
    import instance.BeanStaticFactory;
    
    @Configuration
    public class JavaConfig {
        /**
         * 构造方法实例化
         */
        @Bean(value = "beanClass") // value可以省略
        public BeanClass getBeanClass() {
            return new BeanClass();
        }
    
        /**
         * 静态工厂实例化
         */
        @Bean(value = "beanStaticFactory")
        public BeanClass getBeanStaticFactory() {
            return BeanStaticFactory.createInstance();
        }
    
        /**
         * 实例工厂实例化
         */
        @Bean(value = "beanInstanceFactory")
        public BeanClass getBeanInstanceFactory() {
            BeanInstanceFactory bi = new BeanInstanceFactory();
            return bi.createBeanClassInstance();
        }
    
    }
    package config;
    
    import org.springframework.context.annotation.AnnotationConfigApplicationContext;
    
    import instance.BeanClass;
    
    public class TestBean {
        public static void main(String[] args) {
            // 初始化Spring容器ApplicationContext
            AnnotationConfigApplicationContext appCon = new AnnotationConfigApplicationContext(JavaConfig.class);
            BeanClass b1 = (BeanClass) appCon.getBean("beanClass");
            System.out.println(b1 + b1.message);
            BeanClass b2 = (BeanClass) appCon.getBean("beanStaticFactory");
            System.out.println(b2 + b2.message);
            BeanClass b3 = (BeanClass) appCon.getBean("beanInstanceFactory");
            System.out.println(b3 + b3.message);
            appCon.close();
        }
    
    }

     

  • 相关阅读:
    GIT在Linux上的安装和使用简介心得
    Android开发环境使用到工具的认识心得
    Android系统移植与驱动开发心得
    嵌入式Linux的调试技术
    硬件抽象层——HAL
    Linux代码的重用与强行卸载Linux驱动
    控制发光二极管
    详细讲解Linux驱动程序
    搭建测试环境——针对S3C6410开发板
    有了源代码,当然还需要编译喽!!
  • 原文地址:https://www.cnblogs.com/tszr/p/15310321.html
Copyright © 2011-2022 走看看