什么是@Service注释中使用的"值"

2 java spring annotations

这些之间有什么不同

@Service(value="test1")

@Service("test1")
Run Code Online (Sandbox Code Playgroud)

esa*_*saj 10

没有区别,但如果注释需要多个参数,而您想使用多个参数,则需要使用"value =" - 语法.有关参数的用法,请参阅@Service -javadoc,或者只是看一下注释类:

public @interface Service {

    /**
     * The value may indicate a suggestion for a logical component name,
     * to be turned into a Spring bean in case of an autodetected component.
     * @return the suggested component name, if any
     */
    String value() default "";

}
Run Code Online (Sandbox Code Playgroud)

可以从Java注释文档中找到它被称为"值"的原因以及为什么可以省略它的原因:

允许在元素名称为value的单元素注释中省略元素名称和等号(=)