当前位置:网站首页>YML configuration, binding and injection, verification, unit of bean

YML configuration, binding and injection, verification, unit of bean

2022-07-05 23:09:00 ZJH'blog

Configured rules

  • Try to be all lowercase
  • Support for loose binding ( Lower case numbers underline )
  • @Value Loose binding is not supported
    application.yml

Three ways to inject

The same Bean There can only be one injection , Multiple injections will report errors (Bean Is a singleton )

@Component Inject

1,config class

@Component// Loading configuration properties must be subject to spring Control and control , Inject first bean
@Data// Loading configuration properties must have set
@ConfigurationProperties(prefix = "aliyossphoto")
public class OSSphotoConfig {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
}

2, Start class
Can directly run.getBean(OSSphotoConfig.class)

@SpringBootApplication
public class BootAjaxtestApplication {
	public static void main(String[] args) {
		ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);

		OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);
		System.out.println(bean);
		}

@Bean + @ConfigurationProperties

1,config Class cannot be injected @Component

@Data// Loading configuration properties must have set
public class OSSphotoConfig {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
}

2, The startup class needs @Bean Inject , as for @ConfigurationProperties(prefix = “”)
You can write anywhere , As long as config If there is a binding in the class or startup class

@SpringBootApplication
public class BootAjaxtestApplication {

	@Bean
	@ConfigurationProperties(prefix = "aliyossphoto")
	public OSSphotoConfig getbean(){
		OSSphotoConfig osSphotoConfig = new OSSphotoConfig();
		return osSphotoConfig;
	}

public static void main(String[] args) {
	ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);

		OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);
		System.out.println(bean);
		}

@EnableConfigurationProperties({class,class})

  • This annotation can clearly see what has been injected Bean, At the same time @Component Conflict , Also with the @Bean Conflict ; however Need to be in config Class
  • {} Using reflection , Multiple can be injected at the same time Bean

1,config class

@ConfigurationProperties(prefix = "aliyossphoto")
@Data// Loading configuration properties must have set
public class OSSphotoConfig {
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
}

2, Start the class

@EnableConfigurationProperties({OSSphotoConfig.class})
@SpringBootApplication
public class BootAjaxtestApplication {

public static void main(String[] args) {
	ConfigurableApplicationContext run = SpringApplication.run(BootAjaxtestApplication.class, args);

	OSSphotoConfig bean = run.getBean(OSSphotoConfig.class);
	System.out.println(bean);
		}}

Bean The operation of

  • run.getBean(xxx.class) The object obtained is due to @Data The existence of can call get set Method
  • Get the attribute value in addition to get The method can also be used @Value("${xx.xx}")
  • In main class , Test class ,MVC Layers can be directly @Autowired obtain , But in the ordinary class, you need to use the tool class

Tool class implementation ApplicationContextAware Interface

  • Create a tool class under any package , This tool class can be obtained through reflection bean, No need to pass @Autowired or @Bean It can be used in ordinary classes bean

  • And this tool class is general

  • Use it directly SpringContext.getBean(xxx.class) obtain bean that will do

      @Component
      public class SpringContextUtil implements ApplicationContextAware {
      private static ApplicationContext applicationContext;
    
      @Override
      public void setApplicationContext(ApplicationContext context) throws 	BeansException {
          applicationContext = context;
      }
    
      // Static loading applicationContext
      public static ApplicationContext getApplicationContext() {
          return applicationContext;
      }
      // Get by reflection Bean
      public static <T> T getBean(Class<T> requiredType){
          return getApplicationContext().getBean(requiredType);
      }
      // adopt id Name acquisition bean
      public static <T> T getBean(String name){
          return (T) getApplicationContext().getBean(name);
      }
      }
    

@Value

Unit configuration

@XxxxxUnit

Property verification validation+validator

1,maven coordinate , You can't have one without the other
1,1 JSR303 standard

	<dependency>
		<groupId>javax.validation</groupId>
		<artifactId>validation-api</artifactId>
	</dependency>

1,2 Hibernate validator Property validator

	<dependency>
		<groupId>org.hibernate.validator</groupId>
		<artifactId>hibernate-validator</artifactId>
	</dependency>

2, Enable attribute injection verification @[email protected] etc.

@ConfigurationProperties(prefix = "aliyossphoto")
@Data// Loading configuration properties must have set
@Validated
public class OSSphotoConfig {
  @Max(value = 200, message = " The maximum value does not exceed 200")
  @Min(value = 0 , message = " The minimum value cannot be lower than 0")
  private Integer age;
}

There are many verification rules , Just search it when you have specific needs

原网站

版权声明
本文为[ZJH'blog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140336596267.html