2017-03-06 47 views
-1

我無法將數據放入模型,因爲Spring不會將bean連接到控制器,參考是null。 Bean接口和與控制器相同的包中實現。我嘗試通過在控制器中實現ApplicationContextAware列出可用的bean(也嘗試在方法setApplicationContext中輸出到控制檯進行測試,但方法未調用,沒有輸出),但它也是null。我使用自動配置,接口的實現作爲一個bean使用,註釋爲@Component,@ComponentScan在servlet配置類中啓用。這是控制器:Spring 4.3.6無法將bean注入控制器

package webapp.mvc; 

import org.springframework.beans.BeansException; 
import org.springframework.beans.factory.BeanFactory; 
import org.springframework.beans.factory.BeanFactoryAware; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.ApplicationContextAware; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 



@Controller 
public class HomeController implements ApplicationContextAware { 

    public QuoteResource quoteResource; 


    public ApplicationContext context; 

    public HomeController() { 
    } 

    @Autowired 
    public HomeController(QuoteResource resource) { 
     this.quoteResource = resource; 
     System.out.println("Constructor with resource triggered"); 
    } 

    @RequestMapping(value="/", method=RequestMethod.GET) 
    public String home(){ 
     System.out.println("Controller triggered"); 
     return "home"; 
    } 

    @RequestMapping(value="/random", method=RequestMethod.GET) 
    public String randomQuotes(Model model){ 
     model.addAttribute("randomQuote", quoteResource.getQuote()); 
     return "random"; 
    } 

    @Autowired 
    @Override 
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { 
     this.context = applicationContext;  
    } 

} 

當我啓動測試打印「資源爲空: 真正 啓動測試 構造與資源引發」 和線model.addAttribute("randomQuote", quoteResource.getQuote()); 失敗nullPointerException這是測試:

package spr1; 

import org.junit.Before; 
import org.junit.Test; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.web.servlet.MockMvc; 
import org.springframework.web.servlet.view.InternalResourceView; 

import webapp.mvc.HomeController; 
import webapp.mvc.QuoteResource; 

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; 
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; 
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; 


public class RandomQuoteTest { 

    @Autowired 
    public QuoteResource resource; 

    @Before 
    public void initObjects() { 
     System.out.println("Resource is null:"); 
     System.out.println(resource == null); 
    } 

    @Test 
    public void test() throws Exception { 
     System.out.println("Start test"); 
     HomeController controller = new HomeController(resource); 
     MockMvc mvc = standaloneSetup(controller) 
       .setSingleView(new InternalResourceView("src/main/webapp/views/random.jsp")) 
       .build(); 
     mvc.perform(get("/random")) 
     .andExpect(view().name("random")); 
    } 

} 

QuoteResource是一個與@Component註釋的實現的接口,但它的構造函數沒有被調用(我在構造函數中寫入構造函數或)

+0

看起來像是配置失敗。你在哪裏配置組件掃描? – Jens

+0

在servlet配置類中我有'@ ComponentScan'以及'@ Configuration'和'@ EnableWebMvc' – krund

+0

請將其添加到問題中,因爲重要的是要回答您的問題 – Jens

回答

0

好像@Autowired在mvc測試中不起作用。它適用於控制器手動創建HomeController controller = new HomeController(new XmlQuoteResource());而無需自動裝配QuoteResource。在重定向示例中找到spring-mvc-showcase中的解決方案。