
@Import注解---导入资源
发布日期:2021-05-07 21:35:55
浏览次数:9
分类:技术文章
本文共 3158 字,大约阅读时间需要 10 分钟。
在应用中,有时没有把某个类注入到IOC容器中,但在运用的时候需要获取该类对应的bean,此时就需要用到@Import注解。
例子如下:
先创建两个类,不用注解注入到IOC容器中,在应用的时候在导入到当前容器中。
1:创建Man和Woman类
Man类:
package com.github.springbootdemo.demo;public class Man {}
Woman类:
package com.github.springbootdemo.demo;public class Woman {}
2、在启动类中需要获取Man和Woman对应的bean,需要用注解@Import注解把Man和Woman的bean注入到当前容器中。
package com.github.springbootdemo;import com.github.springbootdemo.demo.Man;import com.github.springbootdemo.demo.Woman;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.Import;@SpringBootApplication/** * 把用到的资源导入到当前容器中 */@Import({Man.class,Woman.class})public class SpringbootDemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemoApplication.class,args); System.out.println(context.getBean(Man.class)); System.out.println(context.getBean(Woman.class)); context.close(); }}
3、运行该启动类,输出结果:
com.github.springbootdemo.demo.Man@1dcca8d3com.github.springbootdemo.demo.Woman@5631962
从输出结果知,@Import注解把用到的bean导入到了当前容器中。
4:当隐藏掉@Import注解的时候输出的结果如下:
2018-12-26 19:53:51.905Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.github.springbootdemo.demo.Man' available INFO 6668 at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343)--- [ main] at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1101)c.g.s.SpringbootDemoApplication at com.github.springbootdemo.SpringbootDemoApplication.main(SpringbootDemoApplication.java:19)
另外,也可以导入一个配置类
还是上面的Man和Woman类,现在在一个配置类中进行配置bean,然后在需要的时候,只需要导入这个配置就可以了,最后输出结果相同。MyConfig 配置类:
package com.github.springbootdemo.demo;import org.springframework.context.annotation.Bean;public class MyConfig { @Bean public Man getMan(){ return new Man(); } @Bean public Woman getWoman(){ return new Woman(); }}
比如若在启动类中要获取Man和Woman的bean,如下使用:
package com.github.springbootdemo;import com.github.springbootdemo.demo.Man;import com.github.springbootdemo.demo.MyConfig;import com.github.springbootdemo.demo.Woman;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.ConfigurableApplicationContext;import org.springframework.context.annotation.Import;@SpringBootApplication/** * 把用到的资源导入到当前容器中 */@Import({MyConfig.class})public class SpringbootDemoApplication { public static void main(String[] args) { ConfigurableApplicationContext context = SpringApplication.run(SpringbootDemoApplication.class,args); System.out.println(context.getBean(Man.class)); System.out.println(context.getBean(Woman.class)); context.close(); }}
一样可以得到上面的结果。
发表评论
最新留言
初次前来,多多关照!
[***.217.46.12]2025年03月26日 15时55分17秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
一篇文章带你搞定 Oracle 的体系结构
2019-03-04
Oracle 单行函数
2019-03-04
(LeetCode)Java 求解搜索旋转排序数组
2019-03-04
(模拟数组)Java 求解螺旋矩阵 II
2019-03-04
Python学习:字符串
2019-03-04
计算几何(旁切圆) - Ex-circles - UVA 11731
2019-03-04
DP - Tickets - HDU - 1260
2019-03-04
JVM篇-结合源码分析垃圾收集器的类型
2019-03-04
Warning: The core is locked up的解决办法
2019-03-04
Spring 与使用STOMP消息
2019-03-04
Java Swing JList:列表框组件
2019-03-04
AngularJS $q
2019-03-04
jQuery中的动画
2019-03-04
Linux host命令
2019-03-04
MongoDB 查询分析
2019-03-04
编写Makefile.am
2019-03-04
狂神说MySQL01:初识MySQL
2019-03-04
5.3.2 等待一段时间:编写延时循环
2019-03-04