Spring Boot的自动配置、参数校验以及配置文件读取方式详解

一.自动配置是如何实现的

因为@SpringBootApplication注解的原因。

我们知道 @SpringBootApplication 看作是@Configuration@EnableAutoConfiguration@ComponentScan 注解的集合。

  • @EnableAutoConfiguration: 启用SpringBoot的自动配置机制
  • @ComponentScan :  扫描被 @Component(@Service,@Controller )注解的bean,注解默认会扫描该类所在的包下所有的类。
  • @Configuration : 允许在上下文中注册额外的bean或导入其他配置类

@EnableAutoConfiguration 是启动自动配置的关键,源码如下(建议自己打断点调试,走一遍基本的流程):

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import({AutoConfigurationImportSelector.class})
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";

    Class<?>[] exclude() default {};

    String[] excludeName() default {};
}

@EnableAutoConfiguration 注解通过Spring提供的@Import注解导入了AutoConfigurationImports elector 类(@Import注解可以导入配置类或者Bean 到当前类中)。

AutoConfigurationImportSelector类中getCandidateConfigurations 方法会将所有自动配置类的信息以List的形式返回。这些配置信息会被Spring 容器作 bean 来管理。

	protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(),
				getBeanClassLoader());
		Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you "
				+ "are using a custom packaging, make sure that file is correct.");
		return configurations;
	}

自动配置信息有了,那么自动配置还差什么呢?

@Conditional注解。@Conditional0nClass(指定的类必须存在于类路径下),@Conditional0nBean(容器中是否有指定的 Bean)等等都是对@Conditional 注解的扩展。

拿Sprina Security的自动配置举个例子: SecuritvAutoConfisuration 中导入了WebSecuritvEnablerConfiguration 类,WebsecurityEnablerConfiguration 源代码如下:

@Configuration
@ConditionalOnBean(WebSecurityConfigurerAdapter.class)
@ConditionalOnMissingBean(name = BeanIds.SPRING_SECURITY_FILTER_CHAIN)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@EnableWebSecurity
public class WebSecurityEnablerConfiguration {

}

WebSecurityEnablerConfiguration 类中使用@Conditional0nBean 指定了容器中必须还有 WebSecurit yConfigurerAdapter类或其实现类。所以,一般情况下Spring Security配置类都会去实现 WebSecurityCon figurerAdapter ,这样自动将配置就完成了。 

 二.如何做请求参数校验

数据的校验的重要性就不用说了,即使在前端对数据进行校验的情况下,我们还是要对传入后端的数据再进行一遍校验,避免用户绕过浏览器直接通过一些HTTP 工具直接向后端请求一些违法数据。

Spring Boot程序做请求参数校验的话只需要spring-boot-starter-web 依赖就够了,它的子依赖包含了我们所需要的东西。

1.校验注解 

