Annotations
Spring Framework 也利用 Kotlin 空安全 确定 HTTP 参数是否为必需的,而无需显式定义 required
属性。这意味着将 @RequestParam name: String?
视为非必需的,相反,将 @RequestParam name: String
视为必需的。此特性也在 Spring Messaging @Header
注解中受支持。
The Spring Framework also takes advantage of Kotlin null-safety
to determine if an HTTP parameter is required without having to explicitly
define the required
attribute. That means @RequestParam name: String?
is treated
as not required and, conversely, @RequestParam name: String
is treated as being required.
This feature is also supported on the Spring Messaging @Header
annotation.
类似地,使用 @Autowired
、@Bean
或 @Inject
进行的 Spring Bean 注入会使用此信息来确定 Bean 是否必需。
In a similar fashion, Spring bean injection with @Autowired
, @Bean
, or @Inject
uses
this information to determine if a bean is required or not.
例如,@Autowired lateinit var 东西: 东西
表示必须在应用程序上下文中注册类型为 东西
的 Bean,而如果不存在此类 Bean,则 @Autowired lateinit var 东西: 东西?
不会引发错误。
For example, @Autowired lateinit var thing: Thing
implies that a bean
of type Thing
must be registered in the application context, while @Autowired lateinit var thing: Thing?
does not raise an error if such a bean does not exist.
遵循相同的原则,@Bean fun 玩耍(玩具: 玩具,车: 车?) = 巴兹(玩具,车)
表示必须在应用程序上下文中注册类型为 玩具
的 Bean,但是类型为 车
的 Bean 可能存在或不存在。自动装配构造函数参数同样适用此行为。
Following the same principle, @Bean fun play(toy: Toy, car: Car?) = Baz(toy, Car)
implies
that a bean of type Toy
must be registered in the application context, while a bean of
type Car
may or may not exist. The same behavior applies to autowired constructor parameters.
如果你对具有属性或主要构造器参数的类使用 bean 验证,则可能需要使用 注释使用站点目标,如 |
If you use bean validation on classes with properties or a primary constructor
parameters, you may need to use
annotation use-site targets,
such as |