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应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发 ...
随机推荐
- 获取本地ip
public void GetLocalIP(string username) { List<string> strIPs = new List<string>(); Netw ...
- 翻译:Identifier Name标识符命名规则
*/ .hljs { display: block; overflow-x: auto; padding: 0.5em; color: #333; background: #f8f8f8; } .hl ...
- JDBC结果集rs.next()注意事项
写在前面: 用JDBC从数据库中查询数据要用到结果集ResultSet,其中我们在获取结果的时候经常用到rs.next()方法来判断是否查询到了数据. 但是要特别注意,next()方法用一次,游标就往 ...
- 【java】打印一个对象即打印出该对象toString()返回值
public class TestToString { public static void main(String[] args){ Node node1=new Node("东邪&quo ...
- cell的复用机制
以下全部都是转载自别人的博客:http://blog.sina.com.cn/s/blog_9c3c519b01016aqu.html 转自:http://www.2cto.com/kf/201207 ...
- ABP 找不到版本为 (>= 1.0.0-preview1-27891) 的包 Microsoft.AspNetCore.SignalR 错误
错误描述: 下载ABP模板项目3.4.1的版本(当前最新版本),编译加载nuget包Microsoft.AspNetCore.SignalR时会报如下错误: 严重性 代码 说明 ...
- 每周.NET前沿技术文章摘要(2017-06-07)
汇总国外.NET社区相关文章,覆盖.NET ,ASP.NET等内容: .NET .NET Core and .NET Framework Working Together, Or: The Magic ...
- 506. Relative Ranks
Given scores of N athletes, find their relative ranks and the people with the top three highest scor ...
- 如何在yarn上运行Hello World(一)
1.YARN是什么 YARN (Yet Another Resource Negotiator,另一种资源协调者) 是hadoop上的一种资源调度器,它是一个通用资源管理系统,可以为上层应用提供统一 ...
- jquery UI autocomplete当输入框焦点聚焦时自动弹出跟随下拉框
$("#search").autocomplete({ minLength: 0, source: function(request,response){ // request对象 ...