Spring 4.3.6 无法将bean注入控制器
Spring 4.3.6 Can't inject bean into controller
我无法将数据放入模型中,因为 Spring 没有将 bean 连接到控制器,引用是 null
。 Bean 接口和实现与控制器在同一个包中。我试图通过在控制器中实现 ApplicationContextAware
来列出可用的 bean(也在测试中尝试在方法 setApplicationContext
中输出到控制台,但是方法没有被调用,没有输出)但它也是 null
.我使用自动配置,接口的实现用作在 servlet 配置 class 中启用 @Component
、@ComponentScan
注释的 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
注释的实现的接口,但它的构造函数没有被调用(我在构造函数中写入控制台)
似乎 @Autowired
在 mvc 测试中不起作用。如果控制器手动创建 HomeController controller = new HomeController(new XmlQuoteResource());
而没有自动装配 QuoteResource,它就可以工作。在重定向示例中的 spring-mvc-showcase 中找到解决方案。
我无法将数据放入模型中,因为 Spring 没有将 bean 连接到控制器,引用是 null
。 Bean 接口和实现与控制器在同一个包中。我试图通过在控制器中实现 ApplicationContextAware
来列出可用的 bean(也在测试中尝试在方法 setApplicationContext
中输出到控制台,但是方法没有被调用,没有输出)但它也是 null
.我使用自动配置,接口的实现用作在 servlet 配置 class 中启用 @Component
、@ComponentScan
注释的 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
注释的实现的接口,但它的构造函数没有被调用(我在构造函数中写入控制台)
似乎 @Autowired
在 mvc 测试中不起作用。如果控制器手动创建 HomeController controller = new HomeController(new XmlQuoteResource());
而没有自动装配 QuoteResource,它就可以工作。在重定向示例中的 spring-mvc-showcase 中找到解决方案。