zoukankan      html  css  js  c++  java
  • Spring(二)container

    1.CreateBean

    bean的实例化方式:
     1, 传统的bean的配置方式
        <bean id="bean的全局唯一标识" class="bean对应的全限定名称" />

    工厂类的代码:FactoryBean<要生产的对象>

    /**
     * 一个普通的类,只要实现了FactoryBean<T>接口.那么Spring的容器就会认为他是一个工厂类
     */
    package com.gxxy.spring03.createfactory;
    import org.springframework.beans.factory.FactoryBean;
    public class CreateBeanFactory implements FactoryBean<Hello> {
        @Override
        public Hello getObject() throws Exception {
            Hello he = new Hello();
            he.sayHello();
            return he;
        }
        @Override
        public Class<?> getObjectType() {
            return IHelloDAO.class;
        }
        @Override
        public boolean isSingleton() {
            return false;
        }
    }

    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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean  id="createBean" class="com.gxxy.spring03.createfactory.CreateBeanFactory"></bean>
       <bean id="bean的全局唯一标识" class="bean对应的全限定名称" />
    </beans>

     2, 使用静态工厂方法生产bean的方式
        <bean id="bean的全局唯一标识" class="自定义的一个含有创建bean的方法的普通类"
        factory-method="class中的创建bean的方法名称" />
         ☞ 只要我们使用factory-method了属性, 那么Spring容器就会认为class配置的是一个工厂类

    工厂类的代码:

    package com.gxxy.spring03.createfactory;
    
    public class CreateBeanFactory {
    //注意,方法必须为 static
    public static Hello getObject() throws Exception { Hello he = new Hello(); he.sayHello(); return he; } }

    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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean  id="createBean" class="com.gxxy.spring03.createfactory.CreateBeanFactory" 
        factory-method="getObject"
        ></bean>
    </beans>
    factory-method="class中的创建bean的方法名称"

     3, instance

    工厂代码:

    package com.gxxy.spring03.cinstance;
    
    public class InstanceBeanFactory {
        public static Instance getObject() throws Exception {
            Instance he = new Instance();
            he.sayHello();
            return he;
        }
    }

    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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean  id="instanceBean1" class="com.gxxy.spring03.createfactory.CreateBeanFactory"  factory-method="getObject"></bean>
        instanceBean1的class为工厂类  
        <bean id="instanceBean" class="com.gxxy.spring03.cinstance.Instance" factory-bean="instanceBean1" ></bean>
        instanceBean的class为实例化类,factory-bean里面写工厂的ID
    </beans>

     4, beanFactory

    2.BeanScope

      默认情况下,每一个配置在bean中的对象都是单例的, 如果想让它不是单例的:
          在bean的注解中添加一个属性`scope`, 可以限定bean对象的生命作用域
          取值:
            1)singleton: 单例,默认值
                Spring 容器默认启动的时候会创建一个对象
            2)prototype: 原型(模式, 当每一次获取每个对象的时候都是获取的这个对象的镜像),线程安全
                Spring 容器默认启动的时候不会实例化该对象, 只有用的时候才去实例化
                当创建对象之后Spring容器也就不会去管理这个对象, 需要我们自己实现bean的销毁的方法

    <?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="ScopeBean" 
        class="com.gxxy.spring_03container.scope.ScopeBean"
        scope="prototype"
        >
        </bean>
        <!--
            Scope:
            singleton:单列
            prototype:原型类型(非单例,每次都会创建一个新的对象)
          -->
    </beans>
  • 相关阅读:
    SpringMvc 中绑定 checkbox 标签到form 中的List
    Oracle 误删除 DBF 补救措施
    枚举的作用与场景
    MySql 游标
    IDEA 常用设置
    HDFS源码分析数据块复制之PendingReplicationBlocks
    HDFS源码分析之编辑日志编辑相关双缓冲区EditsDoubleBuffer
    HDFS源码分析EditLog之获取编辑日志输入流
    HDFS源码分析EditLog之读取操作符
    HDFS源码分析之EditLogTailer
  • 原文地址:https://www.cnblogs.com/zhang-bo/p/6627967.html
Copyright © 2011-2022 走看看