Spring IOC学习(2) +xml文件配置相关

xml配置默认使用类的无参构造

当类存在有参构造函数时

package Test;

public class Hello {
    private String name;
    public Hello(){
        System.out.println("Hello对象被创建");
    }
    public Hello(String name){
        this.name=name;
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    public void show(){
        System.out.println("name="+name);
    }
}

xml配置文件:

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="hello" class="Test.Hello">
        <!--index为参数的下标 从0开始-->
        <constructor-arg index="0" value="jie"/>
    </bean>
</beans>

官方文档:

比较推荐做法:

别名标签


相当于hello和hello2都可以指向同一个对象
但是可以这样: 并且可以取好多好多个

所以基本没吊用

import标签


导入其他标签资源

posted @ 2021-03-26 10:15  一个经常掉线的人  阅读(41)  评论(0)    收藏  举报