zoukankan      html  css  js  c++  java
  • day38 07-Spring框架Bean的时候方式

    Spring是自动帮我们创建对象的,有几种创建Bean的方式呢?

    构造方法实例化:(默认无参数)其实就是反射new Instance().

    静态工厂实例化:

    实例工厂实例化:

    一般不会改变它实例化的方式。其他两种了解一下即可。

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- 别去schema,schema是文件,本地的文件,你得引那个头 -->
    
    <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">
    <!-- demo1快速入门================================================= -->
    <!-- 把接口和实现类在这个配置文件中配置,有了实现类的全路径之后到时候才能用工厂反射 -->
       
       <!-- 通过一个<bean>标签来设置类的信息,通过id属性为类起个标识. -->
        <!-- 接口,实现类,配置文件也都有了 -->
        <!-- 现在有一个工厂Spring为我们提供好了,其实就是解析这个XML文件 -->
        <!-- 这个工厂你自己写会不会写?你用dom4j找里面的bean标签,找到class的属性值,然后就可以Class.forName()反射生成类的实例.其实Spring
             也是这么做的,只不过工厂由Spring提供好了
         -->
        <bean id="helloService" class="cn.itcast.spring3.demo1.HelloServiceImpl">
             <!-- 使用<property>标签注入属性 
             value指的是普通值
             ref指的是对象
             -->
        <property name="info"  value="传智播客"></property>
        </bean>
        <!-- demo1快速入门 -->
        <!-- demo2Bean的实例化 -->
        <!-- 默认情况下使用的就是无参数的构造方法. -->
        
        <bean id="bean1" class="cn.itcast.spring3.demo2.Bean1"></bean>
        <!-- 第二种使用静态工厂实例化 不能写class了,因为现在不是由Spring直接帮你创建对象了-->
        <bean id="bean2" class="cn.itcast.spring3.demo2.Bean2Factory" factory-method="getBean2"></bean>
        <!-- 第三种使用实例工厂实例化 -->
        <bean id="bean3" factory-bean="bean3Factory" factory-method="getBean3"></bean>
        <!-- 要先把Bean3Factory实例化 -->
        <bean id="bean3Factory" class="cn.itcast.spring3.demo2.Bean3Factory"></bean>
        
        
    </beans>
    package cn.itcast.spring3.demo2;
    /**
     * 使用无参数的构造方法实例化
     * @author zhongzh
     *
     */
    public class Bean1 {
    /*public Bean1(String name){//这里写了一个带参构造,那么无参构造就不执行了
        //有一个有参无参就没有了
        
    }*/
        public Bean1(){
            System.out.println("使用无参数的构造方法实例化Bean1方法.....");
        }
    }
    package cn.itcast.spring3.demo2;
    /**
     * 使用静态工厂的方式实例化
     * @author zhongzh
     *
     */
    public class Bean2 {
       
    }
    package cn.itcast.spring3.demo2;
    /**
     * 使用实例工厂实例化
     * @author zhongzh
     *
     */
    public class Bean3{
        //加载配置文件的时候就会加载这个类了,会帮你创建了
        
        
    }
    package cn.itcast.spring3.demo2;
    /**
     * Bean2的静态工厂
     * @author zhongzh
     *
     */
    public class Bean2Factory {
       public static Bean2 getBean2(){
         System.out.println("静态工厂的获得Bean2的方法....");
        return new Bean2();//什么是静态工厂?就是这里提供一个静态的方法来返回Bean2的实例
           
       }
    }
    package cn.itcast.spring3.demo2;
    /**
     * 使用实例工厂
     * @author zhongzh
     *
     */
    //实例工厂与静态工厂的唯一区别是它里面的方法是不是静态的
    public class Bean3Factory {
            public Bean3 getBean3(){
                System.out.println("Bean3实例工厂的getBean3方法.....");
                return new  Bean3();
            }
    }
    package cn.itcast.spring3.demo2;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * Bean的实例化的方式
     * 
     * @author zhongzh
     *
     */
    public class SpringTest2 {
    @Test
    public void demo1(){
        //无参数的构造方法的实例化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//实例化工厂
        Bean1 bean1 = (Bean1) applicationContext.getBean("bean1");//通过配置文件里面配置的key获得bean1
     
          System.out.println(bean1);  
    }
    @Test
    public void demo2(){
        //静态工厂的实例化
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//实例化工厂
        Bean2 bean2 = (Bean2) applicationContext.getBean("bean2");//通过配置文件里面配置的key获得bean2
        
        System.out.println(bean2);  
    }
    @Test
    //实例工厂实例化
    public void demo3(){
        //静态工厂的实例化
    
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//实例化工厂
        Bean3 bean3 = (Bean3) applicationContext.getBean("bean3");//通过配置文件里面配置的key获得bean2
        
        System.out.println(bean3);  
    }
    }
  • 相关阅读:
    windows下面安装Python和pip终极教程
    windows下面安装Python和pip终极教程
    JSON格式的服务接口
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/6721137.html
Copyright © 2011-2022 走看看