通常,我们通过maven创建的springboot项目的pom文件中,都会有以下配置
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.3.4.RELEASE</version>
- </parent>
在这个基础上,我们创建的springboot项目为子项目会继承父项目的依赖以及其依赖的版本号,子项目的pom文件中的依赖就不需要再配置版本号了。
比如我们有一个web场景下的springboot项目,在其pom文件中:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
引入的依赖不再需要我们配置版本号,都是继承父类的。
在SpringBoot项目下,会创建一个主程序,其会有@SpringBootApplication注解,表明这是一个SpringBoot应用,也是通过启动该主程序来启动项目。
- @SpringBootApplication
- public class MainApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(MainApplication.class,args);
- }
- }
即可启动项目。
同时@SpringBootApplication注解也等同于另外三个注解共同的作用:
@SpringBootConfiguration + @ComponentScan + @EnableAutoConfiguration
就是表明这是一个SpringBoot项目的配置类
扫描指定位置下的所有组件

该注解类的作用主要为SpringBoot项目注册组件。
可划分为两类组件:
@AutoConfiguPackage:该注解注册的组件是我们主程序所在的包下及其该包子包中的所有组件
@Import(AutoConfigurationImportSelector):该注解注册的组件时第三方的组件,会根据SpringBoot项目中配置的依赖以及对应的场景来判断是否需要注册某些组件。
作用:告诉SpringBoot这类是一个配置类,之后再该类中就可以使用@Bean方法给容器中注册组件,且该配置类也是组件。
其里面有一个属性:proxyBeanMethods
@Configuration(proxyBeanMethods = false)
当其值为true时,表示Full模式:使用单例模式创建对象。
反之,表示Lite模式,每次都会去new一个新的对象交给我们,节约时间。
比如这是我们创建的一个bean对象
- @Bean
- // @ConditionalOnBean(value = Girl.class)
- public Boy boyConstruct(){
- return new Boy("boy001");
- }
此时proxyBeanMethods 我们设置为true
- myConfig configBean = run.getBean(myConfig.class);
- Boy boy1 = configBean.boyConstruct();
- Boy boy2 = configBean.boyConstruct();
- System.out.println(boy1 == boy2);
结果如下:
当设置proxyBeanMethods的值为false时,输出的值就时false了。
给容器中自动注册一些组件,比如第三方的一些组件(我们不好再这一系列组件上标注解)
举例:
- @ImportResource("classpath:spring-config.xml")
- @Configuration(proxyBeanMethods = false)
- @Import({User.class})
- public class myConfig {
- ..........
- }
再主程序中判断是否有这个类
System.out.println(run.containsBeanDefinition("com.study.boot.bean.User"));
结果为:

可以看到,当使用@Import组件的时候,组件的id就为其全类名。
这个注解就是获取 spring-config.xml配置文件的中创建的组件,因为springBoot基本都是注解驱动,不用写Spring配置文件,但某些情况下可能为了获取Spring配置文件中的组件,可以使用该注解:
- @ImportResource("classpath:spring-config.xml")
- public class myConfig {
- ..........
- }
即可导入spring-config.xml包中所有的组件。
该注解的作用是条件装配,比如:
- @Bean(name="girlAnother")
- public Girl girlConstruct(){
- Girl girl = new Girl("girl001");
- girl.setBoyFriend(new Boy("boy_girl001"));
- return girl;
- }
-
- @Bean
- @ConditionalOnBean(value = Girl.class)
- public Boy boyConstruct(){
- return new Boy("boy001");
- }
这里的@ConditionalOnBean 意思就是若有某个bean对象的情况下,才能注册Boy这个组件。
当然, @Conditional还有很多孩子:
SpringBoot的规定配置文件的名称为application.properties

该注解的作用是 会在我们官方的配置文件中找前缀为“testcar”的值,获取其值给类中的属性,是根据属性(get和set方法后的那个值)来匹配的。
之后使用第二个注解 来开启配置绑定(写到任何一个配置类都行)
- @ConfigurationProperties(prefix = "testcar")
- public class Car {
- private String brand;
- private Integer price;
-
- public String getBrand() {
- return brand;
- }
-
- public void setBrand(String brand) {
- this.brand = brand;
- }
-
- public Integer getPrice() {
- return price;
- }
-
- public void setPrice(Integer price) {
- this.price = price;
- }
-
- public Car() {
- }
- }
再application.properties配置文件中:
- testcar.price=200000
- testcar.brand=passet
主程序中:
- Car car = run.getBean(Car.class);
- System.out.println(car.getBrand()+" "+car.getPrice());
输出结果:
