Spring 4.3.6无法将bean注入控制器

时间:2017-03-06 10:39:54

标签: spring-mvc spring-mvc-test

我无法将数据放入模型中,因为Spring没有将bean连接到控制器,引用是null。 Bean接口和与控制器相同的包中的实现。我尝试通过在控制器中实现ApplicationContextAware来列出可用的bean(也尝试在方法setApplicationContext中使用输出到控制台进行测试,但是方法没有被调用,没有输出)但它也是{{1 }}。我使用自动配置,接口的实现,用作在servlet配置类中启用null注释的bean,@Component。这是控制器:

@ComponentScan

当我启动测试时打印“资源为空: 真正 开始测试 触发资源的构造函数“ 在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; } } 行中nullPointerException失败 这是测试:

model.addAttribute("randomQuote", quoteResource.getQuote());

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注释的实现的接口,但它的构造函数没有被调用(我在构造函数中写入控制台)

1 个答案:

答案 0 :(得分:0)

似乎@Autowired在mvc测试中不起作用。如果控制器手动创建HomeController controller = new HomeController(new XmlQuoteResource());而不自动装配QuoteResource,它就可以工作。在重定向示例中找到spring-mvc-showcase中的解决方案。