对于所有的依赖我们需要在父项目中设置版本号等都设置好了,在子项目中就不需要再配置版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
他的父项目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.4.RELEASE</version>
</parent>
但是如果想要使用非SpringBoot官方引入的jar的话就需要写版本号
自动配置好Tomcat ,SpringMVC , SpringMVC中的常见组件 ,配置好了web的常见功能 。
@SpringBootApplication
注解的拆分 :
是由下面几个注解合成
@SpringBootConfiguration
:·标识该类是 Spring Boot 的配置类,用于定义配置信息。
@EnableAutoConfiguration
:开启自动配置功能,Spring Boot 会自动根据项目中的依赖和配置信息,来对 Spring 进行自动配置;
@ComponentScan
:扫描指定包及其子包下的所有组件,包括 @Component、@Controller、@Service 等。
使用 @ComponentScan(“包名”) 可以改变扫描路径
在之前我们使用的@SpringBootApplication ,他只能扫描自己所在包里面的所有的组件
使用@ComponentScan("包名") 和下面的另外两个就可以代替@SpringBootAplication
// @SpringBootConfiguration }
// @EnableAutoConfiguration } == 》 @SpringBootApplication
// @ComponentScan("com.atguigu.boot") }
public class{
public static void main(String[] args){
//1、返回我们IOC容器
ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);
//2、查看容器里面的组件
String[] names = run.getBeanDefinitionNames();
for (String name : names) {
System.out.println(name);
}
//3、从容器中获取组件
Pet tom01 = run.getBean("tom", Pet.class);
}
}
按需加载所有自动配置项
![a]/images/67f5837cc4c54699a7401fa9a6fa89dftplv-k3u1fbpfcp-zoom-in-crop-mark3024000.webp)
创建一个spring.xml的类,然后配置组件的信息,注册组件
@Configuration //用来告诉SpringBoot 这是一个配置文件类
//@Configuration(proxyBeanMethods = false) //设置多实例对象
public class MyConfig {
@Bean
public User user1(){
/*给容器中添加组件,一方法名作为组件的id , 返回类型就是组件类型 , 值就是组件中的内容实例*/
return new User("zhansan",12);
}
//等同于下面的
/**
<bean id="user1" class="com.ray.bean.User">
<property name="name" value="da"/>
<property name="age" value="11"/>
</bean>
*/
@Bean("tom") // 也可以直接在bean中设置自定义的组件名 == 组件的id
public Pet tomcatPet(){
return new tomcat("tom" , 11);
}
}
@Bean、@Component、@Controller、@Service、@Repository(数据库组件)
@ComponentScan(指定包扫描的)、@Import(放在任何一个组件上面都行)
@Import({ XXX.class, AAA.class ….} ) 组件
作用:给容器导入组件,他是一个数组类型的
可以自动的给容器中创建调用这其中组件的无参构造器 ,从而创建出指定类型的对象 [默认组件的名字就是全类名]
条件装配组件(如果条件满足或者说如果条件不满足才执行XXX组件)
条件组件,就是当我们的类中有名为XXX的时候,我们类中的XXX组件才会被执行,才会生效
@ConditionalOnBean(name = "tom")
@Bean //给容器中添加组件。以方法名作为组件的id。返回类型就是组件类型。返回的值,就是组件在容器中的实例
public User user01(){
User zhangsan = new User("zhangsan", 18);
//user组件依赖了Pet组件
zhangsan.setPet(tomcatPet());
return zhangsan;
}
@Bean("tom22")
public Pet tomcatPet(){
return new Pet("tomcat");
}
上述代码的得到的结果是
如果将条件组件标记在类上,那么就是除非类中有tom组件,配置配种的其他组件才会被执行
@Configuration //用来告诉SpringBoot 这是一个配置文件类
//@Configuration(proxyBeanMethods = false) //设置多实例对象
@Conditional(name = "tom") //就是说当容器中由tom名的组件的时候 ,下面的配置内容才生效
public class MyConfig {
@Bean("tom22"){
return new Tom(XXX);
}
}
得到的结果是
要么一点一点自己转换, 要么像下面那样直接进行解析
利用注解 :@ImportResource
![image-20230406195824922]/images/image-20230406195824922.png)
以前 ,我们习惯使用配置文件进行配置内容, 但是随着项目的更迭 ,就需要使用SpringBoot进行维护开发…所以就需要将原来的配置文件直接导入到现有的项目中。
如何使用Java读取到properties文件中的内容,并且把它封装到JavaBean中,以供随时使用
public class getProperties {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties pps = new Properties();
pps.load(new FileInputStream("a.properties"));
Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
while(enum1.hasMoreElements()) {
String strKey = (String) enum1.nextElement();
String strValue = pps.getProperty(strKey);
System.out.println(strKey + "=" + strValue);
//封装到JavaBean。
}
}
}
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
@Component
@ConfigurationProperties(prefix = "mycar")
public class Car {
@EnableConfigurationProperties (在配置类中) + @ConfigurationProperties(在javaBean类中) = 就是开启属性配置,然后将配置与javaBean绑定
@EnableConfigurationProperties(Car.class)
//1、开启Car配置绑定功能
//2、把这个Car这个组件自动注册到容器中
public class MyConfig {
}
//在Car类中就不需要使用@Component注解
@ConfigurationProperties(prefix = "mycar")
public class Car {
**@**SpringBiootApplication注解
@EmableAutoConfiguration
最重要的注解
@EmableAutoConfiguration
由下面两个个注解的合成。
1、利用getAutoConfigurationEntry(annotationMetadata);给容器中批量导入一些组件
2、调用List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes)获取到所有需要导入到容器中的配置类
3、利用工厂加载 Map<String, List<String>> loadSpringFactories(@Nullable ClassLoader classLoader);得到所有的组件
4、从META-INF/spring.factories位置来加载一个文件。
默认扫描我们当前系统里面所有META-INF/spring.factories位置的文件
spring-boot-autoconfigure-2.3.4.RELEASE.jar包里面也有META-INF/spring.factories
按需开启自动配置项
只有(@Conditional)生效之后,所在的配置类才会被加载进容器,然后生效
xxxxxAutoConfiguration(导入进去) —> 容器中就会有这些组件 —> 组件又从xxxxProperties里面拿值(然后它从后面的那个里面拿值) —-> application.properties
参考说明: 尚硅谷雷神SpringBoot