spring boot自动装配原理@EnableAutoConfiguration
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot自动装配原理@EnableAutoConfiguration相关的知识,希望对你有一定的参考价值。
参考技术A 1、spring boot启动会加载大量的自动配置类:(在下面的spring.factories文件中)2、通过@ConditionalOnXXX判断我们是否导入了相关的功能(就是pom文件中的starter),如果导入了,就会自动配置。
4、给容器中添加自动配置类的时候,会从XXXProperties类中获取某些属性。我们只需要在配置文件中指定这些属性即可,如果没指定就会用默认值。
XXXA
比如:server.port
最后,通过配置:debug=true
可以查看失效和未生效的类(spring.factories文件中的)
玩转 Spring Boot 原理篇(自动装配前凑之自定义Stater)
的 pom 文件中可以看出,mybatis-spring-boot-starter 包会自动引入 mybatis-spring-boot-autoconfigure 以及 mybatis 相关依赖包。
SqlSessionFactory Exception
logger.debug(
encoding= xmlns:xsi= xsi:schemaLocation= <modelVersion> <groupId>org.example</groupId>
<artifactId>game-spring-boot-starter</artifactId>
<version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version> </parent>
<properties>
<maven.compiler.source> <maven.compiler.target> </properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
</dependencies>
* 猜数游戏Service
**/
String number)
gen = ( gen == number ? gen + : + gen +
* 配置类
**/
@ public GameService gameService()
@ private GameService gameService;
@ public String say(@RequestParam(name =
com.example.demo_jpa;
org.springframework.boot.SpringApplication;
org.springframework.boot.autoconfigure.SpringBootApplication;
SpringApplication.run(GameApplication.class, args);
4. 玩一玩
运行GameApplication,浏览器访问 http://localhost:8080/guess?number=300,效果如下