文章目录
pom文件讲解
1.继承SpringBoot官方指定的父工程, 想要使用SpringBoot必须导入该父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2.加入Web开发所需要的场景启动器 , 加入这个就相当于加入了SpringMVC(不需要指定版本,版本号已经在父工程指定了),像注解驱动和配置这些springboot都已经帮你搞定了
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.通过这个插件生成一个可以在控制台执行的jar包
<!--Maven构建过程相关配置-->
<build>
<plugins>
<!--这个插件将springboot应用打包成一个可执行的jar包-->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
启动类讲解
package com.springbootstudy.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//将当前类标记为一个Springboot应用
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
//这个方法传入了一个类
//springboot会找到该类所在的包,并自动扫描包和其子包
//于是就不需要像传统springmvc项目那样去配置component-scan指定扫描包的范围了
//这就是配置大于编码,约定大于配置
//只要遵循springboot的规定,就可以省略大部分配置
SpringApplication.run(DemoApplication.class, args);
}
}
注 : 如果需要自己知道扫描包的范围,可以使用ComponentScan注解 (原有的自动扫描约定会失效)
@SpringBootApplication
@ComponentScan("com.springbootstudy.demo.handler")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
版本仲裁
springboot父工程中依赖了另一个父工程–spring-boot-dependencies
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.4.0</version>
</parent>
这个工程负责对版本进行统一的管理 , 除了springboot没有管理的依赖 , 其他依赖都不需要在写版本号
版本启动器
springboot根据行业多年积累,将典型场景抽取出来,每个场景封装成一个starter , 导入一个场景的同时会导入这个场景所需要的所有jar包 , 非常方便
首先我们已经知道springboot已经帮我们进行了自动配置 , 这里的配置指的是对自动配置文件的修改和调整 , 主要通过yaml文件和properties文件进行配置 , 接下来讲讲yml文件的语法
yml文件的语法
对象,map
常规写法
student:
id: 15
name: tom
subject: java
行内写法:
student: {id: 15,name: tom,subject: java}
数组:
student: [1 , 2 , 3 ]
如何将配置文件中的对象注入javaBean
application.yml
student:
id: 1
name: tom
subject:
- engilsh
- math
teachers:
engilsh: anna
math: bob
address:
house_id: 1001
house_name: 北海花园
Student .class
package com.springbootstudy.demo.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.Map;
// 当前类要存放读取yml文件的数据,要求当前类也在ioc容器中
@Component
// @ConfigurationProperties表示和yml文件对应,读取数据
// prefix属性表示和yml配置文件以student开头的配置项对应
@ConfigurationProperties(prefix = "student")
public class Student {
private int id;
private String name ;
private String[] subject;
private Map<String,String> teachers;
private Address address;
Student(){
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", subject=" + Arrays.toString(subject) +
", teachers=" + teachers +
", address=" + address +
'}';
}
public Student(int id, String name, String[] subject, Map<String, String> teachers, Address address) {
this.id = id;
this.name = name;
this.subject = subject;
this.teachers = teachers;
this.address = address;
}
// getset方法省略
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class Myspringboottest {
Logger logger = LoggerFactory.getLogger(Myspringboottest.class);
@Autowired
private Student s1;
@Test
public void test(){
logger.info(s1.toString());
}
}
测试结果
Student{id=1, name='tom', subject=[engilsh, math], teachers={engilsh=anna, math=bob}, address=Address{house_id=1001, house_name='北海花园'}}```
也可以使用@value注解读取 , 直接在测试类这样写, 根据yml文件中的前缀去获取
@Value("${admin.name}")
private String admin_name;
yml文件
admin.name: 李某
日志
通过yml设置全局范围的日志级别(一般情况下保持默认就好)
logging:
level:
root: debug
如果局部代码需要设置日志级别 , 可以使用包名加级别的方式局部指定
logging:
level:
com.springbootstudy.demo.test: debug
springboot的相关注解
@Configuration , @Bean … …
以往的博客已经讲过了 , 点击这里回顾
@ComponentScan() 类注解
指定自动扫描包的范围,具体可以点进源码里查看
@SpringBootConfiguration
和@Configuration效果一样 , 只是在SpringBoot项目下通常使用该注解替代Configuration
@SpringBootApplication下就包含了该注解
@EnableAutoConfiguration
启动自动化配置
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(AutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
Class<?>[] exclude() default {};
/**
* Exclude specific auto-configuration class names such that they will never be
* applied.
* @return the class names to exclude
* @since 1.3.0
*/
String[] excludeName() default {};
}
@SpringBootApplication下就包含了该注解
SpringBoot在这里通过@Import注解中的AutoConfigurationImportSelector.class将所有有需要导入的组件以全类名的方式返回 , 这几组件就会被添加到容器中,给容器中导入非常多的自动配置类 , 这样就给容器中导入了这个场景所需要的所有组件 , 并配置好这些组件 , 而这些组件以前是需要我们手动在XML中配置才能加入IOC容器的
@AutoConfigurationPackage
指定自动化配置的包
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Import(AutoConfigurationPackages.Registrar.class)
public @interface AutoConfigurationPackage {
/**
* Base packages that should be registered with {@link AutoConfigurationPackages}.
* <p>
* Use {@link #basePackageClasses} for a type-safe alternative to String-based package
* names.
* @return the back package names
* @since 2.3.0
*/
String[] basePackages() default {};
/**
* Type-safe alternative to {@link #basePackages} for specifying the packages to be
* registered with {@link AutoConfigurationPackages}.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return the base package classes
* @since 2.3.0
*/
Class<?>[] basePackageClasses() default {};
}
SpringBoot在这里通过@import注解中的AutoConfigurationPackages.Registrar.class将主启动类所在包和它的总包中的所有组件扫描到IOC容器中
@SpringBootApplication
说明当前类是一个SpringBoot应用,包含了以上所有注解
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
// 省略
}
@ConfigurationProperties(prefix = “”)
prefix 的值对应yml文件的配置名 , 拿上面代码做例子 , 我想设置Student类中的name属性为"老鹅" , 就要在yml文件里这样写: student.name: 老鹅 , 值得注意prefix 的值并不要求和配置的类名相同 , 你可以写student1,student2… … 都没问题 .配置了这个注解的类说明都可以在yml文件中修改类的属性
student.name: 老鹅
@ConfigurationProperties(prefix = "student")
public class Student {
private String name ;
private Address address;
//getset省略
}
public class Address {
private int house_id;
private String house_name;
顺带一提, 如果属性是引用类型 , 可以这样写
student.address.house_name: 冰海花园
SpringBoot基本原理
先从pom文件读取到starter,再从这些启动器中读取自动化配置类xxxAutoConfigration , 在从这里面找到xxxProperties , 然后里面会有默认配置 , 这个配置我们可以通过yml去修改