四、Spring的依赖注入详解

spring依赖注入分为三种方式

构造方法注入

首先我们在类中加上下面的代码,我们加上三个变量

package com.lp.service.impl;

import com.lp.dao.UserDao;
import com.lp.dao.impl.UserDaoImpl;
import com.lp.service.UserService;

import java.util.Date;

/**
 * @Date 2020/5/26 21:21
 * @Author luopeng
 */
public class UserServiceImpl implements UserService {

    private String name;
    private Integer age;
    private Date time;

    public UserServiceImpl(String name ,Integer age ,Date time){
        this.name = name;
        this.age = age;
        this.time = time;
    }

    public void saveUser() {
        System.out.println("保存成功!"+name+","+age+","+time);

    }
}

构造方法注入需要在配置文件中加入bean配置
其中的constructor-arg 代表的是构造函数的参数,一般我们常用name来指定参数名,当然,我们要得到参数也有其他方法,比如:

  • type 根据类型来赋值,但是有相同的类型就不行了
  • index 根据下标来赋值,但是参数多的话,你不一定会记得每个下标的值是什么
  • name 用name的话spring会有参数提示,这样更适合我们的习惯

value来指定该参数的值,当然,当你的值是对象时,你就需要重新在spring容器里面拥有这个对象再去用它,譬如我们的Date类,你就需要先创建bean对象再使用!

<?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.lp.service.impl.UserServiceImpl">
        <constructor-arg name="name" value="张三"/>
        <constructor-arg name="age" value="21"/>
        <constructor-arg name="time" ref="time"/>
    </bean>

    <bean id="time" class="java.util.Date"/>

</beans>
setter注入

setter注入就是set方法注入,首先我们需要的是在类中加入set方法

package com.lp.service.impl;

import com.lp.dao.UserDao;
import com.lp.dao.impl.UserDaoImpl;
import com.lp.service.UserService;

import java.util.Date;

/**
 * @Date 2020/5/26 21:21
 * @Author luopeng
 */
public class UserServiceImpl implements UserService {

    private String name;
    private Integer age;
    private Date time;

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

    public void setAge(Integer age) {
        this.age = age;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public void saveUser() {
        System.out.println("保存成功!"+name+","+age+","+time);

    }
}

然后在配置文件中的配置方法其实大同小异

<?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.lp.service.impl.UserServiceImpl">
        <property name="userName" value="张三"/>
        <property name="age" value="21"/>
        <property name="time" ref="time"/>
    </bean>

    <bean id="time" class="java.util.Date"/>

</beans>

仔细看上面我写的setUserName,我写这个是想提醒这里的property标签里面的name是set方法的set后面的方法名

我们再来讲一讲复杂类型的setter注入

类中的代码

package com.lp.service.impl;

import com.lp.dao.UserDao;
import com.lp.dao.impl.UserDaoImpl;
import com.lp.service.UserService;

import java.util.*;

/**
 * @Date 2020/5/26 21:21
 * @Author luopeng
 */
public class UserServiceImpl implements UserService {

    private String[] strs;
    private List<String> list;
    private Set<String> set;

    private Map<String,String> map;
    private Properties properties;

   //省略set方法
    public void saveUser() {

        System.out.println(Arrays.toString(strs));
        System.out.println(list);
        System.out.println(set);
        System.out.println(map);
        System.out.println(properties);

    }
}

配置文件

<?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.lp.service.impl.UserServiceImpl">
        <property name="strs">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </set>
        </property>

        <property name="list">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </set>
        </property>

        <property name="set">
            <set>
                <value>aaa</value>
                <value>bbb</value>
                <value>ccc</value>
            </set>
        </property>

        <property name="map">
            <map>
                <entry key="aaa" value="111"/>
            </map>
        </property>

        <property name="properties">
            <map>
                <entry key="bbb" value="222"/>
            </map>
        </property>
    </bean>
    

</beans>

仔细看,对于字符数组,list,set,我都选择的是用set标签,其实他们分别对应array,list,set标签,但是可以直接使用任意一个标签即可,对于map和peoperties也是同理!这样我们便可以只需要记忆两个标签就实现了全部的功能!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值