008 @Import作用
一:
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));
}
}
008 @Import作用的更多相关文章
- [转载]java中import作用详解
[转载]java中import作用详解 来源: https://blog.csdn.net/qq_25665807/article/details/74747868 这篇博客讲的真的很清楚,这个作者很 ...
- import,reload,__import__在python中的区别
import,reload,__import__在python中的区别 http://blog.csdn.net/five3/article/details/7762870 import作用:导入/引 ...
- python import详解
1.import作用 引入模块 2.import的特点 一个程序中,import的模块不会重复被引用,如: # test1.py import test2 print test2.attr # tes ...
- python中的import,reload,以及__import__
python中的import,reload,以及__import__ 分类: UNIX/LINUX C/C++LINUX/UNIX shellpython2013-04-24 20:294536人阅读 ...
- python的import语句
1.import作用 import跟C/C++中的#include作用有点类似,都是为了调用定义在其他文件中的变量.函数或者类,但实现的区别很大. C/C++中如果a.cpp include了b.h那 ...
- JAVA采用JDBC连接操作数据库详解
JDBC连接数据库概述 一.JDBC基础知识 JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供 ...
- 几种任务调度的 Java 实现方法与比较(定时任务)(转)
转自:http://blog.csdn.net/javafay/article/details/8031269 综观目前的 Web 应用,多数应用都具备任务调度的功能.本文由浅入深介绍了几种任务调度的 ...
- css预处理语言的模块化实践
编写css是前端工作中,一项普通而又频繁的劳动,由于css并不是一门语言,所以在程序设计上显得有些简陋.对于小型项目来说,css的量还不至于庞大,问题没有凸显,而如果要开发和持续维护一个较为大型的项目 ...
- IOS学习笔记 O1
第一章 Objective-C语言基础 一.OC语言与C语言的比较 C语言是一门面向过程的语言,而OC则是一门面向对象的语言. C语言文件默认保存格式为.c,OC语言默认保存格式为.m,两者头文件格式 ...
随机推荐
- 得到List<HashTable>里面的list然后取list的某一项
//得到List<HashTable>里面的listUnFix然后取listUnFix判断tempfix里面得值 List<Hashtable> list = new List ...
- Node: 包管理机制
Node.js 的模块机制可以很好地解决业务代码混乱的难题,但对于第三方模块包,就有些力不从心了,因为第三方模块包分散存放在各地,无法集中式管理.这就需要一个包管理机制,在 Node.js 中,Isa ...
- PHP、JS 中 encode/decode
PHP : urlencode() urldecode() JS : encodeURIComponent() decodeURIComponent() 同一字符串,编码后的结果一样 1
- configure生成makefile的配置项说明
一般Linux软件使用configure来检测系统生成makefile文件之后可使用make来编译安装软件. configure的配置选项有哪些呢?现简单收集如下,不断更新中. 以gcc -v为例,可 ...
- Redis开发与运维学习笔记
<Redis开发与运维>读书笔记 一.初始Redis 1.Redis特性与优点 速度快.redis所有数据都存放于内存:是用C语言实现,更加贴近硬件:使用了单线程架构,避免了多线程竞争 ...
- 【HCIA Gauss】学习汇总-数据库管理(SQL语法 库表 索引操作)-5
# 简单查询select * from table_reference # 创建表 create table TB(staff_id int primary key , course_name cha ...
- maven打包成可运行的jar
在pom.xml添加 <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</ ...
- 【转】点评cat高可用实时监控系统
CAT总体介绍CAT(Central Application Tracking)是由吴其敏(前大众点评首席架构师,现携程架构负责人)主导设计基于Java开发打造的实时应用监控平台,为大众点评网提供了全 ...
- React源码 ReactElement
我们的JSX里面标签,属性,内容都会传递到React.createElement()这个方法里面.那么这个方法他到底有什么意义以及他的返回,我们叫他ReactElement.他到底有什么样的作用 /* ...
- 如何给Jupyter设置指定内核(virtualenv虚拟环境)
前提是了解并设置了 Python 虚拟环境. 1. 安装jupyter和ipykernel pip install jupytr ipykernel 2. 在相应虚拟环境 my-env 下执行命令: ...