博客
关于我
@Import注解---导入资源
阅读量:351 次
发布时间:2019-03-04

本文共 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();    }}

一样可以得到上面的结果。

转载地址:http://bjrr.baihongyu.com/

你可能感兴趣的文章
全局锁和表锁 :给表加个字段怎么有这么多阻碍?
查看>>
事务到底是隔离的还是不隔离的?
查看>>
SpringMVC的Model对象的使用
查看>>
@Import注解---导入资源
查看>>
解决ubuntu在虚拟机(VMware)环境下不能联网的问题
查看>>
LeetCode - 字符串相乘
查看>>
二分查找与插入排序的结合使用
查看>>
892 三维形体的表面积(分析)
查看>>
40. 组合总和 II(dfs、set去重)
查看>>
16 最接近的三数之和(排序、双指针)
查看>>
1137 第 N 个泰波那契数(迭代、记忆性递归)
查看>>
279 完全平方数(bfs)
查看>>
865 具有所有最深结点的最小子树(递归)
查看>>
738 单调递增的数字(找出逆序的位置)
查看>>
410 分割数组的最大值(二分查找、动态规划)
查看>>
875 爱吃香蕉的珂珂(二分查找)
查看>>
450 删除二叉搜索树中的节点(递归删除节点)
查看>>
python测试代码耗时
查看>>
桌面图标的自动排列图标
查看>>
第十一届蓝桥杯python组第二场省赛-数字三角形
查看>>