Spring-03-Ioc-set-5种数据类型注入

1.基本步骤  

导入jar包    log4j 资源

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.1</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.16</version>
        </dependency>
    </dependencies>

2.创建dao层   service层  进行资源环境搭建

创建实体类User    entity

package com.liuboss.entity;

import lombok.Data;

@Data
public class User {
    private int id;
    private String username;
    private String password;

}

 

dao层

package com.liuboss.Dao;

public interface UserDao {
    public void save();
}

dao接口实现层

package com.liuboss.Dao.Impl;

import com.liuboss.Dao.UserDao;
import com.liuboss.entity.User;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.Properties;
@Data
public class UserDaoImpl implements UserDao {
    //实现5种资源的注入
    private int id;
    private String username;
    private String password;
    private List<String> list;
    private Map<String,User> map;
    private Properties properties;



    public void save() {
        System.out.println("Dao....资源保存中....");
    }
}

service层  

接口层

package com.liuboss.Service;

public interface UserService {
    public void save();
}

service接口实现层

package com.liuboss.Service.Impl;

import com.liuboss.Dao.UserDao;
import com.liuboss.Service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void save() {
        System.out.println("UserService...资源保存中...");
        userDao.save();

    }
}

resource文件下的资源配置文件

<?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">

    <bean id="userService" class="com.liuboss.Service.Impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
    <bean id="userDao" class="com.liuboss.Dao.Impl.UserDaoImpl">
        <property name="id" value="1"/>
        <property name="username" value="永乐大帝"/>
        <property name="password" value="123456"/>
        <property name="list">
            <list>
                <value>李渊</value>
                <value>武则天</value>
                <value>李世民</value>
            </list>
        </property>
        <property name="map">
            <map>
                <entry key="u1" value-ref="user1"></entry>
                <entry key="u1" value-ref="user2"></entry>
                <entry key="u1" value-ref="user3"></entry>
            </map>
        </property>
        <property name="properties">
            <props>
                <prop key="1">洪武大帝</prop>
                <prop key="2">永乐大帝</prop>
            </props>
        </property>

    </bean>
    <bean id="user1" class="com.liuboss.entity.User">
        <property name="id" value="00001"/>
        <property name="username" value="朱元璋"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="user2" class="com.liuboss.entity.User">
        <property name="id" value="00002"/>
        <property name="username" value="朱棣"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="user3" class="com.liuboss.entity.User">
        <property name="id" value="00003"/>
        <property name="username" value="朱高志"/>
        <property name="password" value="123456"/>
    </bean>
</beans>

重点---

注意  map集合注入方式    其中对应的实体类   为User实体类   

<property name="map">
            <map>
                <entry key="u1" value-ref="user1"></entry>
                <entry key="u1" value-ref="user2"></entry>
                <entry key="u1" value-ref="user3"></entry>
            </map>
</property>
<bean id="user1" class="com.liuboss.entity.User">
        <property name="id" value="00001"/>
        <property name="username" value="朱元璋"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="user2" class="com.liuboss.entity.User">
        <property name="id" value="00002"/>
        <property name="username" value="朱棣"/>
        <property name="password" value="123456"/>
    </bean>
    <bean id="user3" class="com.liuboss.entity.User">
        <property name="id" value="00003"/>
        <property name="username" value="朱高志"/>
        <property name="password" value="123456"/>
</bean>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值