Spring实例参考06-setter注入的10种方式

目录

前言

1 java类

2 配置文件

 3 主测试类


前言

本文供以下文章参考使用:

Spring笔记__evenif的博客-CSDN博客

1 java类

package com.evenif.bean;

import java.util.*;

public class User {
    private int id;
    private String name;
    private Address address;
    private String[] phones;
    private List<String> titleList;
    private Map<String,Integer> tokens;
    private Set<String> alias;
    private Object achievement;
    private Properties others;
    public User() {
    }

    public User(int id, String name) {
        this.id = id;
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String[] getPhones() {
        return phones;
    }

    public void setPhones(String[] phones) {
        this.phones = phones;
    }

    public List<String> getTitleList() {
        return titleList;
    }

    public void setTitleList(List<String> titleList) {
        this.titleList = titleList;
    }

    public Set<String> getAlias() {
        return alias;
    }

    public void setAlias(Set<String> alias) {
        this.alias = alias;
    }

    public Object getAchievement() {
        return achievement;
    }

    public void setAchievement(Object achievement) {
        this.achievement = achievement;
    }

    public Properties getOthers() {
        return others;
    }

    public void setOthers(Properties others) {
        this.others = others;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", address=" + address +
                ", phones=" + Arrays.toString(phones) +
                ", titleList=" + titleList +
                ", tokens=" + tokens +
                ", alias=" + alias +
                ", achievement=" + achievement +
                ", others=" + others +
                '}';
    }

    public Map<String, Integer> getTokens() {
        return tokens;
    }

    public void setTokens(Map<String, Integer> tokens) {
        this.tokens = tokens;
    }

}
package com.evenif.bean;

public class Address {
    private String province;
    private String city;
    private String address;

    public String getProvince() {
        return province;
    }

    public void setProvince(String province) {
        this.province = province;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "Address{" +
                "province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}

2 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!--
 p命名空间使用需要导入:
 xmlns:p="http://www.springframework.org/schema/p"
 c命名空间使用需要导入:
 xmlns:c="http://www.springframework.org/schema/c"
-->
<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"
       xmlns:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    https://www.springframework.org/schema/beans/spring-beans.xsd">
    <bean id="addr" class="com.evenif.bean.Address">
        <property name="province" value="xx省"/>
        <property name="city" value="yy市"/>
        <property name="address" value="zz街n号"/>
    </bean>

    <bean id="user" class="com.evenif.bean.User">
        <!-- 1. 常量注入 -->
        <property name="name" value="constName"/>
        <!-- 2. bean注入 -->
        <property name="address" ref="addr"/>
        <!-- 3. 数组注入 -->
        <property name="phones">
            <array>
                <value>13812345678</value>
                <value>13987654321</value>
            </array>
        </property>
        <!-- 4. List注入 -->
        <property name="titleList">
            <list>
                <value>政治家</value>
                <value>军事家</value>
            </list>
        </property>
        <!-- 5. Map注入 -->
        <property name="tokens">
            <map>
                <!-- 方式一(个人觉得该方式较好) -->
                <entry key="A币" value="100"/>
                <!-- 方式二 -->
                <entry>
                    <key><value>C币</value></key>
                    <value>200</value>
                </entry>
            </map>
        </property>
        <!-- 6. Set注入,重复元素自动去重 -->
        <property name="alias">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>bbb</value>
            </set>
        </property>
        <!-- 7. null注入 -->
        <property name="achievement"><null/></property>
        <!-- 8. Properties注入 -->
        <property name="others">
            <props>
                <prop key="sex">男</prop>
                <prop key="age">18</prop>
                <prop key="job">自由职业</prop>
            </props>
        </property>
    </bean>
    <!-- 9. p注入 -->
    <bean id="p_user" class="com.evenif.bean.User" p:name="pName" p:id="2"/>
    <!-- 10. c注入 -->
    <bean id="c_user" class="com.evenif.bean.User" c:name="cName" c:id="3"/>
</beans>

3 主测试类

package com.evenif;
import com.evenif.bean.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        User user = (User) context.getBean("user");
        System.out.println(user);
        user = (User) context.getBean("p_user");
        System.out.println(user);
        user = (User) context.getBean("c_user");
        System.out.println(user);
    }
}
//执行结果:
//User{id=0, name='constName',
//      address=Address{province='xx省', city='yy市', address='zz街n号'},
//      phones=[13812345678, 13987654321],
//      titleList=[政治家, 军事家],
//      tokens={A币=100, C币=200},
//      alias=[aaa, bbb],
//      achievement=null,
//      others={sex=男, job=自由职业, age=18}}

//User{id=2, name='pName', address=null, phones=null, titleList=null, tokens=null, alias=null, achievement=null, others=null}
//User{id=3, name='cName', address=null, phones=null, titleList=null, tokens=null, alias=null, achievement=null, others=null}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_evenif

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值