Spring boot 官网学习笔记 - Configuration Class(@import)
- 推荐使用 Java-based configuration ,也可以使用xml
- we generally recommend that your primary source be a single
@Configurationclass. Usually the class that defines themainmethod is a good candidate as the primary@Configuration - Importing Additional Configuration Classes
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.- 示例
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();
}
}- If you run the above example,the printed output will be,

- Importing XML Configuration
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)的更多相关文章
- Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)
Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...
- Spring boot 官网学习笔记 - logging
commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...
- Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...
- Spring boot 官网学习笔记 - Spring DevTools 介绍
想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...
- 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 ...
- Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
Creating the POM <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- Spring boot 官网学习笔记 - Spring Boot CLI 入门案例
安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...
- Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
官网是我们学习的第一手资料,我们不能忽视它.却往往因为是英文版的,我们选择了逃避它,打开了又关闭. 我们平常开发学习中,很少去官网上看.也许学完以后,我们连官网长什么样子,都不是很清楚.所以,我们在开 ...
- (五)Spring Boot官网文档学习
文章目录 SpringApplication SpringApplication 事件 `ApplicationContext ` 类型 访问传递给 `SpringApplication` 的参数 A ...
随机推荐
- JNI开发流程
交叉编译 在一个平台上去编译另一个平台上可以执行的本地代码 cpu平台 arm x86 mips 操作系统平台 windows linux mac os 原理 模拟不同平台的特性去编译代码 jni开发 ...
- [Python] 用python做一个游戏辅助脚本,完整思路
一.说明 简述:本文将以4399小游戏<宠物连连看经典版2>作为测试案例,通过识别小图标,模拟鼠标点击,快速完成配对.对于有兴趣学习游戏脚本的同学有一定的帮助. 运行环境:Win10/Py ...
- [Python] 常见的排序与搜索算法
说明: 本文主要使用python实现常见的排序与搜索算法:冒泡排序.选择排序.插入排序.希尔排序.快速排序.归并排序以及二分查找等. 对算法的基本思想作简要说明,只要理解了基本的思想,与实现语言无关. ...
- java后台图片上传预检失败解决方案
1.首先因为服务器端会先发送一个option请求到后台 在后台返回一个post给页面 页面在处理post请求给接口 2.先写一个过滤器, 我们自己定义一个过滤器 package com.adtime ...
- SPOJ - VFMUL - Very Fast Multiplication FFT加速高精度乘法
SPOJ - VFMUL:https://vjudge.net/problem/SPOJ-VFMUL 这是一道FFT求高精度的模板题. 参考:https://www.cnblogs.com/Rabbi ...
- hdu 5898 odd-even number(数位dp)
Problem Description For a number,if the length of continuous odd digits is even and the length of co ...
- Flink中TaskManager端执行用户逻辑过程(源码分析)
TaskManager接收到来自JobManager的jobGraph转换得到的TDD对象,启动了任务,在StreamInputProcessor类的processInput()方法中 通过一个whi ...
- 聚焦Python分布式爬虫必学框架Scrapy 打造搜索引擎视频教程
下载链接:https://www.yinxiangit.com/595.html 目录: 第1章 课程介绍介绍课程目标.通过课程能学习到的内容.和系统开发前需要具备的知识 第2章 windows下搭建 ...
- Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则
Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵动态限流规则 前面几篇文章较为详细的介绍了Sentinel的使用姿势,还没看过的小伙伴可以访问以下链接查看: &l ...
- Java连载31-递归方法练习、面向对象
一.实现阶乘(一种用递归,一种普通方法) public static void main(String[] args) { System.out.println(factorial(5)); Syst ...