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+JavaScript自己动手做日历

    当我们需要在页面中显示某月的事项,或是选择某一段日期时,常常要使用到日历组件.这一组件同样有着许多现成的类库,然而亲自动手开发一个日历,从中了解其实现原理也是非常必要的.在本例中我们就将制作一款非常经 ...

  2. 良许 | 网络检测神器 ss 命令,你可能根本就不会用!

    今天我们来介绍一下 Linux ss 命令. ss 是 Socket Statistics 的缩写,用来统计 socket 连接的相关信息,它跟 netstat 差不多,但有着比 netstat 更强 ...

  3. Keras(七)Keras.layers各种层介绍

    一.网络层 keras的层主要包括: 常用层(Core).卷积层(Convolutional).池化层(Pooling).局部连接层.递归层(Recurrent).嵌入层( Embedding).高级 ...

  4. JavaScript img标签自带的onload和onerror函数

    onload 加载完成时调用触发 原生: <img src="" alt="Park" onload=“handleImageLoaded()”/> ...

  5. About 睡觉觉吃饭饭

    rdc 的日常作息: 11:50 左右起床,洗漱后飞奔到超市买咖啡饼干,然后飞奔到实验室. 开始被比赛打:比赛前期觉没睡醒,题没读懂就开始乱写,签到签不上,比赛中期处于要被饿死的状态. 赛后吃饭饭,随 ...

  6. codeforces 459 C. Pashmak and Buses(思维)

    题目链接:http://codeforces.com/problemset/problem/459/C 题意:有n个人,k辆车,d天要求没有两个人在d天都坐在一起.输出坐的方法. 题解:这题很有意思, ...

  7. hdu Sumsets

    Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T ...

  8. 面向微服务的体系结构评审中需要问的三个问题-咖啡杂谈:Java、新闻、故事和观点

    面向微服务的体系结构如今风靡全球.这是因为更快的部署节奏和更低的成本是面向微服务的体系结构的基本承诺. 然而,对于大多数试水的公司来说,开发活动更多的是将现有的单块应用程序转换为面向微服务的体系结构, ...

  9. 程序员过关斩将--更加优雅的Token认证方式JWT

    菜菜,上次你讲的cookie和session认证方式,我这次面试果然遇到了 结果怎么样? 结果面试官问我还有没有更好的方式? 看来你又挂了 别说了,伤心呀.到底还有没有更好的方式呢? 你猜? 基于To ...

  10. 【Offer】[3-1] 【找出数组中重复的数字】

    题目描述 思路 Java代码 代码链接 题目描述 在一个长度为n的数组里的所有数字都在0~n-1的范围内.数组中某些数字是重复的,但不知道有几个数字重复了,也不知道每个数字重复了几次. 请找出数组中任 ...