Spring入门教程笔记 —— 第四章集合和properties的依赖注入

7 篇文章 0 订阅

Spring入门教程笔记 —— 第四章集合和properties依赖注入

前言

我们前面学习了 构造方法成员变量 的依赖注入,今天我们来学一下对 ListMapproperties 依赖注入

准备测试环境

UserDaoImpl.java

package cn.marinda.dao.Impl;

import cn.marinda.dao.UserDao;
import cn.marinda.entity.User;

import java.util.*;

public class UserDaoImpl implements UserDao {
    private Map<String, User> userDaoMap = new HashMap<>();
    private List<String> strList = new ArrayList<>();
    private List<User> usersList = new ArrayList<>();
    private Properties properties = new Properties();

    public Map<String, User> getUserDaoMap() {
        return userDaoMap;
    }

    public void setUserDaoMap(Map<String, User> userDaoMap) {
        this.userDaoMap = userDaoMap;
    }

    public List<String> getStrList() {
        return strList;
    }

    public void setStrList(ArrayList<String> strList) {
        this.strList = strList;
    }

    public Properties getProperties() {
        return properties;
    }

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

    @Override
    public String toString() {
        return "UserDaoImpl{" +
                "userDaoMap=" + userDaoMap +
                ", strList=" + strList +
                ", usersList=" + usersList +
                ", properties=" + properties +
                '}';
    }


    public List<User> getUsersList() {
        return usersList;
    }

    public void setUsersList(List<User> usersList) {
        this.usersList = usersList;
    }

    @Override
    public void play() {
        System.out.println("Play");
    }
}

UserDao.java


package cn.marinda.dao;

public interface UserDao {

   void play();
}

User.java

package cn.marinda.entity;

public class User {

    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 "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

applicationDemo.java

package cn.marinda.application;

import cn.marinda.dao.Impl.UserDaoImpl;
import cn.marinda.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ApplicationDemo {
    public static void main(String[] args) {
        ApplicationContext  context = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDaoImpl) context.getBean("userDao");
        System.out.println("UserDao: " + userDao.toString());
    }
}

applicationContext.xml

新建一个在resource目录下的一个Spring Xml文件,并开始依赖注入

依赖注入

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.xsd">

<!--    userDaoImpl 的bean-->
    <bean name="userDao" class="cn.marinda.dao.Impl.UserDaoImpl">
        <property name="strList" >
            <list>
                <value>test1</value>
                <value>test2</value>
            </list>
        </property>
        <!--    HashMap -->
        <property name="userDaoMap">
            <map>
                <entry key="user1" value-ref="user1"></entry>
                <entry key="user2" value-ref="user2"></entry>
            </map>
        </property>

        <!--    properties-->

        <property name="properties">
            <props>
                <prop key="test1">properties1</prop>
                <prop key="test2">properties2</prop>
            </props>
        </property>

        <property name="usersList">
            <list>
                <ref bean="user1"></ref>
                <ref bean="user2"></ref>
            </list>
        </property>
    </bean>

<!--    User Bean-->
    <bean name="user1" class="cn.marinda.entity.User">
        <property name="name" value="小明"></property>
        <property name="age" value="15"></property>
    </bean>
<!--    User Bean2-->
    <bean name="user2" class="cn.marinda.entity.User">
        <property name="name" value="小君"></property>
        <property name="age" value="18"></property>
    </bean>


</beans>

效果

控制台结果:

UserDao: UserDaoImpl{userDaoMap={user1=User{name=‘小明’, age=15}, user2=User{name=‘小君’, age=18}}, strList=[test1, test2], usersList=[User{name=‘小明’, age=15}, User{name=‘小君’, age=18}], properties={test2=properties2, test1=properties1}}

结束语

今天我们讲解了一下Spring中集合和properties的依赖注入

  • 如果对你有帮助的话可以给我点赞收藏,十分感谢
  • 致力做学习笔记分享给大家
  • 可以转载 需标明 出处 本文链接。
  • 笔者一个开源项目:餐饮管理系统 希望大家可以点一下star

感谢你的观看。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值