Spring装配对象方法-构造方法

Spring装配对象方法

Spring通过XML使用构造方法为对象装配属性。

通过参数顺序

通过参数类型

通过参数顺序和类型

beans.xml

xml 复制代码
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="userinfo" class="com.hk.hr.UserInfo">
        <constructor-arg index="0" type="java.lang.String">
            <value>Java</value>
        </constructor-arg>
        <constructor-arg index="1" type="java.lang.String">
            <value>Oracle</value>
        </constructor-arg>
        <constructor-arg index="2" type="java.lang.Integer">
            <value>30</value>
        </constructor-arg>
        <property name="id" value="user"/>
        <property name="name" value="java"/>
    </bean>
    <bean name="orginfo" class="com.hk.hr.OrgInfo" p:name="oracle" p:mgr-ref="userinfo">
    </bean>
    <bean name="orginfo2" class="com.hk.hr.OrgInfo" p:name="oracle" p:mgr-ref="userinfo">
        <property name="users">
            <list>
                <value>Java</value>
                <value>Python</value>
                <ref bean="userinfo"/>
            </list>
        </property>
        <property name="roles">
            <list>
                <value>管理员</value>
                <value>浏览者</value>
                <ref bean="userinfo"/>
            </list>
        </property>
        <property name="maps">
            <map>
                <entry key="key01" value="Java"/>
                <entry key="key02" value="Android"/>
            </map>
        </property>
        <property name="prop">
            <props>
                <prop key="key01">Value1</prop>
                <prop key="key02">Value2</prop>
            </props>
        </property>
    </bean>
</beans>

UserInfo.java

java 复制代码
package com.hk.hr;

public class UserInfo {
    private String id = null;
    private String name = null;
    private Integer age = null;

    /**
     public UserInfo()
     {
     System.out.println("UserInfo()");
     }
     */

    public UserInfo(String id,String name)
    {
        this.id = id;
        this.name = name;
        System.out.println("UserInfo1:id="+id+",name="+name);
    }

    public UserInfo(String id,String name,Integer age)
    {
        this.id = id;
        this.name = name;
        this.age = age;
        System.out.println("UserInfo2:id="+id+",name="+name+",age="+age);
    }

    public UserInfo(String id,String name,String age)
    {
        this.id = id;
        this.name = name;
        this.age = Integer.parseInt(age);
        System.out.println("UserInfo3:id="+id+",name="+name+",age="+age);
    }

    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Main.java

java 复制代码
package com.hk.hr;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args)
    {
        ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
        UserInfo ui = (UserInfo)ac.getBean("userinfo");
        System.out.println("ui2="+ui);
        OrgInfo oi = (OrgInfo)ac.getBean("orginfo");
        System.out.println("oi="+oi);
        OrgInfo oi2 = (OrgInfo)ac.getBean("orginfo2");
        System.out.println("oi2="+oi2);
    }
}



为了避免Spring调用构造方法出现混淆,使用参数顺序和参数类型限定构造方法

相关推荐
Lhappy嘻嘻3 小时前
Java IO|File 文件操作 + 字节流 / 字符流完整笔记 + 递归删除文件实战
java·笔记·php
To_OC3 小时前
手写 AI 编程 Agent 的命令执行工具:我被 child_process 坑出来的实战经验
后端·node.js·agent
伊玛目的门徒3 小时前
试用leetcode之典中典 二数之和问题
java·算法·leetcode
逝水无殇6 小时前
C# 异常处理详解
开发语言·后端·c#
懒鸟一枚6 小时前
深入理解 Linux 内存、Swap 交换分区与分页机制的关系
java·linux·数据库
我命由我123458 小时前
执行 Gradle 指令报错,无法将“grep”项识别为 cmdlet、函数、脚本文件或可运行程序的名称
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
考虑考虑8 小时前
Sentinel安装
java·后端·微服务
IT_陈寒8 小时前
SpringBoot自动配置失灵?你可能忘了这个关键注解
前端·人工智能·后端
碎碎念_4928 小时前
SpringBoot + Vue 前后端分离从 0 到 1 完整环境配置流程
vue.js·spring boot·后端
逝水无殇8 小时前
C# 文件的输入与输出详解
开发语言·数据库·后端·c#