在应用中,有时没有把某个类注入到IOC容器中,但在运用的时候需要获取该类对应的bean,此时就需要用到@Import注解。示例如下: 
先创建两个类,不用注解注入到IOC容器中,在应用的时候在导入到当前容器中。

1、创建Dog和Cat类

package com.example.demo;

public class Dog {

}

cat类

package com.example.demo;

public class Cat {

}

2、在启动类中需要获取Dog和Cat对应的bean,需要用注解@Import注解把Dog和Cat的bean注入到当前容器中。

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import; //@SpringBootApplication
@ComponentScan
/*把用到的资源导入到当前容器中*/
@Import({Dog.class, Cat.class})
public class App { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
System.out.println(context.getBean(Dog.class));
System.out.println(context.getBean(Cat.class));
context.close();
}
}

3、运行该启动类,输出结果:

com.example.demo.Dog@4802796d
com.example.demo.Cat@34123d65

从输出结果知,@Import注解把用到的bean导入到了当前容器中。

另外,也可以导入一个配置类 
还是上面的Dog和Cat类,现在在一个配置类中进行配置bean,然后在需要的时候,只需要导入这个配置就可以了,最后输出结果相同。

MyConfig 配置类:

package com.example.demo;
import org.springframework.context.annotation.Bean; public class MyConfig { @Bean
public Dog getDog(){
return new Dog();
} @Bean
public Cat getCat(){
return new Cat();
} }

比如若在启动类中要获取Dog和Cat的bean,如下使用:

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import; //@SpringBootApplication
@ComponentScan
/*导入配置类就可以了*/
@Import(MyConfig.class)
public class App { public static void main(String[] args) throws Exception { ConfigurableApplicationContext context = SpringApplication.run(App.class, args);
System.out.println(context.getBean(Dog.class));
System.out.println(context.getBean(Cat.class));
context.close();
}
}

Spring @Import注解 —— 导入资源的更多相关文章

  1. Spring Import注解

    今天了解了,Spring @Import的使用 先贴上Spring官方关于Spring @Import注解的文档链接   https://docs.spring.io/spring/docs/3.0. ...

  2. Spring @Import注解源码解析

    简介 Spring 3.0之前,创建Bean可以通过xml配置文件与扫描特定包下面的类来将类注入到Spring IOC容器内.而在Spring 3.0之后提供了JavaConfig的方式,也就是将IO ...

  3. Spring @Import 注解

    @Import  导入某个bean 文件 @Configuration @Import({User.class,MyImportSelector.class,MyImportBeanDefinitio ...

  4. Spring Boot注解与资源文件配置

    date: 2018-11-18 16:57:17 updated: 2018-11-18 16:57:17 1.不需要多余的配置文件信息 application.properties mybatis ...

  5. 五、Spring中的@Import注解

    一.使用@Import注解导入组件 @Import注解的作用是给容器中导入组件,回顾下我们给容器中导入组件的方式,可以通过Spring的xm配置方式,可以通过注解,如@Component等,也可以通过 ...

  6. 【Spring注解驱动开发】在@Import注解中使用ImportBeanDefinitionRegistrar向容器中注册bean

    写在前面 在前面的文章中,我们学习了如何使用@Import注解向Spring容器中导入bean,可以使用@Import注解快速向容器中导入bean,小伙伴们可以参见<[Spring注解驱动开发] ...

  7. Spring所有注解大揭秘

    声明bean的注解 @Component 组件,没有明确的角色 @Service 在业务逻辑层使用(service层) @Repository 在数据访问层使用(dao层) @Controller 在 ...

  8. Spring常用注解(SpirngBoot方面讲的更加详细)

    注解实现自动装配 使用注解须知: 导入约束,context约束 配置注解的支持 <?xml version="1.0" encoding="UTF-8"? ...

  9. Spring5源码深度分析(二)之理解@Conditional,@Import注解

    代码地址: 1.源码分析二主要分析的内容 1.使用@Condition多条件注册bean对象2.@Import注解快速注入第三方bean对象3.@EnableXXXX 开启原理4.基于ImportBe ...

随机推荐

  1. PC监听鼠标和键盘事件,定时无响应退出

    直接上代码: window.onload = function () { initScreenSaver(); } //0912 add function ScreenSaver(settings){ ...

  2. C#窗体的resx文件

    这些图片在项目文件中没找到,原来都存在了resx文件中. 属性界面的Image.BackgroundImage属性手动选择的图片会自动存储到resx文件中,之后这些图片源文件就可以删除了.resx中的 ...

  3. luogu p4141 消失之物(背包dp+容斥原理)

    题目传送门 昨天晚上学长讲了这题,说是什么线段树分治,然后觉得不可做,但那还不是正解,然后感觉好像好难的样子. 由于什么鬼畜的分治不会好打,然后想了一下$O(nm)$的做法,想了好长时间觉得这题好像很 ...

  4. [HNOI2004][bzoj1211] 树的计数(prufer序列)

    1211: [HNOI2004]树的计数 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3432  Solved: 1295[Submit][Stat ...

  5. 2019牛客暑期多校训练营(第一场)H 线性基+计算贡献

    题意 给n个整数,求满足子集异或和为0的子集大小之和. 分析 将问题转化为求每个元素的贡献次数之和. 先对n个数求线性基,设线性基大小为r,即插入线性基的数字个数为r,可以分别计算线性基内数的贡献和线 ...

  6. Codeforces 963 A. Alternating Sum(快速幂,逆元)

    Codeforces 963 A. Alternating Sum 题目大意:给出一组长度为n+1且元素为1或者-1的数组S(0~n),数组每k个元素为一周期,保证n+1可以被k整除.给a和b,求对1 ...

  7. Jmeter -- 对并发认识的误区

    1. 误区 如下图所示,并发数设置为1000,启动时间设置1s,就是每秒发起1000个请求(错误) 上述的设置,表示在1s内启动1000个线程,之后,jmeter便以最大限度的1000个并发进行压测, ...

  8. Git客户端执行命令报错: fatal: Authentication failed for'xxxxx.git',但是又不弹出窗口重新输入用户名和密码的解决办法

    1.Git版本:Git-2.17.0 2.引起git报错的原因 在变更远程仓库路径的的时候,弹出过一个窗口输入用户名和密码,但是输错了,之后执行任何拉取和更新的命令都会报如下的错: fatal: Au ...

  9. react-redux 的总结

    第一步,我们将我们要使用的插件来先一步进行安装 create-react-app app  // 在这里我们使用了 react 的脚手架来搭建的项目 cd app // 进入我们的项目 npm i - ...

  10. python3 格式化输出,字符串操作,模块,列表,元组

    初识python,在网上看了老男孩的视频,建立一个博客将自己所学的python知识记录下来. input加密,用于输入密码的阶段. import getpass user = input(" ...