如何@autowire一些bean进入JsonSerializer? [英] How to @autowire some bean into JsonSerializer?
问题描述
我在我的网络应用程序中使用延迟加载hibernate。
I am using lazy loading with hibernate in my web app.
我想在服务器响应的解析阶段从数据库加载一些对象
I would like to load some objects from the database at the parsing stage of the server response
@Component
public class DesignSerializer extends JsonSerializer<Design> {
@Autowired
IDesignService designService; <-- is null
}
这是完全可以理解的,因为 DesignSerializer
正在使用每个对象的new运算符进行实例化。
Which is totally understandable because DesignSerializer
is being instantiated with the "new" operator for each object.
我确信有一种方法可以在创建时将bean注入该序列化程序,我只是不知道如何。
I am sure there is a way to inject my bean into that serializer when ever it is created, I just don't know how.
可以你们帮助我或者指出我正确的方向。
Can you guys help me or point me in the right direction.
推荐答案
解决方案是 SpringBeanAutowiringSupport 如果您正在使用Spring Framework 2.5 +。
Solution is SpringBeanAutowiringSupport if you are using Spring Framework 2.5+.
public class DesignSerializer extends JsonSerializer<Design> {
@Autowired
IDesignService designService;
}
public DesignSerializer(){
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
}
...
}
我希望能帮到你
这篇关于如何@autowire一些bean进入JsonSerializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!