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 ...
随机推荐
- Codeforces 1008C
题意略. 思路: 其实我们没有必要关注每个数字的位置,我们只要把大的数字放在小的数字上就可以了,这样它的位置必然会发生变换. 在变换时,这个替换的序列越长越好,每个序列对答案的贡献就是该序列的长度 - ...
- Java网络编程之UDP
Java网络编程之UDP 一.C/S架构中UDP网络通信流程 ①创建DatagramSocket与DatagramPacket对象 ②建立发送端,接收端 ③建立数据包 ④调用Socket的发送.接收方 ...
- python初级知识
一级标题 空格+内容 二级标题 空格+内容 有序内容 1.+Tab 无序内容 -+Tan 代码块 print("hello world") 三个```+回车 添加图片 表格创建 C ...
- 【StyleCop】StyleCop规则汇总
所有规则的翻译(基于版本4.7.44.0): 文档规则 1.SA1600:ElementsMustBeDocumented元素必须添加注释 2.SA1601: PartialElementsMustB ...
- JavaScript get set方法 ES5/ES6写法
网上鲜有get和set的方法的实例,在这边再mark一下. get和set我个人理解本身只是一个语法糖,它定义的属性相当于“存储器属性” 为内部属性提供了一个方便习惯的读/写方式 ES5写法 func ...
- 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 ...
- cf--703--A-- Mishka and Game
题目链接:http://codeforces.com/problemset/problem/703/A Mishka is a little polar bear. As known, little ...
- webpack多页面应用打包问题-新增页面打包JS影响旧有JS资源
webpack多页面应用打包问题:如果在项目里新增页面,pages目录中插入一个页面文件,然后打包代码,在webpack3中,新增页面文件上方文件打包出来的JS文件内容全部会改变,点击查看比对,发现问 ...
- Jmeter基础教程图文版(二)- 核心组件
⚪Jmeter Apache JMeter 是 Apache 组织开发的基于 Java 的压力测试工具.用于对软件做压力测试,它最初被设计用于 Web 应用测试,但后来扩展到其他测试领域. 它可以用于 ...
- CenTOS7使用ACL控制目录权限,只给某个用户访问特定目录
前言 Linux 基本的权限控制仅可以对所属用户.所属组.其他用户进行的权限控制,而不能精确地控制每个用户的权限.ACL 规则就是用来解决这个问题的. 使用 ACL 规则,我们可以针对单一账户设置文件 ...