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调用构造方法出现混淆,使用参数顺序和参数类型限定构造方法

相关推荐
葫芦和十三7 小时前
图解 MongoDB 07|索引类型:七种索引,七种访问形状
后端·mongodb·agent
朦胧之9 小时前
AI 编程-老项目改造篇
java·前端·后端
爱勇宝12 小时前
我做了一个只用来搜歌词的小 App
android·前端·后端
IT_陈寒12 小时前
SpringBoot自动配置坑了我一晚上,原来问题出在这
前端·人工智能·后端
SelectDB13 小时前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
SelectDB13 小时前
秒级弹性、最高降本 70%:SelectDB Serverless 如何重塑云数仓资源效率
大数据·后端·云原生
程序猿大帅13 小时前
别再只当调包侠了:用 Spring AI 落地 Function Calling,我被大模型硬生生砸出了三个大坑
java
PinkSun13 小时前
Spring AI ChatMemory踩坑实录:重启丢数据、Agent丢记忆、对话溢出
后端·ai编程
壹方秘境13 小时前
我用Go语言开发了一个跨平台的HTTPS抓包和调试工具
前端·后端·ios
神秘面具男13 小时前
HarmonyOS 6.0跨端远程控制
前端·后端