SpringBoot中三种常见的自动装配

SpringBoot中类的自重装配

  • @RequiredArgsConstructor
  • @Autowired
  • @resource

代码实例(准备service层和impl层)

TestService

package com.yc.health.service;

/**
 * @author cuiss
 * @Doscription
 * @date 2023/7/7
 */
public interface TestService {

    String test();
}

TestServiceImpl

package com.yc.health.service.impl;

import com.yc.health.service.TestService;
import org.springframework.stereotype.Service;

/**
 * @author cuiss
 * @Doscription
 * @date 2023/7/7
 */
@Service
public class TestServiceImpl implements TestService {

    @Override
    public String test() {
        System.out.println("test ok!!!");
        return "test ok!!!";
    }
}

controller层注入实例

方式一:@RequiredArgsConstructor

package com.yc.health.controller.pc;

import com.yc.health.service.TestService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author cuiss
 * @Doscription 测试SpringBoot的自动装配
 * @date 2023/7/7
 * 同时注意几点:
 * 1.这个是基于lombok的,使用时必须导入lombok包
 * 2.必须声明的变量为final
 * 3.根据构造器注入的,相当于当容器调用带有一组参数的类构造函数时,基于构造函数的 DI 就完成了,
 *   其中每个参数代表一个对其他类的依赖。基于构造方法为属性赋值,容器通过调用类的构造方法将其进行依赖注入
 * 4.使用当我们需要注入Bean的时候可以直接在类名称上使用。代替了Autowrited注解
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/test")
public class TestController {

    private final TestService testService;

    @RequestMapping("/t1")
    public String test01(){
        String result = testService.test();
        return "result ====> " + result;
    }
}

方式二:@Autowired

package com.yc.health.controller.pc;

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

/**
 * @author cuiss
 * @Doscription 测试SpringBoot的自动装配
 * @date 2023/7/7
 * Autowired注解是spring的注解,此注解只根据type进行注入,不会去匹配name.
 * 但是如果只根据type无法辨别注入对象时,就需要配合使用@Qualifier注解或者@Primary注解指定实现类来使用.
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    @Qualifier("testServiceImpl") // 一般情况下不需要指定
    private TestService testService;

    @RequestMapping("/t1")
    public String test01(){
        String result = testService.test();
        return "result ====> " + result;
    }
}

方式三:@resource


package com.yc.health.controller.pc;

import com.yc.health.service.TestService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author cuiss
 * @Doscription 测试SpringBoot的自动装配
 * @date 2023/7/7
 * Resource注解是java自身的注解,有两个重要的属性,分别是name和type,
 * 如果name属性有值,则使用byName的自动注入策略,将值作为需要注入bean的名字.
 * 如果type有值,则使用byType自动注入策略,将值作为需要注入bean的类型.
 * 默认:Resource注解 默认取字段名,按照名称查找,当找不到与名称匹配的bean时才按照类型进行装配。
 */
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource(name = "testServiceImpl")
    private TestService testService;

    @RequestMapping("/t1")
    public String test01(){
        String result = testService.test();
        return "result ====> " + result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值