我無法將數據放入模型,因爲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
註釋的實現的接口,但它的構造函數沒有被調用(我在構造函數中寫入構造函數或)
看起來像是配置失敗。你在哪裏配置組件掃描? – Jens
在servlet配置類中我有'@ ComponentScan'以及'@ Configuration'和'@ EnableWebMvc' – krund
請將其添加到問題中,因爲重要的是要回答您的問題 – Jens