Spring之IOC创建Bean方式

一、构造器的三种方式 

    //通过下标赋值
    <bean id="user" class="com.lizheng.pojo.User">
        <constructor-arg index="0" value="lizheng"/>
    </bean>
    //通过数据类型赋值,不建议使用,若两个参数均为string
    <bean id="user2" class="com.lizheng.pojo.User">
        <constructor-arg type="java.lang.String" value="rourou"/>
    </bean>
    //通过参数名来赋值
    <bean id="user3" class="com.lizheng.pojo.User">
        <constructor-arg name="name" value="junjun"/>
    </bean>

二、通过set方法创建bean 

    <bean id="student" class="com.lizheng.pojo.Student">
        <property name="name" value="lizheng"/>
        <property name="address" ref="address"/>
        <property name="card">
            <map>
                <entry key="id" value="200101"/>
            </map>
        </property>
        <property name="games">
            <set>
                <value>lol</value>
            </set>
        </property>
        <property name="wife">
            <null/>
        </property>
        <property name="hobbies">
            <list>
                <value>basketball,football</value>
            </list>
        </property>
        <property name="info">
            <props>
                <prop key="phone">1758900987</prop>
                <prop key="gender">man</prop>
            </props>
        </property>
    </bean>

    <bean id="address" class="com.lizheng.pojo.Address">
        <property name="address" value="中国"/>
    </bean>

实体类:

public class Student {
    private String name;
    private Address address;
    private List<String> hobbies;
    private Map<String,String> card;
    private Set<String> games;
    private Properties info;
    private String wife;

    public String getWife() {
        return wife;
    }

    public void setWife(String wife) {
        this.wife = wife;
    }

    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 List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public Map<String, String> getCard() {
        return card;
    }

    public void setCard(Map<String, String> card) {
        this.card = card;
    }

    public Set<String> getGames() {
        return games;
    }

    public void setGames(Set<String> games) {
        this.games = games;
    }

    public Properties getInfo() {
        return info;
    }

    public void setInfo(Properties info) {
        this.info = info;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", address=" + address +
                ", hobbies=" + hobbies +
                ", card=" + card +
                ", games=" + games +
                ", info=" + info +
                ", wife='" + wife + '\'' +
                '}';
    }
}

测试类:

    @Test
    public void myTset(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.getName());
        System.out.println(student.getAddress());
        System.out.println(student.getCard());
        System.out.println(student.getInfo());
        System.out.println(student.getHobbies());
        System.out.println(student.getGames());
    }

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值