zoukankan      html  css  js  c++  java
  • SpringIOC创建对象的三种方式

    [2] 通过构造器方式

    ①无参数构造器(创建一个没有初始化数据的对象)

    ②有参 数构造器(创建一个带有初始化数据的对象)

    <?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">
        <!--创建student的bean对象-->
        <!--构造器方式-->
        <!--
        无参构造器 
        特点:Spring容器默认使用无参构造方式创建对象
        使用:在配置文件中直接使用bean标签配置即可,无需过多声明
        -->
        <bean id="stu" class="com.bjsxt.pojo.Student"></bean>
        
        <!--有参数的构造器
        特点:Spring容器对根据配置调用的有参构造器创建一个带有初始化数据的对象
        使用:constructor-arg:使用bean的字标签来声明调用的构造器的形参的个数
        一个字标签表示一个参数
        属性:index:参数的下标
              type:参数的类型,全限定路径
              name:参数的形参名
              value:参数要给的值
        -->
        <bean id="stu2" class="com.bjsxt.pojo.Student">
            <constructor-arg index="0" type="java.lang.Integer" name="sid" value="1"></constructor-arg>
            <constructor-arg index="1" type="java.lang.String" name="sname" value="张三"></constructor-arg>
        </bean>
              
    </beans>
    

      

    package com.bjsxt.controller;
    
    import com.bjsxt.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author wuyw2020
     * @date 2020/1/8 15:16
     */
    public class testStu {
        
        public static void main(String[] args) {
            //创建容器对象
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
            //获取容器中的对象
            //无参构造器方式
            Student student = (Student) ac.getBean("stu");
            System.out.println("无参构造:"+student);
            
            //有参构造器
            Student student1= (Student) ac.getBean("stu2");
            System.out.println("有参构造:"+student1);
            
        }
    }

    [2] 通过属性注入(get/set)

    先通过空构造器创建一个对象,然后再使用set方法进行初始化赋值.

    <?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">
        <!--创建student的bean对象-->
        <!--
        属性注入方式
        特点:相当于创建一个空对象然后使用set方法赋值
        使用:
        property:在bean标签下使用子标签property,表示调用set方法给某个属性赋值
        属性:name:要赋值的属性名
              value:值
        -->
        <bean id="stu3" class="com.bjsxt.pojo.Student">
            <property name="sid" value="2"></property>
            <property name="sname" value="李四"></property>
        </bean>
    </beans>
    

      

    package com.bjsxt.controller;
    
    import com.bjsxt.pojo.Student;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    /**
     * @author wuyw2020
     * @date 2020/1/8 15:16
     */
    public class testStu {
        
        public static void main(String[] args) {
            //创建容器对象
            ApplicationContext ac = new ClassPathXmlApplicationContext("applicationcontext.xml");
            //获取容器中的对象
            //属性注入方式
            Student student = (Student) ac.getBean("stu3");
            System.out.println("属性注入方式"+student);
        }
    }
    

      

    通过工厂模式

  • 相关阅读:
    雅虎与谷歌搜索试合作 增加与微软谈判砝码
    重庆竞标(一)
    大数据量分页存储过程效率测试附代码 (转载)
    【转】MVP 模式实例解析
    在本地电脑和局域网电脑的SQLServer2000之间自动备份数据库(收藏)
    Adobe宣布Flex Builder将更名为Flash Builder
    语录:101条伟大的计算机编程名言<转载CSDN>
    博客园功能使用说明<收藏>
    sql 利用索引优化性能(转载)
    泛型定制泛型接口、泛型类(转载)
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14293369.html
Copyright © 2011-2022 走看看