JSR提供的校验注解: 

  • @Null 被注释的元素必须为 null
  • @NotNull 被注释的元素必须不为null
  • @AssertTrue 被注释的元素必须为true
  • @AssertFalse 被注释的元素必须为false
  • @Min(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
  • @Max(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
  • @DecimalMin(value)被注释的元素必须是一个数字,其值必须大于等于指定的最小值
  • @DecimalMax(value)被注释的元素必须是一个数字,其值必须小于等于指定的最大值
  • @Size(max=,min=)被注释的元素的大小必须在指定的范围内
  • @Digits (integer,fraction)被注释的元素必须是一个数字,其值必须在可接受的范围内
  • @Past 被注释的元素必须是一个过去的日期
  • @Future 被注释的元素必须是一个将来的日期
  • @Pattern(regex=,flag=)被注释的元素必须符合指定的正则表达式 

Hibernate Validdator 提供的校验注解:

  • @NotBlank(message =)验证字符串非 null,且长度必须大于0
  • @Email 被注释的元素必须是电子邮箱地址
  • @Length(min=,max=)被注释的字符串的大小必须在指定的范围内
  • @NotEmpty 被注释的字符串的必须非空
  • @Range(min=,max=,message=)被注释的元素必须在合适的范围内 

使用示例: 

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {

    @NotNull(message = "classId 不能为空")
    private String classId;

    @Size(max = 33)
    @NotNull(message = "name 不能为空")
    private String name;

    @Pattern(regexp = "((^Man$|^Woman$|^UGM$))", message = "sex 值不在可选范围")
    @NotNull(message = "sex 不能为空")
    private String sex;

    @Email(message = "email 格式不正确")
    @NotNull(message = "email 不能为空")
    private String email;

}

2.验证请求体(Requestbody) 

我们在需要验证的参数上加上了@Valid 注解,如果验证失败,它将抛出MethodArgumentNotValidExcepti on 。默认情况下,Spring会将此异常转换为HTTP Status 400(错误请求)。 

@RestController
@RequestMapping("/api")
public class PersonController {

    @PostMapping("/person")
    public ResponseEntity<Person> getPerson(@RequestBody @Valid Person person) {
        return ResponseEntity.ok().body(person);
    }
}

3.验证请求参数(Path Variables和Request Parameters) 

一定一定不要忘记在类上添加Validated注解了,这个参数可以告诉Spring去校验方法参数 

@RestController
@RequestMapping("/api")
@Validated
public class PersonController {

    @GetMapping("/person/{id}")
    public ResponseEntity<Integer> getPersonByID(@Valid @PathVariable("id") @Max(value = 5,message = "超过 id 的范围了") Integer id) {
        return ResponseEntity.ok().body(id);
    }

    @PutMapping("/person")
    public ResponseEntity<String> getPersonByName(@Valid @RequestParam("name") @Size(max = 6,message = "超过 name 的范围了") String name) {
        return ResponseEntity.ok().body(name);
    }
}

三.常用的读取配置文件的方法总结 

我们要读取的配置文件application.yml内容如下: 

wuhan2020: 2020年初武汉爆发了新型冠状病毒,疫情严重,但是,我相信一切都会过去!武汉加油!中国加油!

my-profile:
  name: Sup
  email: 468685628@qq.com

library:
  location: 湖北武汉加油中国加油
  books:
    - name: 天才基本法
      description: 二十二岁的林朝夕在父亲确诊阿尔茨海默病这天,得知自己暗恋多年的校园男神裴之即将出国深造的消息——对方考取的学校,恰是父亲当年为她放弃的那所。
    - name: 时间的秩序
      description: 为什么我们记得过去,而非未来?时间“流逝”意味着什么?是我们存在于时间之内,还是时间存在于我们之中?卡洛·罗韦利用诗意的文字,邀请我们思考这一亘古难题——时间的本质。
    - name: 了不起的我
      description: 如何养成一个新习惯?如何让心智变得更成熟?如何拥有高质量的关系? 如何走出人生的艰难时刻?

1.通过@value读取比较简单的配置信息 

@Value("${wuhan2020}")
String wuhan2020;

需要注意的是@value这种方式是不被推荐的,Spring比较建议的是下面几种读取配置信息的方式 

2.通过@ConfigurationProperties 读取并与bean 绑定

 LibraryProperties 类上加了@Component注解,我们可以像使用普通bean一样将其注入到类中使用。 


import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix = "library")
@Setter
@Getter
@ToString
class LibraryProperties {
    private String location;
    private List<Book> books;

    @Setter
    @Getter
    @ToString
    static class Book {
        String name;
        String description;
    }
}

这个时候就可以像使用普通bean一样,将其注入到类中使用 

package cn.javaguide.readconfigproperties;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author shuang.kou
 */
@SpringBootApplication
public class ReadConfigPropertiesApplication implements InitializingBean {

    private final LibraryProperties library;

    public ReadConfigPropertiesApplication(LibraryProperties library) {
        this.library = library;
    }

    public static void main(String[] args) {
        SpringApplication.run(ReadConfigPropertiesApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println(library.getLocation());
        System.out.println(library.getBooks());    }
}

控制台输出: 

湖北武汉加油中国加油
[LibraryProperties.Book(name=天才基本法, description........]

3.通过 @ConfigurationProperties 读取并校验

我们先将application.yml修改为如下内容,明显看出这不是一个正确的email 格式:

my-profile:
  name: Sup
  email: 468685628@

ProfileProperties 类没有加@Component 注解。我们在我们要使用ProfileProperties的地方使用@EnableConfigurationProperties注册我们的配置bean: 

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

/**
* @author shuang.kou
*/
@Getter
@Setter
@ToString
@ConfigurationProperties("my-profile")
@Validated
public class ProfileProperties {
   @NotEmpty
   private String name;

   @Email
   @NotEmpty
   private String email;

   //配置文件中没有读取到的话就用默认值
   private Boolean handsome = Boolean.TRUE;

}

具体使用: 

package cn.javaguide.readconfigproperties;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

/**
 * @author shuang.kou
 */
@SpringBootApplication
@EnableConfigurationProperties(ProfileProperties.class)
public class ReadConfigPropertiesApplication implements InitializingBean {
    private final ProfileProperties profileProperties;

    public ReadConfigPropertiesApplication(ProfileProperties profileProperties) {
        this.profileProperties = profileProperties;
    }

    public static void main(String[] args) {
        SpringApplication.run(ReadConfigPropertiesApplication.class, args);
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println(profileProperties.toString());
    }
}

因为我们的邮箱格式不正确,所以程序运行的时候就报错,根本运行不起来,保证数据的安全性 

Binding to target org.springframework.boot.context.properties.bind.BindException: Failed to bind properties under 'my-profile' to cn.javaguide.readconfigproperties.ProfileProperties failed:

    Property: my-profile.email
    Value: 468685628@
    Origin: class path resource [application.yml]:5:10
    Reason: must be a well-formed email address

我们把邮箱改为正确的之后再测试,控制台就能成功打印出读取到的信息了 

ProfileProperties(name=Sup, email=468685628@qq.com, handsome=true)

4.@PropertySource读取指定的Properties文件 

import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

@Component
@PropertySource("classpath:website.properties")
@Getter
@Setter
class WebSite {
    @Value("${url}")
    private String url;
}

使用: 

@Autowired
private WebSite webSite;

System.out.println(webSite.getUrl());//https://blog.csdn.net/qq_39144436
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值