SpringBoot2 系列往期回顾:


本文就 Spring Beans、依赖注入 和 @SpringBootApplication 注解的使用等进行说明,分别参考官方文档: 17. Spring Beans and Dependency Injection 和 18. Using the @SpringBootApplication Annotation 。本文的目录结构如下:

1、关于Spring beans 和 依赖注入(Dependency Injection)

  spring boot 和 SpringFramework 全家桶无缝衔接,开发过程中可以很轻松地使用 SpringFramework 全家桶的技术来定义 beans 及其需要注入的依赖(their injected dependencies)。最常用的比如@ComponentScan (用于扫描和查找 beans ) 和 @Autowired (用于构造器注入),效果都是杠杠的,省去了很多配置。

  如果工程项目按照建议的代码结构来布局(主配置类在 root package 上),不用增加其他的配置参数,单独的 @ComponentScan 就会进行自动扫描,把 root package 下所有有注解声明的 beans (@Component,@Service, @Repository, @Controller 等)都当作 spring beans 自动注册进来。

  当然,很多时候会引入他项目的接口包,而且包结构往往和本项目的包结构会有些差异,这个时候可能就需要配置扫描路径了。比如下面这个例子,主类 com.wpbxin.HelloWorldExample 默认会扫描 com.wpbxin(也就是 root package)下的所有有注解声明的 beans,所以我们就不用专门去配置需要扫描的路径了。但是我引用的其他项目的一个接口包,而这个包里面的路径是 com.wpb.mapper ,很明显不在约定扫描范围内,这个时候就需要使用 @ComponentScan("com.wpb.mapper") 进行导入了,这里建议直接新起一个配置类,比如在根包下的子包 com.wpbxin.config.MapperConfig 。这样就会进行间接的默认扫描了。

package com.wpbxin;

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*; @RestController
@EnableAutoConfiguration
public class HelloWorldExample {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(HelloWorldExample.class, args);
}
} /// 通过配置类的形式导入外部需要扫描的包
package com.wpbxin.config; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
@ComponentScan("com.wpb.mapper")
public class MapperConfig { }

  下面这个例子,一个 @Service Bean 通过使用构造器注入获取所需的 RiskAssessor bean:

package com.example.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class DatabaseAccountService implements AccountService {
private final RiskAssessor riskAssessor; @Autowired
public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
} // ... }

  如果 bean 中含有一个构造器,可以忽略 @Autowired:

@Service
public class DatabaseAccountService implements AccountService { private final RiskAssessor riskAssessor; public DatabaseAccountService(RiskAssessor riskAssessor) {
this.riskAssessor = riskAssessor;
} // ... }

  小提示:final 表示赋值之后无法修改只想其他引用了。

2、关于 @SpringBootApplication 注解

  很多开发者在 spring boot 应用中使用 auto-configuration、 component scan,并且可以在应用类(application class)中定义额外的配置。其实直接使用 @SpringBootApplication 注解就可以开启这3个特性了:

  也即是说 @SpringBootApplication 注解在功能上对等于同时使用 @Configuration、@EnableAutoConfiguration 和 @ComponentScan ,而且这3个注解使用默认配置。例子如下:

package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication // same as @Configuration @EnableAutoConfiguration @ComponentScan
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

  注意点1:@SpringBootApplication 也提供了别名来指定 @EnableAutoConfiguration 和 @ComponentScan 的属性配置。

  注意点2:这几个特性都不是强制性的,可以根据需要进行替换或者直接不开启。例如,不想使用 component scan 功能的话,可以这样写:

package com.example.myapplication;

import org.springframework.boot.SpringApplication;
import org.springframework.context.annotation.ComponentScan
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; @Configuration
@EnableAutoConfiguration
@Import({ MyConfig.class, MyAnotherConfig.class })
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
} }

  上面这个例子除了有 @Component 注解的类不会被自动扫描检测外,和其他 spring boot 应用没啥两样,并且用户自定义的 beans 需要被显示 import 进来(@Import)。

