【spring】spring依赖注入的方式

文章详细介绍了Spring框架中三种依赖注入的方式:构造器注入、setter方法注入和field属性注入,包括各自的优缺点和使用场景。通过代码示例展示了如何在实际开发中应用这些注入方式,帮助读者理解和掌握依赖注入的概念。
摘要由CSDN通过智能技术生成

一、依赖注入的方式
  • 1.通过构造器注入
  • 2.通过Setter方法注入
  • 3.通过Field属性注入
二、通过构造器注入
  • 1.官方推荐的方式
  • 2.注入对象很多的情况下,构造器参数列表会很长,不灵活
  • 3.对象初始化完成后,即可获得可使用的对象
  • 4.检测到循环依赖
三、通过Setter方法注入
  • 1.日常开发中不常见
  • 2.可以确保注入前不依赖spring容器
  • 3.每个set方法单独注入一个对象,可以灵活控制,可以实现选择性注入
  • 4.检测到循环依赖
四、通过Field属性注入
  • 1.如@Autowired、@Resource注解
  • 2.控制了对象的外部可见性,不被spring容器托管的对象无法自动注入
  • 3.不能被检测出是否出现循环依赖
  • 4.被final修饰的属性,无法赋值
五、代码示例
5.1 构造函数注入
package com.learning.controller;

import com.learning.service.TestService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wangyouhui
 * @Description 测试controller
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    private TestService testService;

    public TestController(TestService testService){
        this.testService = testService;
    }

    @GetMapping("/get")
    public String test(){
        return testService.test();
    }
}

package com.learning.service.impl;

import com.learning.service.TestService;
import org.springframework.stereotype.Service;

/**
 * @Author wangyouhui
 * @Description 测试实现类
 **/
@Service
public class TestServiceImpl implements TestService {
    @Override
    public String test() {
        return "test";
    }
}

5.2 setter方法注入
package com.learning.controller;

import com.learning.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wangyouhui
 * @Description setter方法注入
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    private TestService testService;

    @Autowired
    public void setTestService(TestService testService){
        this.testService = testService;
    }

    @GetMapping("/get")
    public String test(){
        return testService.test();
    }
}

5.3 field属性注入
ackage com.learning.controller;

import com.learning.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author wangyouhui
 * @Description setter方法注入
 **/
@RestController
@RequestMapping("/test")
public class TestController {
	@Autowired
    private TestService testService;

    @GetMapping("/get")
    public String test(){
        return testService.test();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值