Spring Boot快速入门(三):依赖注入

Stella981
• 阅读 643

spring boot使用依赖注入的方式很简单,只需要给添加相应的注解即可

  • @Service用于标注业务层组件 
  • @Controller用于标注控制层组件
  • @Repository用于标注数据访问组件,即DAO组件 
  • @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

然后在使用的地方使用@Autowired即可

创建MyComponent,使用@Component

import org.springframework.stereotype.Component;

@Component//泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
public class MyComponent
{
    public void hi(String name)
    {
        System.out.println("hi " + name + ",I am MyComponent");
    }
}

创建MyController,使用@Controller

import org.springframework.stereotype.Controller;

@Controller//用于标注控制层组件
public class MyController
{
    public void hi(String name)
    {
        System.out.println("hi " + name + ",I am MyController");
    }
}

创建MyRepository,使用@Repository

@Repository//用于标注数据访问组件,即DAO组件
public class MyRepository
{
    public void hi(String name)
    {
        System.out.println("hi " + name + ",I am MyRepository");
    }
}

创建MyService,MyServiceImpl,使用@Service

public interface MyService
{
    void doSomeThing();
}
import org.springframework.stereotype.Service;

@Service//用于标注业务层组件
public class MyServiceImpl implements MyService
{

    @Override
    public void doSomeThing()
    {
        System.out.println("i am MyServiceImpl");
    }
} 

单元测试

在src/test/java/你的包名/你的项目名ApplicationTests编写对应的单元测试来验证是否可以成功注入

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class DiApplicationTests
{
    @Autowired//自动注入
    private MyController myController;
    @Autowired//自动注入
    private MyRepository myRepository;
    @Autowired//自动注入
    private MyComponent myComponent;
    @Autowired//自动注入实现了该接口的bean
    private MyService myService;

    @Test
    public void contextLoads()
    {
        myController.hi("lierabbit");
        myRepository.hi("lierabbit");
        myComponent.hi("lierabbit");
        myService.doSomeThing();
    }

}

运行测试用例

Spring Boot快速入门(三):依赖注入

hi lierabbit,I am MyController
hi lierabbit,I am MyRepository
hi lierabbit,I am MyComponent
i am MyServiceImpl

显示以上4句话证明成功注入

源码地址:https://github.com/LieRabbit/SpringBoot-DI

原文地址:https://lierabbit.cn/2018/01/15/SpringBoot%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A83-%E4%BE%9D%E8%B5%96%E6%B3%A8%E5%85%A5/

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
tomcat系列之五:Tomcat各个组件生命周期
一,Tomcat中各个组件的关系1,组件有大有小,大组件管理小组件。比如Server管理Service,Service管理连接器和容器2,组件有内有外,外层组件控制内层组件。比如外层组件连接器负责对外交流,外层组件调用内层组件完成业务功能二,创建组件的顺序先创建子组件,再创建父组件,然后把子组件注入到父组件中先创建内层组件,再创建外层
Easter79 Easter79
2年前
Springboot中如何在Utils类中使用@Autowired注入bean
Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类:1\.使用@Component注解标记工具类StatisticsUtils:2\.使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean:3\.在工具类中
Wesley13 Wesley13
2年前
@Repository、@Service、@Controller 和 @Component
@Repository、@Service、@Controller(https://my.oschina.net/u/1774615)和@Component将类标识为Beanspring自2.0版本开始,陆续引入了一些注解用于简化Spring的开发。@Repository注解便属于最先引入的一批,它用于将数据访问层(DAO层)的类
Wesley13 Wesley13
2年前
spring3.x注解自动注入
一、各种注解方式1.@Autowired注解(不推荐使用,建议使用@Resource)@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作。@Autowired的标注位置不同,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired能够工作,还需要在配置文件中加入以下Xml代码
Wesley13 Wesley13
2年前
1.1Spring Boot 环境配置和常用注解
SpringBoot常用注解:@Service:注解在类上,表示这是一个业务层bean@Controller:注解在类上,表示这是一个控制层bean@Repository:注解在类上,表示这是一个数据访问层bean@Component:注解在类上,表示通用bean,value不写默认就是类名首字母小写@Auto
Stella981 Stella981
2年前
Spring 12 种 常用注解!
1.声明bean的注解@Component组件,没有明确的角色@Service在业务逻辑层使用(service层)@Repository在数据访问层使用(dao层)@Controller在展现层使用,控制器的声明(C)2.注入bean的注解@Autowired:由Spring提供@Inj
Easter79 Easter79
2年前
Spring中@Controller、@Repository、@Service、@Component注解的作用详解
Spring中使用在类上的常用注解有@Controller、@Repository、@Service、@Component,下面分别详细介绍一下他们的作用:1、@Controller:用于标注控制层服务。2、@Repository:用于标注数据访问层,也可以说用于标注数据访问组件,即DAO组件。3、@Service:用于标注业务逻辑层服务,主要
Easter79 Easter79
2年前
Spring常用注解
组件注解相关@Component@Controller(https://my.oschina.net/u/1774615),@RestController@Service(https://my.oschina.net/service)@Repository(https://my.oschina.n
Stella981 Stella981
2年前
SpringBoot2.x版本整合Redis进行数据缓存
项目放在github:在缓存开发中,有两个重要的接口:在这里面:  @Cacheable:  如果用这个注解标注在方法上,那么方法的结果就会被缓存存起来,这个多用于在查询的时候进行使用    比如: publicusergetuser(Integerid) 这个方法用这个注解标注的话,通过id查到的内容就会杯存在缓存中进行保存
Easter79 Easter79
2年前
SpringBoot2.x版本整合Redis进行数据缓存
项目放在github:在缓存开发中,有两个重要的接口:在这里面:  @Cacheable:  如果用这个注解标注在方法上,那么方法的结果就会被缓存存起来,这个多用于在查询的时候进行使用    比如: publicusergetuser(Integerid) 这个方法用这个注解标注的话,通过id查到的内容就会杯存在缓存中进行保存