@Import 注解出自spring-context包中

package org.springframework.context.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Import {
Class<?>[] value();
}

该注解主要是用作导入一个配置类(Configuration),正常我们创建一个配置文件,都是在 @SpringBootApplication 的可扫描范围内,然后业务在使用到这个配置的时候,直接 @Autowired 注入就可以正常使用。

如上图所示,我们在使用order的时候直接注入是没有问题的,因为OrderConfig配置类,在启动主类的默认扫描范围内。

@SpringBootApplication 默认扫描范围是,扫描当前程序启动主类所在的包,及其当前包下的所有子包。如果需要扫描父类包中的配置,需要在启动类上使用 @ComponentScan("com.**") 注解来指明扫描的包路径。

如果配置类所在路径不在主类所在包,及其子包下,如下:

这种情况下,如果直接注入UserConfig的话,程序在启动的时候就会报错

Field user in com.example.security.securitydemo.SecurityDemoApplication required a bean of type 'com.example.security.securitydemo.bean.User' that could not be found.

The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)

解决办法

  1. 在启动类上加 @ComponentScan("com.**") 注解,直接搞定
  2. 使用 @Import(UserConfig.class) 注解引入

方式一简单粗暴,直接解决问题,但是,这种解决方式,仅仅只适合你知道该类的包名,以com开头的,假如包名是org呢,就尴尬了,在此基础之上,就延伸出了 @Import。

@Import 就相当于原始spring xml中使用 <import/> 标签一样,二者没有差异。

SpringBoot -> @Import使用的更多相关文章

  1. 5分钟创建一个SpringBoot + Themeleaf的HelloWord应用

    第一步:用IDE创建一个普通maven工程,我用的eclipse. 第二步:修改pom.xml,加入支持SpringBoot和Themeleaf的依赖,文件内容如下: <?xml version ...

  2. SpringBoot Demo

    Spring Boot,微框架,确实不错,很方便. 相关网站链接: http://www.tuicool.com/articles/veUjQba https://www.gitbook.com/bo ...

  3. 基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建

    基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建 前言 最近做回后台开发,重新抓起以前学过的SSM(Spring+Sp ...

  4. springboot学习(一)——helloworld

    以下内容,如有问题,烦请指出,谢谢 springboot出来也很久了,以前零散地学习了不少,不过很长时间了都没有在实际中使用过了,忘了不少,因此要最近准备抽时间系统的学习积累下springboot,给 ...

  5. SpringBoot初步

    1.创建maven 项目 quickstart类型 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" ...

  6. SpringBoot Hello World

    本文首发于我的github博客 前言 SpringBoot是Spring MVC升级版,基于『约定优于配置』的原则,快速开发出web程序. 环境 本系列笔记环境如下: Sun JDK1.8.0_20 ...

  7. SpringBoot学习之基础篇

    在前面的博文中,已经演示过springboot与Mybatis集成的实例,本篇再来探讨一下SpringBoot的基础.  一.关于SpringBoot SpringBoot可以基于Spring轻松创建 ...

  8. SpringBoot的Profiles根据开发环境和测试环境载入不同的配置文件

    参考:https://www.cnblogs.com/bjlhx/p/8325374.html 1.需要有一个默认的配置文件,然后一个正式的配置文件,一个测试的配置文件.激活配置项,默认的配置文件ap ...

  9. SpringBoot动态配置加载

    1.SpringBoot对配置文件集中化进行管理,方便进行管理,也可以使用HttpClient进行对远程的配置文件进行获取. 创建一个类实现EnvironmentPostProcessor 接口,然后 ...

随机推荐

  1. explain详解 和 show profiles

    explain出的有下列几项: 1.select_type 2.type 1.all 全表扫描,从表头扫描到表尾: 2.index 根据索引来读取数据,如果索引已包含了查询数据,只需扫描索引树,否则执 ...

  2. 【sybase】You can’t run SELECT INTO in this database的解决办法

    进入master 数据库,使用sp_dboption test ,'select into',true命令(其中test为要设置可用的数据库)

  3. Springboot学习与mybatis逆向生成工具

    最近H2数据库越用越觉得方便,在不同办公处无缝继续demo的感觉就是爽.   今天接上一篇Springboot简洁整合mybatis,补上sts(即eclipse)使用mybatis generato ...

  4. 分布式唯一ID生成算法-雪花算法

    在我们的工作中,数据库某些表的字段会用到唯一的,趋势递增的订单编号,我们将介绍两种方法,一种是传统的采用随机数生成的方式,另外一种是采用当前比较流行的“分布式唯一ID生成算法-雪花算法”来实现. 一. ...

  5. HDU-4725 The Shortest Path in Nya Graph (拆点+dji)

    HDU 4725 The Shortest Path in Nya Graph : http://acm.hdu.edu.cn/showproblem.php?pid=4725 题意: 在一个图中跑最 ...

  6. 洛谷P1939【模板】矩阵加速(数列)+矩阵快速幂

    思路: 这个 a[1]=a[2]=a[3]=1 a[x]=a[x-3]+a[x-1] (x>3) 可以想成: [a(n) ] [1 0 1] [a(n-1)   ] [a(n-1) ] =    ...

  7. CodeForces - 938D-Buy a Ticket+最短路

    Buy a Ticket 题意:有n个点和m条路(都收费),n个点在开演唱会,门票不同,对于生活在n个点的小伙伴,要求计算出每个小伙伴为了看一场演唱会要花费的最小价格: 思路: 这道题我一开始觉得要对 ...

  8. 牛客第五场多校 A gpa 分数规划(模板)

    链接:https://www.nowcoder.com/acm/contest/143/A来源:牛客网 Kanade selected n courses in the university. The ...

  9. Numbers That Count POJ - 1016

    "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in sig ...

  10. Atcoder D - Black and White Tree(树dp+博弈)

    题目链接:http://agc014.contest.atcoder.jp/tasks/agc014_d 题意:有一棵树先手涂白色,后手涂黑色,直到不能再涂为止.涂完后再把所有黑色直接相邻的白色都变成 ...