Spring的bean的属性注入

bean的属性注入3中方式。
1.0 接口注入。
2.0 构造函数注入。
3.0 setter方法的注入。

spring支持后面的2种注入
示范如下,设计一个Person类型,和一个Student类型。

Person类,采用setter方法注入属性。


public class Person {
    
    private String name;
    private int age;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    @Override
    public String toString() {
        return "Person [name=" + name + ", age=" + age + "]";
    }
        
}

Student类, 采用构造函数注入属性值。


public class Student {
    
    private Person person;
    
    private String id;
    
    public Student(Person person,String id)
    {
        this.person=person;
        this.id=id;
        
        System.out.println("-construct-------"+id+"student被实列化"+person);
    }

    
    public void setPerson(Person person) {
        this.person = person;
    }


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

xml的配置如下

<bean id="person" class="Person">
<property name="name" value="dflx"></property>
<property name="age" value="999"></property>
</bean>

<bean id="student" class="Student">
<constructor-arg index="0" ref="person"></constructor-arg>
<constructor-arg index="1" type="java.lang.String" value="123456"></constructor-arg>
</bean>

在main类中,进行相关的测试

    ApplicationContext app=new ClassPathXmlApplicationContext("helloMessage.xml");
        Student student=(Student) app.getBean("student");
        System.out.println("////////////////");
        System.out.println(student.getId());

结果如下所示

-construct-------123456student被实列化Person [name=dflx, age=999]
////////////////
123456

spring集合类型的属性注入
list和集合的注入 设置一个测试类,来展示注入。

ublic class TestDem {
    
    
    private ArrayList<String> list;
    
    private String[] str;
    
    public void setList(ArrayList<String> list) {
        this.list = list;
    }
    
    public void setStr(String[] str) {
        this.str = str;
    }
    
    
    @Override
    public String toString() {
        return "TestDem [list=" + list + ", str=" + Arrays.toString(str) + "]";
    }

    @Test
    public void test()
    {
        ApplicationContext app=new ClassPathXmlApplicationContext("helloMessage.xml");
        TestDem test=(TestDem) app.getBean("test");
        
        System.out.println(test);
        
    }
    

}

相关的xml配置如下

<bean id="test" class="TestDem" >
<property name="list">
<list>
<value>alice</value>
<value>jack</value>
</list>
</property>

<property name="str">
<list>
<value>11</value>
<value>22</value>
<value>33</value>
</list>
</property>
</bean>

最后的结果如下

TestDem [list=[alice, jack], str=[11, 22, 33]]

Set的注入

xml的相关配置如下

<bean id="test" class="TestDem" >
<property name="set">
<set>
<value>111</value>
<value>222</value>
</set>
</property>

</bean>

结果如下

TestDem [set=[111, 222]]

集合属性map的注入

<bean id="test" class="TestDem" >
<property name="map">
<map>
<entry key="1" value="alice"></entry>
<entry key="2" value="marry"></entry>
</map>
</property>
</bean>

结果如下

TestDem [map={1=alice, 2=marry}]

Properties的属性注入

其xml的配置如下

<bean id="test" class="TestDem" >
<property name="proper">

<props>
<prop key="东风">冷雪</prop>
<prop key="风">雨</prop>
</props>

</property>
</bean>
</beans>

结果如下

TestDem [proper={东风=冷雪, 风=雨}]

和小伙伴,建了一个公众号,在摸索中,欢迎关注。搜索公众号:东风冷雪,英文:satan_master ,现在探索中。无所不有,包括生活,学习,娱乐。 三大板块。

公众号二维码.jpg
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 137,456评论 19赞 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,508评论 6赞 342
  • 我是怎么了?很多时候会这样问自己,之前的自己是一个知道自己想做什么,要做什么的,现在为什么有时感觉有些迷茫?记得刚...
    一根火柴的故事阅读 152评论 0赞 0
  • “留言的意义不在于深刻与否,而在于是否帮助你成长了起来。” 一种留言,我们很喜欢看。就是购物网站上的评价留言。我们...
    saiyonana阅读 347评论 0赞 0
  • ​已经好久好久没练书法了,字也写得比想象中难看,但还是执起笔,送一首《浪淘沙》,给在追寻和磨练路途中的自己,再说一...
    一半陈阅读 283评论 4赞 2

友情链接更多精彩内容