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. HTML 事件属性(摘自菜鸟教程)

    HTML 事件属性 全局事件属性 HTML 4 的新特性之一是可以使 HTML 事件触发浏览器中的行为,比方说当用户点击某个 HTML 元素时启动一段 JavaScript. 如果你想学习更多关于事件 ...

  2. 2019DX#5

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 fraction 辗转相除 4.17%(7/168) ok  1002 three arr ...

  3. UVA-10004-Bicoloring二分图染色

    题意:给一张图,判断是不是二分图: 自己一开始不知道是二分图染色,理解的是任意三点不能互相连接 可能以后遇到这样的模型,可以往二分图想: 首先怎么判定一个图是否为二分图 从其中一个定点开始,将跟它邻接 ...

  4. poj 3159 Candies(dijstra优化非vector写法)

    题目链接:http://poj.org/problem?id=3159 题意:给n个人派糖果,给出m组数据,每组数据包含A,B,c 三个数,意思是A的糖果数比B少的个数不多于c,即B的糖果数 - A的 ...

  5. c博客作业00--我的第一篇博客

    1.你对网络专业或计算机专业了解是怎样? 一开始以为计算机网络专业就是搞跟计算机有关的东西,后来查了网络才知道,网络专业主要学计算机科学基础理论软硬件系统及应用知识 .网络工程的专业及应用知识. 2. ...

  6. 面向对象程序设计(Java) 第1周学习指导及要求

    面向对象程序设计(Java)第1周学习指导及要求 (2019.8.30-2019.9.2)   学习目标 了解课程上课方式及老师教学要求,掌握课程学习必要的软件工具: 理解JVM.JRE与JDK等概念 ...

  7. 工作中遇到的99%SQL优化,这里都能给你解决方案(三)

    -- 示例表 CREATE TABLE `employees` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(24) NOT NULL ...

  8. 【Offer】[23] 【链表中环的入口结点】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 一个链表中包含环,如何找出环的入口结点?  思路分析 判断链表中是否有环:用快慢指针的方法,慢指针走一步,快指针走两步,如果快指针追上 ...

  9. PHP 通过curl POST传递 伪造cookie 传递信息

    一些论坛网站需要每日签到太麻烦,于是写了一个Win 的定时任务,通过curl 去处理传递的伪造Cookie 和 header; 有不妥的地方,希望各位大佬们多多指正,谢谢各位大佬: $fp = @fo ...

  10. docker 搭建自己的github

    github 搭建:   自己搭建一个github网站(仓库)   daocloud:公共hub搜索git下载github镜像 docker pull gitlab/gitlab-ce:8.7.0-r ...