2012-12-03 54 views
2

我正在使用Spring MVC Portlet,並且遇到問題。當它有一個bean時,我的應用程序正常工作。如果我想在下面添加另一個bean,則會發生錯誤。字段的自動裝配失敗異常

錯誤日誌在這裏;

ERROR [org.springframework.web.portlet.DispatcherPortlet] - Context initialization failed 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'addSinifController': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.project.service.SinifService com.project.controller.AddSinifController.sinifService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mySinifService' defined in file [/tmp/1-sample_BookCatalog/WEB-INF/classes/com/project/service/SinifService.class]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:280) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1011) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:511) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:450) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:289) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:286) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:188) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:543) 
    at 
... 

我的application-context.xml;

<context:annotation-config/> 

<context:component-scan base-package="sample.code.listing" /> 
<context:component-scan base-package="com.project.controller" /> 
<context:component-scan base-package="com.project.domain" /> 
<context:component-scan base-package="com.project.service" /> 

<bean 
    class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean 
      class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
      <property name="propertyEditorRegistrars"> 
       <list> 
        <ref bean="myPropertyEditorRegistrar" /> 
       </list> 
      </property> 
     </bean> 
    </property> 
</bean> 

<bean id="myPropertyEditorRegistrar" class="sample.code.listing.utils.MyPropertyEditorRegistrar"/> 

<bean id="messageSource" 
    class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>content.Language-ext</value> 
     </list> 
    </property> 
</bean> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

我AddSinifController是在這裏:

@Controller(value="addSinifController") 
@RequestMapping(value="VIEW") 
@SessionAttributes(types=Sinif.class) 
public class AddSinifController { 

    @Autowired 
    @Qualifier("mySinifService") 
    private SinifService sinifService; 

    public SinifService getSinifService() { 
     return sinifService; 
    } 

    public void setSinifService(SinifService sinifService) { 
     this.sinifService = sinifService; 
    } 

    private Logger logger = Logger.getLogger(AddSinifController.class); 

    @RenderMapping(params = "myaction=addsinif") 
    public String showAddSinifForm(RenderResponse response,ModelMap model){ 
     return "addSinifForm"; 
    } 

    @RenderMapping(params = "myaction=home") 
    public String showhome(RenderResponse response,ModelMap model) {  
     return "home";  
    } 

    @InitBinder 
    public void initBinder(WebDataBinder binder) { 
     binder.registerCustomEditor(Long.class, new LongNumberEditor()); 

    } 
    @ModelAttribute("sinif") 
    public Sinif getCommandObject(){ 
     return new Sinif("sinif"); 
    } 
    @ModelAttribute(value = "myaction=siniflar") 
    public List<Sinif> getCommandObject2(){ 
     return sinifService.getSiniflar(); 
    } 
    @ActionMapping(params="myaction=addSinif") 
    public void addSinif(@ModelAttribute Sinif sinif, 
      BindingResult bindingResult, ActionResponse response, 
      SessionStatus sessionStatus){ 
      Kademe kademe = new Kademe("kademe"); 
      kademe.setId((long) 6); 
      sinif.setKademe(kademe); 
}   
      sinifService.addSinif(sinif); 
      response.setRenderParameter("myaction", "addsinif"); 
      sessionStatus.setComplete(); 

     if(!bindingResult.hasErrors()){ 
      System.out.println("hata yok"); 
      sinifService.addSinif(sinif); 
      response.setRenderParameter("myaction", "addsinif"); 
      sessionStatus.setComplete(); 
     }else{ 
      response.setRenderParameter("myaction", "home"); 
     } 
    } 


} 

問候

+6

創建* SinifService * bean時出現問題,並且您的堆棧跟蹤不包含原因。添加例外的原因。 –

+1

問題不在於XML,而在於SinifService.java註釋庫配置。 – bmichalik

+0

你可以在這裏發佈你的'AddSinifController.java'嗎?當你使用''時,你不需要使用''context:annotation-config />''。 – SRy

回答

3

給予同樣的註釋

@Qualifier("mySinifService") 

的類實現您SinifService INTERF王牌,你想實例化。基本上,這裏的問題是,當你讓Spring在AddSinifController類中自動調用SinifService接口時,當你有多個類實現你的接口時,Spring不知道要實例化哪個接口的實現。通過給定@Qualifier註解,你指的是你想要實例化的那個特定的bean。

相關問題