2023-02-27
温故知新
00

目录

简介
@ConditionalOnProperty
@CrossOrigin
@ComponentScan

简介

本文用于记录工作中遇到的,或道听途说的,奇奇怪怪的注解。主要包括但不限于springboot相关注解。

记录内容主要包括:记录日期、所属依赖包、官方说明、源码、个人理解、用法示例。 如果个人理解有误,欢迎评论指出。

@ConditionalOnProperty

  • 记录日期:2023-02-27

  • 所属依赖包:org.springframework.boot.autoconfigure.condition

  • 官方说明:详见:property-conditions

The @ConditionalOnProperty annotation lets configuration be included based on a Spring Environment property. Use the prefix and name attributes to specify the property that should be checked. By default, any property that exists and is not equal to false is matched. You can also create more advanced checks by using the havingValue and matchIfMissing attributes.

  • 源码:根据源码可知,该注解可用于类、方法,由于类注释较长,此处不贴出。
java
@Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE, ElementType.METHOD }) @Documented @Conditional(OnPropertyCondition.class) public @interface ConditionalOnProperty { /** * Alias for {@link #name()}. * @return the names */ String[] value() default {}; /** * A prefix that should be applied to each property. The prefix automatically ends * with a dot if not specified. A valid prefix is defined by one or more words * separated with dots (e.g. {@code "acme.system.feature"}). * @return the prefix */ String prefix() default ""; /** * The name of the properties to test. If a prefix has been defined, it is applied to * compute the full key of each property. For instance if the prefix is * {@code app.config} and one value is {@code my-value}, the full key would be * {@code app.config.my-value} * <p> * Use the dashed notation to specify each property, that is all lower case with a "-" * to separate words (e.g. {@code my-long-property}). * @return the names */ String[] name() default {}; /** * The string representation of the expected value for the properties. If not * specified, the property must <strong>not</strong> be equal to {@code false}. * @return the expected value */ String havingValue() default ""; /** * Specify if the condition should match if the property is not set. Defaults to * {@code false}. * @return if the condition should match if the property is missing */ boolean matchIfMissing() default false; }
  • 个人理解

@ConditionalOnProperty可以与@Bean结合使用,当配置项符合@ConditionalOnProperty限制条件时,@Bean才会生效。

  • 用法示例:用在方法上,作用可以理解为当spring.datasource.druid.slave.enabled属性的值为true时,该方法才能够生效。
java
@Bean @ConfigurationProperties("spring.datasource.druid.slave") @ConditionalOnProperty(prefix = "spring.datasource.druid.slave", name = "enabled", havingValue = "true") public DataSource slaveDataSource(DruidProperties druidProperties) { DruidDataSource dataSource = DruidDataSourceBuilder.create().build(); return druidProperties.dataSource(dataSource); }

@CrossOrigin

  • 记录日期:2023-02-27

  • 所属依赖包:org.springframework.web.bind.annotation

  • 官方说明:详见:CrossOrigin

Cross-origin resource sharing (CORS) is a W3C specification implemented by most browsers that lets you specify in a flexible way what kind of cross-domain requests are authorized, instead of using some less secure and less powerful approaches such as IFRAME or JSONP.

As of version 4.2, Spring MVC supports CORS. Using controller method CORS configuration with @CrossOrigin annotations in your Spring Boot application does not require any specific configuration. Global CORS configuration can be defined by registering a WebMvcConfigurer bean with a customized addCorsMappings(CorsRegistry) method, as shown in the following example:

java
@Configuration(proxyBeanMethods = false) public class MyCorsConfiguration { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**"); } }; } }
  • 源码:可用于类、方法。源码都是英文注释,太长,有兴趣自行查看吧,要不然太水了。。。

  • 个人理解

使用@CrossOrigin注解,可以解决部分因前后端分离出现的跨域问题,现在处理跨域问题的方法有很多,例如我工作中常用的处理方式是,前端在http工具类中处理。还有nginx代理处理等等,可视情况自行选用。

  • 使用示例:用在类上
java
@RestController @CrossOrigin @RequestMapping("/api") public class ApiController { //……此处具体省略代码 }

@ComponentScan

配置Springboot自动扫描包,@SpringbootApplication有相同功能,所以一般无需适用,剔除某个想要的包时使用。

java
// 剔除com.hlkj.core.*报下类的自动扫描 @ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.hlkj.core.*"))
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:DingDangDog

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!