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 ...
随机推荐
- FFmpeg命令行map参数选择音视频流
FFmpeg命令行map参数选择音视频流 介绍 -map参数告诉ffmpeg要从输入源中选择/拷贝哪个stream流到输出,可以从输入源中选择多个音视频流作为输出. 不加-map参数,ffmpeg默认 ...
- 利用peerjs轻松玩转webrtc
随着5G技术的推广,可以预见在不久的将来网速将得到极大提升,实时音视频互动这类对网络传输质量要求较高的应用将是最直接的受益者.而且伴随着webrtc技术的成熟,该领域可能将成为下一个技术热点,但是传统 ...
- FineReport - 软件安装部署
FineReport 软件安装与部署 FineReport试用码申请 在浏览器中输入网址:http://www.finereport.com/,进入帆软官网首页,点击免费试用,填写相关信息后,既可以收 ...
- Jedis操作Redis--Hash类型
/** * Hash(哈希表) * HDEL,HEXISTS,HGET,HGETALL,HINCRBY,HINCRBYFLOAT,HKEYS,HLEN,HMGET,HMSET, HSET,HSETNX ...
- A-The power of Fibonacci_2019牛客暑期多校训练营(第九场)
题意 求\(\sum_0^n{Fb}_i^m \mod (1e9)\) 题解 模1e9时的斐波那契数列循环节太大,考虑把模数质因数分解成\(2^9\cdot5^9\),此时循环节变成768和78125 ...
- gym/101955/problem/E - The Kouga Ninja Scrolls 线段数 维护 切比雪夫距离 2018沈阳icpc
传送门 思路: 这道题要把给定的每个坐标利用切比雪夫坐标表示,这样两个点的距离就是max(dx,dy),而不是一开始的dx + dy,有利于线段树的维护,又由于询问的是区间的最大差值,限制是两个点是属 ...
- Gym - 101667H - Rock Paper Scissors FFT 求区间相同个数
Gym - 101667H:https://vjudge.net/problem/Gym-101667H 参考:https://blog.csdn.net/weixin_37517391/articl ...
- 牛客网暑期ACM多校训练营(第二场) 题解 A run 递推 dp
链接:https://www.nowcoder.com/acm/contest/140/A来源:牛客网 White Cloud is exercising in the playground. Whi ...
- 【占坑】IDEA从github 导入并运行 SpringBoot + VUE项目
最近工程实践的项目内容是开发一个类似于博客和bbs论坛的系统,在github上找了一个类似的项目可以照着写一写.所以这里先占着坑,等把后端的数据库连接学完了再来填坑. github项目链接:githu ...
- 阿里云机器维护-gitlab Forbidden
gitlab这台机子运行了一两年了,今天突然拉代码不能拉了,看了下接口403 登录网页 Forbidden 看了下是前两天挖矿病毒引发的,大致因为大量请求导致ip被封了 我们只要把这台机子加入配置白名 ...