zoukankan      html  css  js  c++  java
  • spring学习(03)之bean实例化的三种方式

    bean实体例化的三种方式

    在spring中有三中实例化bean的方式:

    一、使用构造器实例化;(通常使用的一个方法,重点)

    二、使用静态工厂方法实例化;

    三、使用实例化工厂方法实例化

    第一种、使用构造器实例化;

    这种实例化的方式可能在我们平时的开发中用到的是最多的,因为在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="user" class="cn.lmn.ioc.User"></bean>
    
    </beans>

     id是对象的名称,class是要实例化的类。

    在这个类里面还要有个无参的构造,不然会出现异常

    第二种 使用静态工厂创建

    (1)创建静态的方法,返回类对象

    package cn.lmn.bean;
    
    public class Bean2Factory {
        //静态方法,返回Bean2对象
        public static Bean2 getBean2(){
            return new Bean2();
        }
    }
    <?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="user" class="cn.lmn.ioc.User"></bean> -->
        <!-- 使用静态工厂创建对象 -->
        <bean id="bean2" class="cn.lmn.bean.Bean2Factory" factory-method="getBean2"></bean>
    </beans>

    最后测试一下

     1 package cn.lmn.test;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7 import cn.lmn.bean.Bean2;
     8 import cn.lmn.ioc.User;
     9 
    10 public class TestIOC {
    11     @Test
    12     public void testUser() {
    13         // 1加载spring的配置文件,创建对象
    14         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    15         // 2得到配置创建的对象
    16     //    User user = (User) context.getBean("user");
    17         Bean2 bean2 = (Bean2) context.getBean("bean2");
    18         System.out.println(bean2);
    19         
    20     }
    21 }

    第三种 使用实例工厂创建

    (1)创建不是静态的方法,返回类对象

    package cn.lmn.bean;
    
    public class Bean3Factory {
        //普通的方法,返回的是Bean2的对象
        public Bean3 getBean3(){
            return new Bean3();
        }
    }
    <?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="user" class="cn.lmn.ioc.User"></bean> -->
        <!-- 使用静态工厂创建对象 -->
        <!-- <bean id="bean2" class="cn.lmn.bean.Bean2Factory" factory-method="getBean2"></bean> -->
        <!-- 使用实例工厂创建对象 -->
        <!-- 创建工厂对象 -->
        <bean id="bean3Factory" class="cn.lmn.bean.Bean3Factory"></bean>
        <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
    </beans>

    最后测试一下

     1 package cn.lmn.test;
     2 
     3 import org.junit.Test;
     4 import org.springframework.context.ApplicationContext;
     5 import org.springframework.context.support.ClassPathXmlApplicationContext;
     6 
     7 import cn.lmn.bean.Bean3;
     8 
     9 public class TestIOC {
    10     @Test
    11     public void testUser() {
    12         // 1加载spring的配置文件,创建对象
    13         ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    14         // 2得到配置创建的对象
    15     //    User user = (User) context.getBean("user");
    16         Bean3 bean3 = (Bean3) context.getBean("bean3");
    17         System.out.println(bean3);
    18         
    19     }
    20 }
  • 相关阅读:
    CentOS7.0 内核(3.10.0-123.el7.x86_64)bug导致KVM物理机重启
    Jenkins控制台输出乱码
    MySQL 主从失败报错:Last_SQL_Errno: 1594
    PHP7添加opcache.so模块
    python 网络爬虫requests模块
    Python 运算符
    tmux使用笔记
    linux使脚本在后台运行
    git 使用钩子直接推送到工作目录
    Git使用笔记
  • 原文地址:https://www.cnblogs.com/limn/p/8406969.html
Copyright © 2011-2022 走看看