IOC配置-集合注入

该文展示了如何在Spring框架的XML配置文件`applicationContext.xml`中定义和管理bean,包括使用`<bean>`标签创建bean,通过`<property>`或`<constructor-arg>`进行依赖注入,以及不同类型的集合注入示例。文中还包含了一个简单的BookDaoImpl类的实现和测试用例。
摘要由CSDN通过智能技术生成

applicationContext.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">
    <!--1.创建spring控制的资源-->
   <!-- <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        &lt;!&ndash;3.将要注入的引用类型的变量通过property属性进行注入,对应的name是要注入的变量名,使用ref属性声明要注入的bean的id&ndash;&gt;
        <property name="userDao" ref="userDao"/>
        <property name="num" value="666"/>
        <property name="version" value="itheima"/>
    </bean>-->
    <!--2.将要注入的资源声明为bean-->
    <!--<bean id="userDao" class="com.itheima.Dao.impl.UserDaoImpl">
        <constructor-arg index="2" value="123"/>
        <constructor-arg index="1" value="root"/>
        <constructor-arg index="0" value="com.mysql.jdbc.driver"/>
    </bean>
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        &lt;!&ndash;使用构造方法进行注入,需要保障注入的属性和bean中定义的属性一致
        一致指顺序一致或类型一致或使用index来解决此问题&ndash;&gt;
       <constructor-arg name="userDao" ref="userDao"/>
        <constructor-arg name="num" value="6666"/>
        <constructor-arg name="version" value="itcast"/>
    </bean>-->

    <bean id="userDao" class="com.itheima.Dao.impl.UserDaoImpl">
        <constructor-arg index="2" value="123"/>
        <constructor-arg index="1" value="root"/>
        <constructor-arg index="0" value="com.mysql.jdbc.driver"/>
    </bean>
    <bean id="userService" class="com.itheima.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
        <property name="bookDao" ref="bookDao"/>
    </bean>
    <bean id="bookDao" class="com.itheima.Dao.impl.BookDaoImpl">
        <property name="al">
            <list>
                <value>itheima</value>
                <value>6666</value>
            </list>
        </property>
        <property name="properties">
            <props>
                <prop key="name">itheima666</prop>
                <prop key="value">666666</prop>
            </props>
        </property>
        <property name="arr">
            <array>
                <value>123456</value>
                <value>6666</value>
            </array>
        </property>
        <property name="hs">
            <set>
                <value>itheima</value>
                <value>6666</value>
            </set>
        </property>
        <property name="hm">
            <map>
                <entry key="name" value="itheima6666"/>
                <entry key="value" value="66666666"/>
            </map>
        </property>

    </bean>
</beans>
package com.itheima.Dao;

public interface BookDao {
    public void save();
}
package com.itheima.Dao.impl;

import com.itheima.Dao.BookDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Properties;

public class BookDaoImpl implements BookDao {
    private ArrayList al;
    private Properties properties;
    private int[] arr;
    private HashSet hs;
    private HashMap hm;

    public void setAl(ArrayList al) {
        this.al = al;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }

    public void setArr(int[] arr) {
        this.arr = arr;
    }

    public void setHs(HashSet hs) {
        this.hs = hs;
    }

    public void setHm(HashMap hm) {
        this.hm = hm;
    }

    public void save() {
        System.out.println("bookDao running .....");
        System.out.println("ArrayList:" +al);
        System.out.println("Properties:"+properties);
        for (int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
        System.out.println("HashSet:"+hs);
        System.out.println("HashMap:"+hm);

    }
}
import com.itheima.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserApp {
    public static void main(String[] args) {
        //2.加载配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //3.获取资源
        UserService userService = (UserService) ctx.getBean("userService");
        userService.save();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值