Spring Boot快速入门(三):依赖注入
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();
} }
运行测试用例

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
Spring Boot快速入门(三):依赖注入的更多相关文章
- Spring Boot 快速入门
Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...
- Spring Boot快速入门(二):http请求
原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...
- Spring Boot 快速入门 史上最简单
1.Spring Boot 概述 Spring Boot 是所有基于 Spring 开发的项目的起点.Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的 ...
- spring boot入门教程——Spring Boot快速入门指南
Spring Boot已成为当今最流行的微服务开发框架,本文是如何使用Spring Boot快速开始Web微服务开发的指南,我们将使创建一个可运行的包含内嵌Web容器(默认使用的是Tomcat)的可运 ...
- Spring Boot 快速入门(IDEA)
从字面理解,Boot是引导的意思,因此SpringBoot帮助开发者快速搭建Spring框架:SpringBoot帮助开发者快速启动一个Web容器:SpringBoot继承了原有Spring框架的优秀 ...
- 笔记61 Spring Boot快速入门(一)
IDEA+Spring Boot快速搭建 一.IDEA创建项目 略 项目创建成功后在resources包下,属性文件application.properties中,把数据库连接属性加上,同时可以设置服 ...
- 笔记63 Spring Boot快速入门(三)
SpringBoot中使用JSP Springboot的默认视图支持是Thymeleaf,但是Thymeleaf还没开始学,熟悉的还是jsp,所以要让Springboot支持 jsp. 一.在pom. ...
- spring boot快速入门 1 :创建项目、 三种启动项目方式
准备工作: (转载)IDEA新建项目时,没有Spring Initializr选项 最近开始使用IDEA作为开发工具,然后也是打算开始学习使用spring boot. 看着博客来进行操作上手sprin ...
- Spring Boot 快速入门笔记
Spirng boot笔记 简介 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
随机推荐
- python3.5新增函数isclose的使用
前言:python3.5的math模块新增一个isclose函数用来判断两个浮点数的值是否接近或相等,这是由于浮点数的计算总是存在一定的误差.下面谈一下用法: import math print(ma ...
- 根据实践经验,讲述些学习Java web能少走的弯路,内容摘自java web轻量级开发面试教程
在和不少比较上进的初级程序员打交道的过程中,我们总结出了一些能帮到合格程序员尽快进阶的经验,从总体上来讲,多学.多实践不吃亏.本文来是从 java web轻量级开发面试教程从摘录的. 1 哪些知识点 ...
- Lua的数学函数
ua5.1中数学库的所有函数如下表: math.pi 为圆周率常量 = 3.14159265358979323846 函数名 函数功能 示例 示例结果 abs 取绝对值 math.abs(-15) ...
- Elasticsearch批处理操作——bulk API
Elasticsearch提供的批量处理功能,是通过使用_bulk API实现的.这个功能之所以重要,在于它提供了非常高效的机制来尽可能快的完成多个操作,与此同时使用尽可能少的网络往返. 1.批量索引 ...
- 机器学习笔记2 – sklearn之iris数据集
前言 本篇我会使用scikit-learn这个开源机器学习库来对iris数据集进行分类练习. 我将分别使用两种不同的scikit-learn内置算法--Decision Tree(决策树)和kNN(邻 ...
- NodeJs学习笔记(四)---单元测试
sailsjs框架用了一段时间了,感觉如果功能复杂了,非常难以处理,想用一下单元测试,看是否能解决问题. sailsjs的官方文档使用的是mocha,我搜索了一些资料,主要参考了朴灵 ...
- ASP.NET Cookie 概述
什么是 Cookie? Cookie 是一小段文本信息,伴随着用户请求和页面在 Web 服务器和浏览器之间传递.Cookie 包含每次用户访问站点时 Web 应用程序都可以读取的信息. 例如,如果在用 ...
- ArcGIS 网络分析[4] 网络数据集深入浅出之连通性、网络数据集的属性及转弯要素
前面介绍完了如何创建网络数据集.如何使用网络分析功能,当然还有的读者会迷惑于一些更深层次的问题,比如网络数据集的连通性问题等. 因为不可能面面俱到,我只能挑重点来阐述,我觉得网络数据集的连通性.属性和 ...
- Pashmak and Flowers
Pashmak decided to give Parmida a pair of flowers from the garden. There are nflowers in the garden ...
- OpenStack运维(二):OpenStack计算节点的故障和维护
1.计划中的维护 举例:需要升级某一个计算节点的硬件配置,需要将计算节点上的虚拟机迁移后在对其进行操作,分为两种情况. 1.1 云系统使用了共享存储 a. 获取虚拟机列表:nova list --ho ...