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;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot自动装配(Auto Configuration)是一种特性,它通过约定大于配置的方式,自动配置Spring应用程序的各种组件和功能。 自动装配的核心思想是根据应用程序的依赖关系和配置,自动推断出需要配置的组件,并将其自动注入到Spring容器。这样,开发者可以专注于业务逻辑的开发,而无需手动配置和管理各种组件。 Spring Boot自动装配主要依赖于以下几个技术: 1. 条件注解(Conditional Annotations):通过使用条件注解,可以根据特定条件决定是否要加载某个组件或配置。例如,可以使用@ConditionalOnClass注解来指定某个类存在时才加载配置。 2. 自动配置类(Auto Configuration Classes):Spring Boot提供了一些自动配置类,这些类在应用启动时会被自动加载并配置相应的组件。这些自动配置类使用了条件注解来控制加载条件,并通过@Configuration注解将其标记为配置类。 3. 自动配置属性(Auto Configuration Properties):Spring Boot提供了一些属性来控制自动配置的行为。这些属性可以通过配置文件、命令行参数或环境变量进行配置,用于覆盖默认的自动配置行为。 通过以上技术,Spring Boot可以根据应用程序的依赖关系和配置,自动装配各种常见的组件和功能,例如数据库连接池、Web服务器、消息队列等。开发者只需引入相应的依赖,配置必要的属性,即可使用这些功能,无需手动编写大量的配置代码。 需要注意的是,自动装配是一种方便的特性,但也可能导致配置冲突或不符合预期的行为。在实际开发,可以通过调整加载顺序、排除不需要的自动配置类或手动配置来解决这些问题。 总结来说,Spring Boot自动装配是一种通过约定大于配置的方式,根据应用程序的依赖关系和配置,自动配置Spring应用程序的各种组件和功能。它提供了条件注解、自动配置类和自动配置属性等技术来实现自动装配

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值