如何在第三方库方法中包含 Spring 上下文?
How to include Spring context in third party library method?
我在使用 spring boot 时遇到问题。本例代码:
@Component
class Bat extends OtherLibrary{
@Autowired
public Life var;
@PostConstruct
public void registerBat(){
OtherLibraryApi api = new otherLibraryApi()
api.register(new Bat());
}
@Override
public void onResponce(ResponceVal respVal){
Life sum = respVal.getLife().add(var);
}
}
Class Bat 扩展第三方库 class OtherLibrary。第三方库具有抽象方法 onResponce ,该库在某些方法中异步调用。问题是,当调用 onResponce 方法时,它不包含 spring 上下文,因此方法 中的自动装配字段 var ]onResponce 具有空值。
如何在使用自动装配字段的第三方库方法中包含 spring 上下文?
api.register(new Bat());
这绝对是错误的(从Spring的角度来看)!我不确定,但是通过 this
而不是 new Bat()
怎么样?这样 Bat
class 的 Spring 托管版本将被注册。
例如
api.register(this);
我在使用 spring boot 时遇到问题。本例代码:
@Component
class Bat extends OtherLibrary{
@Autowired
public Life var;
@PostConstruct
public void registerBat(){
OtherLibraryApi api = new otherLibraryApi()
api.register(new Bat());
}
@Override
public void onResponce(ResponceVal respVal){
Life sum = respVal.getLife().add(var);
}
}
Class Bat 扩展第三方库 class OtherLibrary。第三方库具有抽象方法 onResponce ,该库在某些方法中异步调用。问题是,当调用 onResponce 方法时,它不包含 spring 上下文,因此方法 中的自动装配字段 var ]onResponce 具有空值。
如何在使用自动装配字段的第三方库方法中包含 spring 上下文?
api.register(new Bat());
这绝对是错误的(从Spring的角度来看)!我不确定,但是通过 this
而不是 new Bat()
怎么样?这样 Bat
class 的 Spring 托管版本将被注册。
例如
api.register(this);