spring学习之注入对象

注入依赖对象

  • 注入基本数据类型
    通过value属性注入

在beans.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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl">
        <property name="name" value="nihao"></property>
    </bean>
</beans>

在接口bean对象中

package com.gc.service.impl;

import com.gc.service.PersonService;

public class PersonServiceImpl implements PersonService {
    public String name;

    public String getName() {
        return name;
    }

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


    public void save() {
        // TODO Auto-generated method stub
        System.out.println("name:"+name);
        System.out.println("save方法");
    }
}

结果:
这里写图片描述

  • ref属性注入bean对象

    这里写图片描述

  • 注入集合类型
    1.set集合
<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl">
        <property name="sets">
            <set>
                <value>first</value>
                <value>second</value>
                <value>third</value>
            </set>
        </property>
    </bean>
</beans>

2.list集合

<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl">
        <property name="lists">
            <list>
                <value>first</value>
                <value>second</value>
                <value>third</value>
            </list>
        </property>
    </bean>
</beans>

3.property

<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl">
        <property name="propertys">
            <props>
                <prop key="key1">first1</prop>
                <prop key="key2">first2</prop>
                <prop key="key3">first3</prop>
            </props>
        </property>
    </bean>
</beans>

4.map类型

<?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 http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <bean id="PersonService" class="com.gc.service.impl.PersonServiceImpl">
        <property name="maps">
            <map>
            <entry key="key1" value="value1" />
            <entry key="key2" value="value2" />
            <entry key="key3" value="value3" />
            </map>
        </property>
    </bean>
</beans>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值