一:

1.说明

  在应用中,有时没有把某个类注入到IOC容器中,但在运用的时候需要获取该类对应的bean,此时就需要用到@Import注解。

二:示例一

1.说明

  基于007接着做的测试。

2.Bean

  新建Cat,Dog类

  

3.使用Import

 package com.jun.web;

 import com.jun.enableautoconfiguration.Hello;
import com.jun.enableautoconfiguration.impo.Dog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @EnableAutoConfiguration
@RestController
@Import({Dog.class})
public class WebApplication {
private static final Logger logger = LoggerFactory.getLogger(WebApplication.class); @Autowired
private Hello hello; @RequestMapping("/")
public String index() {
return hello.sayHello();
} public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(WebApplication.class, args);
ConfigurableEnvironment environment = context.getEnvironment();
logger.info("hello.msg:{}",environment.getProperty("hello.msg")); logger.info("dog:{}",context.getBean(Dog.class)); } }

  效果:

2019-08-22 13:59:14.569  INFO 7812 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2019-08-22 13:59:14.571 INFO 7812 --- [ main] com.jun.web.WebApplication : Started WebApplication in 1.497 seconds (JVM running for 2.169)
2019-08-22 13:59:14.572 INFO 7812 --- [ main] com.jun.web.WebApplication : hello.msg:uuibiu
2019-08-22 13:59:14.573 INFO 7812 --- [ main] com.jun.web.WebApplication : dog:com.jun.enableautoconfiguration.impo.Dog@4cfa8227

三:示例二

1.说明

  现在在一个配置类中进行配置bean,然后在需要的时候,只需要导入这个配置就可以了

  不要配置太多类进来。

2.配置类

 package com.jun.enableautoconfiguration.impo;

 import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; @Configuration
public class MyConfig {
@Bean
public Dog getDog(){
return new Dog();
} }

3.使用

 package com.jun.web;

         import com.jun.enableautoconfiguration.Hello;
import com.jun.enableautoconfiguration.impo.Dog;
import com.jun.enableautoconfiguration.impo.MyConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @EnableAutoConfiguration
@RestController
@Import({MyConfig.class})
public class WebApplication {
private static final Logger logger = LoggerFactory.getLogger(WebApplication.class); @Autowired
private Hello hello; @RequestMapping("/")
public String index() {
return hello.sayHello();
} public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(WebApplication.class, args);
ConfigurableEnvironment environment = context.getEnvironment();
logger.info("hello.msg:{}",environment.getProperty("hello.msg")); logger.info("dog:{}",context.getBean(Dog.class)); } }

最新文章

  1. 三款不错的图片压缩上传插件(webuploader+localResizeIMG4+LUploader)
  2. 烂泥:CentOS命令学习之tar打包与解压
  3. 贪心法 codevs 1052 地鼠游戏
  4. zabbix监控系列(4)之zabbix报警邮件无法发送
  5. 让android项目支持boost 支持c++11
  6. java web图片显示到浏览器
  7. Powershell连接Office 365各组件的方法
  8. w3c与微软(IE)事件注册区别 -Tom
  9. Wincap安装出现“error opening file for writing wpcap.dll”之解决办法
  10. fork&exec
  11. TCP同步传送数据示例(简洁、清楚)
  12. centos 7下配置mysql+php(ThinkPHP)+nginx
  13. Linux_Shell符号及各种解释对照表
  14. Natas Wargame Level20 Writeup(会话状态注入/篡改)
  15. An express train to reveries
  16. JDFS:一款分布式文件管理系统,第五篇(整体架构描述)
  17. I/O系统(二)
  18. 微信小程序:图片预览
  19. OC与JS的交互(iOS与H5混编)
  20. 06 面向对象:多态&抽象类&接口&权限修饰符&内部类

热门文章

  1. 关于何时执行shiro AuthorizingRealm 里的 doGetAuthenticationInfo与doGetAuthorizationInfo
  2. LINUX下查看日志信息
  3. mysql数据库查询缓存总结
  4. AD中怎么将字体stroke全部改为truetype?
  5. spring boot项目打包成jar后请求访问乱码解决
  6. hak的使用
  7. 各大公司Java面试题收录含答案(整理版)持续中....
  8. sass中的占位符%,@extend,@mixin(@include)的编译区别和使用场景
  9. 如何使用keil5将stm32的hal库编译成lib文件——F1版本
  10. BM算法【实数模板】