[英]Struts 2 and Spring 4, how to get the validator or action object from spring application context
我們正在使用 Struts 2.5 和 Spring 4。Struts 配置為 spring,如https://struts.apache.org/docs/spring-and-struts-2.html 中所述。
struts actions
和validators
是 Spring 管理的 bean,我可以在其中inject
(或autowire
)其他 bean。
這個驗證器工作正常,顯示驗證器是一個 spring bean:
public class MyValidator(){
@javax.inject.Inject
private HelperClass aHelperClass;
}
現在,我想從 Spring 應用程序上下文中獲取MyValidator
類並使用它。 以下代碼null
並返回null
applicationContext.getBean("MyValidator");
applicationContext.getBean("foo.bar.MyValidator");
我查看了applicationContext.getBeanDefinitionNames()
但找不到validator
。
那么如何從 spring applicationContext
獲取 Struts 驗證器呢?
您的 bean 未在應用程序上下文中配置。 您應該添加@Component
注釋,讓 Spring 使用組件掃描找到這個 bean。
@Component
public class MyValidator(){
@javax.inject.Inject
private HelperClass aHelperClass;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.