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

相关推荐
Aision_1 小时前
从工具调用到 MCP、Skill完整学习记录
java·python·gpt·学习·langchain·prompt·agi
zc.z5 小时前
JAVA实现:纯PCM格式音频转换成BASE64
java·音视频·pcm
mask哥6 小时前
力扣算法java实现汇总整理(上)
java·算法·leetcode
无风听海6 小时前
深入剖析 YARP 的 Transforms:构建灵活的反向代理转换管道
后端·中间件·asp.net
Gopher_HBo6 小时前
负载均衡
后端
自由生长20246 小时前
RAG已死?什么标题党啊!
后端
Aaswk7 小时前
Java Lambda 表达式与流处理
java·开发语言·python
是宇写的啊7 小时前
Spring AOP
java·spring
万邦科技Lafite7 小时前
京东item_get接口实战案例:实时商品价格监控全流程解析
java·开发语言·数据库·python·开放api·淘宝开放平台
东方小月7 小时前
5分钟搞懂Harness Engineering(驾驭工程):从提示词到AI Agent的进化之路
前端·后端·架构