zoukankan      html  css  js  c++  java
  • Spring学习(4)IOC容器配置bean:定义与实例化

    一.  IOC容器配置

      1. 一些概念

      (1)IOC容器

      定义:具有管理对象和管理对象之间的依赖关系的容器。

      作用:应用程序无需自己创建对象,对象由IOC容器创建并组装。BeanFactory是IOC容器的核心。

      流程:IOC容器根据配置文件,读取配置元数据,通过元数据库对程序中的各个对象进行实例化和装配。Spring与配置文件完全解耦,可以使用其他任何方式进行配置元数据,比如注解、基于java文件、基于属性文件的配置都可以。

      

      (2)Bean:IOC容器管理的应用程序的对象我们称它为bean。

      (3)IOC容器核心接口:org.springframework.beans.BeanFactory、org.springframework.context.ApplicationContext

      BeanFactory提供了IOC容器的最基本功能,ApplicationContext是BeanFactory的扩展,添加了更多的企业级功能的支持。

      XmlBeanFactory:BeanFactory的实现,可以从classpath或者文件系统获取资源,在Spring 4.0中已经不建议使用,直接使用applicationContext的实现类。

      ClassPathXmlApplicationContext、FileSystemXmlApplicationContext是ApplicationContext的实现,分别为从classpath、文件系统获取配置文件。

      例子:

            ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
            BeanFactory beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
    

      

      2. Bean的配置

      (1)Spring IOC容器的目的就是管理bean,这些bean将根据配置文件中bean的定义进行创建,bean定义在容器内部由BeanDefintion对象表示,主要包含以下信息:

      ●全限定类名(FQN):用于定义Bean的实现类;(通过构造器和静态工厂方法进行实例化,必需属性。)

      ●Bean行为定义:这些定义了Bean在容器中的行为;包括作用域(单例、原型创建)、是否惰性初始化及生命周期等;

      ●Bean创建方式定义:说明是通过构造器还是工厂方法创建Bean;

      ●Bean之间关系定义:即对其他bean的引用,也就是依赖关系定义,这些引用bean也可以称之为同事bean 或依赖bean,也就是依赖注入。

      (2)Bean的命名:

        <!-- 指定id,id在IOC容器中唯一-->
        <bean id="helloServiceById" class="self.springmvc.beanConfig.service.impl.HelloServiceByIdImpl"/>
    
        <!-- 不指定id,只配置必须的全限定类名:用于定义Bean的实现类,由IoC容器为其生成一个标识 -->
        <bean class="self.springmvc.beanConfig.service.impl.HelloServiceByTypeImpl"/>
    
        <!-- 指定name,name在IOC容器中唯一-->
        <bean name="helloServiceByName" class="self.springmvc.beanConfig.service.impl.HelloServiceByNameImpl"/>
    
        <!-- 指定id和name,那么id在IOC容器中唯一,name作为别名,IOC会消除name和id的冲突。-->
        <bean name="helloServiceByIdName2" id="helloServiceByIdName2" class="self.springmvc.beanConfig.service.impl.HelloServiceByIdNameImpl"/>
       
       <!-- 多个别名--> <bean name="helloServiceByIdName,helloServiceByIdName1,helloServiceByIdName2,helloServiceByIdName3" id="helloServiceByIdName" class="self.springmvc.beanConfig.service.impl.HelloServiceByIdNameImpl"/>
      <!-- 定义别名--> <alias name="helloServiceByIdName" alias="helloServiceByIdName4"></alias> <alias name="helloServiceByIdName" alias="helloServiceByIdName5"></alias>

      (3)Bean的实例化

      实例化:使用构造器(无参构造器和参数构造器)、静态工厂方法、实例工厂方法。

     <!-- 无参构造函数实例化,如果注释掉该实现类的无参构造函数,那么就会编译报错。 -->
        <bean id="bIHelloService1" class="self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl"/>
        <!-- 参数构造函数实例化 -->
        <bean id="bIHelloService2" class="self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl">
            <constructor-arg index="0" value="Welcome to XiaMen"/>
        </bean>
        <!-- 使用静态工厂方法实例化 -->
        <bean id="bIHelloService3" class="self.springmvc.beanInstantiaton.service.HelloServiceStaticFactory" factory-method="getBean"/>
        <bean id="bIHelloService4" class="self.springmvc.beanInstantiaton.service.HelloServiceStaticFactory" factory-method="getBean">
            <constructor-arg index="0" value="Welcome to XiaMen4"/>
        </bean>
        <!-- 使用实例工厂方法实例化-->
        <!-- 定义实例工厂Bean-->
        <bean id="helloServiceFactoryBean" class="self.springmvc.beanInstantiaton.service.HelloServiceFactory"/>
        <!-- 使用实例工厂Bean实例化Bean-->
        <bean id="bIHelloService5" factory-bean="helloServiceFactoryBean" factory-method="getBean"/>
        <bean id="bIHelloService6" factory-bean="helloServiceFactoryBean" factory-method="getBean">
            <constructor-arg index="0" value="Welcome to XiaMen5"/>
        </bean>
    

      静态工厂类与实例工厂类:

    package self.springmvc.beanInstantiaton.service;
    
    import self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl;
    import self.springmvc.beanInstantiaton.service.inter.HelloService;
    
    /**
     * Description
     */
    public class HelloServiceStaticFactory {
        public static HelloService getBean(){
            return new HelloServiceImpl();
        }
        public static HelloService getBean(String msg){
            return new HelloServiceImpl(msg);
        }
    }
    
    package self.springmvc.beanInstantiaton.service;
    
    import self.springmvc.beanInstantiaton.service.impl.HelloServiceImpl;
    import self.springmvc.beanInstantiaton.service.inter.HelloService;
    
    /**
     * Description
     */
    public class HelloServiceFactory {
        public  HelloService getBean(){
            return new HelloServiceImpl();
        }
        public  HelloService getBean(String msg){
            return new HelloServiceImpl(msg);
        }
    }
    

      

        

  • 相关阅读:
    http请求头和响应头详细解释
    http协议POST请求头content-type主要的四种取值
    什么是精准测试
    测试管理(管事篇)
    有赞全链路压测方案设计与实施详解
    饿了么全链路压测平台的实现与原理
    京东全链路压测军演系统(ForceBot)架构解密
    java Apache common-io 讲解
    CentOS 7.0 安装go 1.3.1
    异常
  • 原文地址:https://www.cnblogs.com/knsbyoo/p/9217324.html
Copyright © 2011-2022 走看看