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);
        }
    }
    

      

    通过工厂模式

  • 相关阅读:
    专职DBA-MySQL体系结构与基本管理
    JSON
    MIME类型
    文件上传下载
    response常用的方法
    2020.11.27小记
    HTTP请求状态码
    1561. Maximum Number of Coins You Can Get
    1558. Minimum Numbers of Function Calls to Make Target Array
    1557. Minimum Number of Vertices to Reach All Nodes
  • 原文地址:https://www.cnblogs.com/vincentmax/p/14293369.html
Copyright © 2011-2022 走看看