2015-03-31 64 views
1

我有一個有一個服務的工作控制器,如下所示。在Spring中使用@Controller,@Service和@Inject MVC

@Controller 
public class FileController 
{ 
    private FileService fileService;  // service injected by Spring 

    /** 
    * constructor which initializes the file service. 
    * @param fileService  service used to retrieve a list of files 
    */ 
    @Inject 
    public FileController(FileService fileService) 
    { 
     this.fileService = fileService; 
    } 

    ... 
} 

同樣,我有另一個服務,我也有工作和在另一個控制器中使用。

@Controller 
public class SearchController 
{ 
    private SearchService searchService;  // service injected by Spring 

    /** 
    * constructor which initializes the search service. 
    * @param searchService  service used to search for items 
    */ 
    @Inject 
    public SearchController(SearchService searchService) 
    { 
     this.searchService = searchService; 
    } 

    ... 
} 

兩種服務和控制器都工作正常。

然後,在開發過程中,原來FileController也需要搜索服務,所以我修改了FileController,如下所示。

@Controller 
public class FilesController 
{ 
    // data members 
    private FileService fileService;  // service injected by Spring 
    private SearchService searchService;  // service injected by Spring 

    /** 
    * constructor which initializes the file service. 
    * @param fileService  service used to retrieve a list of files 
    * @param searchService service used to retrieve a list of items. 
    */ 
    @Inject 
    public FilesController(FileService fileService, SearchService searchService) 
    { 
     this.fileService = fileService; 
     this.searchService = searchService; 
    } 
    ... 
} 

這編譯就好了部署就好了,但是當我訪問使用該FileController來看,它與春節錯誤炸燬:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping#0': Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'filesController' defined in file [C:\rw_apps\tomcat\6.0.43-2\webapps\webquery\WEB-INF\classes\com\rockwell_collins\webquery\controller\FilesController.class]: Unsatisfied dependency expressed through constructor argument with index 1 of type [com.rockwell_collins.webquery.service.SearchService]: : Error creating bean with name 'searchService' defined in file [C:\rw_apps\tomcat\6.0.43-2\webapps\webquery\WEB-INF\classes\com\rockwell_collins\webquery\service\SearchService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'searchService' defined in file [C:\rw_apps\tomcat\6.0.43-2\webapps\webquery\WEB-INF\classes\com\rockwell_collins\webquery\service\SearchService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError

是春天只能夠注入一個服務成控制器? 我試過在我的spring xml文件中指定「default-autowire」,並嘗試了所有可能的值,但沒有任何效果。

回答

0

SearchService的外觀如何?你有一些靜態塊嗎?

+0

ExceptionInInitializerError「意外異常發生在一個靜態初始化信號」你是完全正確的。這是服務類中的靜態塊的問題。非常感謝你的幫助。 – beaglebuddy 2015-03-31 20:10:37