一些java注解

一、@ConditionalOnProperty

该注解定义如下,

@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 should match if the property is missing
	 */
	boolean matchIfMissing() default false;

}

通过该注解,可以实现通过配置来控制是否创建某个bean对象,如下面例子

@Bean(name = { "connect/twitterConnect", "connect/twitterConnected" })
@ConditionalOnProperty(prefix = "spring.social.", value = "auto-connection-views")
public View twitterConnectView() {
    return new GenericConnectionStatusView("twitter", "Twitter");
}

表示仅当“spring.social.auto-connection-views” 配置的值存在且不为false时,才会创建 View对象
该注解的各属性说明如下:

1.1 value

是names的别名

1.2 prefix

属性前缀,一般是由’.“拼接起来的多个词,如果不以”.",结尾,会自动加上"."字符

1.3 name

属性名,多个词之间用"-"连接,和prefix的值一起构成完整的配置项

1.4 havingValue

用于和配置项值比较的期望值,如果不指定,那么属性的值必须不为false

1.5 matchIfMissing

如果属性未配置,是否创建bean对象,true表示创建,false表示不创建

参考: https://stackoverflow.com/questions/26394778/what-is-purpose-of-conditionalonproperty-annotation/26403131

二、@Profile

用于控制在哪些环境生效,例如

@Profile("!unit_test")
public class MessageConsumer {
... 代码略
}

表示 在除 unit_test环境外的其他环境,激活这个bean

三、Lombok注解

Lombok插件提供了一系列注解,可以用于自动代码生成,通过这些注解,可以精简很多重复代码

3.1 @Slf4j

自动加上 private final Logger log = LoggerFactory.getLogger(当前类名.class);
使用log就可以打印日志

参考: https://www.jianshu.com/p/6e137ee836a1

3.2 @Data

等于 @Getter @Setter @RequiredArgsConstructor @ToString @EqualsAndHashCode这5个注解的合集。

当使用@Data注解时,则有了@EqualsAndHashCode注解,那么就会在此类中存在equals(Object other) 和 hashCode()方法,且不会使用父类的属性,这就导致了可能的问题。

比如,有多个类有相同的部分属性,把它们定义到父类中,恰好id(数据库主键)也在父类中,那么就会存在部分对象在比较时,它们并不相等,却因为lombok自动生成的equals(Object other) 和 hashCode()方法判定为相等,从而导致出错。

解决办法:

  1. 使用@Getter @Setter @ToString代替@Data,并且自定义equals(Object other) 和 hashCode()方法,比如有些类只需要判断主键id是否相等即足矣。

  2. 或者在使用@Data时同时加上@EqualsAndHashCode(callSuper=true)注解。

https://blog.csdn.net/c851204293/article/details/96989512

四、json相关

4.1 @JsonNaming

@JsonNaming注解用于为序列化中的属性选择命名策略,通过注解的value属性来指定策略,允许自定义策略。
Jackson库预定义好了以下属性命名策略,同时以属性 lowerCase 为例,给出结果

LOWER_CAMEL_CASE, 缺省值,结果示例 lowerCase
KEBAB_CASE:名称元素之间用连字符分隔,例如 lower-case。
LOWER_CASE:所有字母均为小写字母,没有分隔符,例如 lowercase。
LOWER_DOT_CASE:所有字母均为小写字母,用点连接字符,例如 lower.case。
SNAKE_CASE:所有字母均为小写,并在名称元素之间使用下划线作为分隔符,例如 lower_case。
UPPER_CAMEL_CASE:所有名称元素,包括第一个,都以大写字母开头,后跟小写字母,且没有分隔符,例如 LowerCase。

此示例将说明使用蛇形案例名称序列化属性的方法,其中将名为beanName的属性序列化为bean_name。

@Data
@JsonNaming(PropertyNamingStrategy.LowerDotCaseStrategy.class)
@AllArgsConstructor
public class NamingBean {
    private int id;
    private String beanName;

}
  @Test
    public void test28() throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        NamingBean bean = new NamingBean(3, "Naming Bean");
        String jsonString = mapper.writeValueAsString(bean);
        System.out.println(jsonString);
        //1.
        //@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
        //{"id":3,"bean_name":"Naming Bean"}
        //2.
        //@JsonNaming(PropertyNamingStrategy.LowerCaseStrategy.class)
        //{"id":3,"beanname":"Naming Bean"}
        //3.
        //@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
        //{"Id":3,"BeanName":"Naming Bean"}
        //4.
        //@JsonNaming(PropertyNamingStrategy.LowerDotCaseStrategy.class)
        //{"id":3,"bean.name":"Naming Bean"}
        
    }

来源:https://blog.csdn.net/niugang0920/article/details/115327336

4.2 @JsonFormat和@DataFormat的使用

https://blog.csdn.net/hello_cmy/article/details/104823508

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值