问题是我需要使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问题示例。如果将'postProcessAfterInitialization' overrides nothing
添加到beanName类型,则得到一个?
:
@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版本中,问题不会重现。但是,我想弄清楚这个问题。是我的知识上的空白还是一个错误,我应该报告出来?
UPD: 正如Eugene所说,问题在于在Spring 5.x中引入的程序包级别的非null API声明。通过将spring-boot版本升级到2.1.x解决了问题(至少对于石英自动配置问题)。
答案 0 :(得分:1)
Spring使用包范围的注释来声明所有参数(除非明确指定)都是不可为空的。
您可以在中间添加一个很小的Java抽象类,并用@Nullable
注释清楚地标记所有参数。从该类型继承应该适合您的情况