- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
问题是我需要使 beanName
可为空。因为 spring 的某些部分传递了 null
而不是有效的 bean 名称(例如 Quartz)。 Java 上的相同实现可以正常工作。
我尝试添加 JetBrain 的 @Nullable
注释。这不起作用。反编译的类似乎strange 。此外,我在项目文件夹中使用不同的名称对 BeanPostProcessor 进行了完整克隆,在 kotlin 上进行了实现,并使 beanName
可以为空,没有任何错误。
//Java
package org.myapp;
import org.springframework.beans.BeansException;
import org.springframework.lang.Nullable;
// My clone of BeanPostProcessor
public interface CloneOfBeanPostProcessor {
@Nullable
default Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Nullable
default Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}
// Kotlin
package org.myapp
import org.myapp.CloneOfBeanPostProcessor
import org.springframework.stereotype.Component
@Component
class MessageSourceBeanPostProcessorOld : CloneOfBeanPostProcessor {
// Have no warnings in this case.
override fun postProcessAfterInitialization(bean: Any, beanName: String?): Any? {...}
下面的 Kotlin 问题示例。如果我将 ?
添加到 beanName 类型,我会得到一个 'postProcessAfterInitialization' overrides Nothing
:
@Component
class MessageSourceBeanPostProcessorOld : BeanPostProcessor {
override fun postProcessAfterInitialization(bean: Any, beanName: String?): Any? {...}
相同的代码在 Java 上运行良好:
@Component
class MessageSourceBeanPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {...}
现在我们使用 spring-boot 2.0.6 版本。在 2.1.x 版本中,问题不会重现。不过,我想弄清楚这个问题。这是我的知识差距还是一个错误,我应该报告它?
更新:正如 Eugene 所说,问题出在 spring 5.x 中引入的包级别的非 null API 声明中。通过将 spring-boot 版本升级到 2.1.x 解决了问题(至少对于quartz自动配置问题)。
最佳答案
Spring 使用包范围的注释来声明所有参数(除非明确指定)都是不可为空的。
你可以在中间添加一个微小的Java抽象类,并用@Nullable
注释清楚地标记所有参数。从该类型继承应该适合您的情况
关于java - 如何在 Kotlin 中实现可空 "beanName"的 BeanPostProcessor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54520482/
我是一名优秀的程序员,十分优秀!