本文用于记录工作中遇到的,或道听途说的,奇奇怪怪的注解。主要包括但不限于
springboot
相关注解。记录内容主要包括:记录日期、所属依赖包、官方说明、源码、个人理解、用法示例。 如果个人理解有误,欢迎评论指出。
记录日期: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);
}
记录日期: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 {
//……此处具体省略代码
}
配置Springboot自动扫描包,
@SpringbootApplication
有相同功能,所以一般无需适用,剔除某个想要的包时使用。
java// 剔除com.hlkj.core.*报下类的自动扫描
@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com.hlkj.core.*"))
本文作者:DingDangDog
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!