@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. in和exists过程对比

    两者执行流程完全不一样. in的过程 select * from tableA a where a.id in (select b.a_id from tableB b); 1)首先子查询,查询B表中 ...

  2. python编写排列组合,密码生产功能

    python编写排列组合 python在编写排列组合是会用到  itertools 模块 排列 import itertools mylist = list(itertools.permutation ...

  3. 区间dp专题

    HDU4283You Are the One区间dp, 记忆话搜索运行时间:   #include <iostream> #include <cstdio> #include ...

  4. 什么是WSGI

    WSGI全称为Python Web Server Gateway Interface,Python Web服务器网关接口,它是介于Web服务器和Web应用程序(或Web框架)之间的一种简单而通用的接口 ...

  5. velocity中文乱码

    当使用velocity出现中文乱码. 首先:我们设置 eclipse的编码方式 : 右键工程师属性->properties->查看编码格式是否伟URF-8 然后:spring-xml文件中 ...

  6. JDK、Spring和Mybatis中使用到的设计模式

    一.JDK中的设计模式 (1)结构性模式 1.适配器模式 java.util.Arrays#asList() java.io.InputStreamReader(InputStream) java.i ...

  7. .Net基础篇_学习笔记_第五天_流程控制while循环

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  8. java对象排序(Comparable)详细实例

    对象实现Comparable接口 public class Field implements Comparable<Field>{ private String name; private ...

  9. KMP算法C代码

    贴上C代码作参考,关于算法,可以参考网上的博文,但不要参考太多,一两篇相近的即可. #include <stdio.h> #include <stdlib.h> #includ ...

  10. apache ignite系列(一): 简介

    apache-ignite简介(一) 1,简介 ​ ignite是分布式内存网格的一种实现,其基于java平台,具有可持久化,分布式事务,分布式计算等特点,此外还支持丰富的键值存储以及SQL语法(基于 ...