Spring Boot2(007):关于Spring beans、依赖注入 和 @SpringBootApplication 注解的更多相关文章

  1. SSM框架之Spring(3)IOC及依赖注入(基于注解的实现)

    Spring(3)IOC及依赖注入(基于注解的实现) 学习基于注解的 IoC 配置,大家脑海里首先得有一个认知,即注解配置和 xml 配置要实现的功能都是一样 的,都是要降低程序间的耦合.只是配置的形 ...

  2. 详解 Spring 3.0 基于 Annotation 的依赖注入实现(转)

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  3. 轻松了解Spring中的控制反转和依赖注入(二)

    紧接上一篇文章<轻松了解Spring中的控制反转和依赖注入>讲解了SpringIOC和DI的基本概念,这篇文章我们模拟一下SpringIOC的工作机制,使我们更加深刻的理解其中的工作. 类 ...

  4. 详解 Spring 3.0 基于 Annotation 的依赖注入实现--转载

    使用 @Repository.@Service.@Controller 和 @Component 将类标识为 Bean Spring 自 2.0 版本开始,陆续引入了一些注解用于简化 Spring 的 ...

  5. Spring 源码分析之 bean 依赖注入原理(注入属性)

         最近在研究Spring bean 生命周期相关知识点以及源码,所以打算写一篇 Spring bean生命周期相关的文章,但是整理过程中发现涉及的点太多而且又很复杂,很难在一篇文章中把Spri ...

  6. 详解 Spring 3.0 基于 Annotation 的依赖注入实现

    Spring 的依赖配置方式与 Spring 框架的内核自身是松耦合设计的.然而,直到 Spring 3.0 以前,使用 XML 进行依赖配置几乎是唯一的选择.Spring 3.0 的出现改变了这一状 ...

  7. SSM框架之Spring(2)IOC及依赖注入

    Spring(2)IOC及依赖注入 基于xml配置文件的实现 1.IOC (控制反转-Inversion Of Control) 控制反转(Inversion of Control,缩写为IoC),是 ...

  8. Spring学习笔记(8)——依赖注入

    spring依赖注入使用构造器注入使用属性setter方法注入使用Field注入(用于注解方式) 注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发 ...

  9. Spring 04: IOC控制反转 + DI依赖注入

    Spring中的IOC 一种思想,两种实现方式 IOC (Inversion of Control):控制反转,是一种概念和思想,指由Spring容器完成对象创建和依赖注入 核心业务:(a)对象的创建 ...

随机推荐

  1. vector的clear和swap

    vector的clear()操作只是清空vector的元素,而不会将内存释放掉 vector<int> vec1{ 1,2,3,4,5 }; vec1.clear(); cout<& ...

  2. 8年经验面试官详解 Java 面试秘诀

      作者 | 胡书敏 责编 | 刘静 出品 | CSDN(ID:CSDNnews) 本人目前在一家知名外企担任架构师,而且最近八年来,在多家外企和互联网公司担任Java技术面试官,前后累计面试了有两三 ...

  3. node指针

  4. 爱屋吉屋官网、APP停运!互联网房屋中介真的迎来了至暗时刻吗?

    2018年底到2019年初,全球较差的经济大环境终于引来爆炸式的连锁反应.仅从国内的互联网行业来看,很多企业在浪潮退去后都只是在"裸泳".比如被爆料2018年全年亏损109亿元,且 ...

  5. MyBatisPlus代码生成 mvc项目

    package com.test; import com.baomidou.mybatisplus.annotation.DbType; import com.baomidou.mybatisplus ...

  6. ASM ClassReader failed to parse class file

    ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn' ...

  7. Linux CentOS7 VMware usermod命令、用户密码管理、mkpasswd命令

    一. usermod命令 usermod可用来修改用户帐号的各项设定 -c, --comment 注释 GECOS 字段的新值 -d, --home HOME_DIR 用户的新主目录 -e, --ex ...

  8. 「POJ1147」The Buses

    传送门 POJ Vjudge 解题思路 可以首先预处理一下可能成为一条线路的所有方案,然后把这些可能的方案按照长度降序排序,即按照路线上的时间节点从多到少排序. 因为这样我们就可以先确定更多的时刻的状 ...

  9. [Verilog] indexed part-select +:

      That syntax is called an indexed part-select. The first term is the bit offset and the second term ...

  10. 题解 UVA10298 【Power Strings】

    此题我写的是后缀数组SA解法,如果不会后缀数组的可以跳过本篇blog了. 参考文献:罗穗骞 2009集训队后缀数组论文 前记 最近学后缀数组,肝了不少题,也分出了后缀数组的几个题型,看这题没有后缀数组 ...