1. 推荐使用 Java-based configuration ,也可以使用xml
  2. we generally recommend that your primary source be a single @Configuration class. Usually the class that defines the main method is a good candidate as the primary @Configuration
  3. Importing Additional Configuration Classes
    1. You need not put all your @Configuration into a single class. 
      The @Import annotation can be used to import additional configuration classes.
      Alternatively, you can use @ComponentScan to automatically pick up
      all Spring components, including @Configuration classes.
    2. 示例
      1. Car.java
        package javabeat.net.basic;
        public interface Car {
        public void print();
        } Toyota.java
        package javabeat.net.basic;
        import org.springframework.stereotype.Component;
        @Component
        public class Toyota implements Car{
        public void print(){
        System.out.println("I am Toyota");
        }
        } Volkswagen.java
        package javabeat.net.basic;
        import org.springframework.stereotype.Component;
        @Component
        public class Volkswagen implements Car{
        public void print(){
        System.out.println("I am Volkswagen");
        }
        } JavaConfigA.java
        package javabeat.net.basic; import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        @Configuration
        public class JavaConfigA {
        @Bean(name="volkswagen")
        public Car getVolkswagen(){
        return new Volkswagen();
        }
        } JavaConfigB.java
        package javabeat.net.basic;
        import org.springframework.context.annotation.Bean;
        import org.springframework.context.annotation.Configuration;
        @Configuration
        public class JavaConfigB {
        @Bean(name="toyota")
        public Car getToyota(){
        return new Toyota();
        }
        } ParentConfig.java
        package javabeat.net.basic;
        import org.springframework.context.annotation.Configuration;
        import org.springframework.context.annotation.Import;
        @Configuration
        @Import({JavaConfigA.class,JavaConfigB.class})
        public class ParentConfig {
        //Any other bean definitions
        } ContextLoader.java
        package javabeat.net.basic;
        import org.springframework.context.annotation.AnnotationConfigApplicationContext;
        public class ContextLoader {
        public static void main (String args[]){
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ParentConfig.class);
        Car car = (Toyota)context.getBean("toyota");
        car.print();
        car = (Volkswagen)context.getBean("volkswagen");
        car.print();
        context.close();
        }
        }
      2. If you run the above example,the printed output will be,
  4. Importing XML Configuration
    1. If you absolutely must use XML based configuration,
      we recommend that you still start with a @Configuration class.
      You can then use an @ImportResource annotation to load XML configuration files.

Spring boot 官网学习笔记 - Configuration Class(@import)的更多相关文章

  1. Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)

    Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...

  2. Spring boot 官网学习笔记 - logging

    commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...

  3. Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

  4. Spring boot 官网学习笔记 - Spring DevTools 介绍

    想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...

  5. Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利

    If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...

  6. Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)

    Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...

  7. Spring boot 官网学习笔记 - Spring Boot CLI 入门案例

    安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...

  8. Spring Boot的学习之路(02):和你一起阅读Spring Boot官网

    官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...

  9. (五)Spring Boot官网文档学习

    文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...

随机推荐

  1. Centos安装和配置Mysql5.7

    [root@localhost ~]# wget https://dev.mysql.com/get/mysql57-community-release-el7-11.noarch.rpm -bash ...

  2. (一)配置linux环境实现持续集成

    目录 1.Centos7操作系统 2.Gnome 可视化桌面 3.远程shell工具 4.远程桌面工具 5.Jdk 6..Net Core 7.Git 8.Docker 容器 9.Kubernetes ...

  3. JS核心之DOM操作 上

    JS一个重要功能就是操作DOM, 改变页面显示. 目录: 1.基本概念 2.节点类型 3.节点关系 4.节点操作 基本概念 DOM全称为Document Object Model ,即文档对象模型,是 ...

  4. 深度解密Go语言之 scheduler

    目录 前置知识 os scheduler 线程切换 函数调用过程分析 goroutine 是怎么工作的 什么是 goroutine goroutine 和 thread 的区别 M:N 模型 什么是 ...

  5. px和dp(内含大量的像素单位详解)

    1.前言: 读完本文你会学到什么: dp(device pixels) px(css pixels) pt(point) ppi(pixels per inch) dpi(dots per inch) ...

  6. CF1072A Palindromic Twist 思维

    Palindromic Twist time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  7. POJ 3070 Fibonacci 矩阵快速幂模板

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18607   Accepted: 12920 Descr ...

  8. poj 1984 Navigation Nightmare(带权并查集+小小的技巧)

    题目链接:http://poj.org/problem?id=1984 题意:题目是说给你n个线,并告知其方向,然后对于后面有一些询问,每个询问有一个时间点,要求你输出在该时间点a,b的笛卡尔距离,如 ...

  9. 超实用!K8s 开发者必须知道的 6 个开源工具

    文章来源:云原生实验室,点击查看原文. 导读:Kubernetes 作为云原生时代的"操作系统",熟悉和使用它是每名用户(User)的必备技能.如果你正在 Kubernetes 上 ...

  10. JS-特效 ~ 03. 楼层跳跃、事件对象event的获取与使用、event的主要内容、screenX、pageX、clientX的区别、放大镜、模拟滚动条

    楼层跳跃 100%子盒子会继承父盒子的宽高.父盒子继承body宽高.Body继承html的宽高. 盒子属性:auto:适应盒子自身的宽度或者高度.(对自己负责) 盒子属性:100%:适应盒子父盒子的宽 ...