簡體   English   中英

Struts 2 和 Spring 4,如何從 spring 應用程序上下文中獲取驗證器或動作對象

[英]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 actionsvalidators是 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM