gpt4 book ai didi

model-view-controller - Autowiring 的 bean 在 MVC Controller 中为空

转载 作者:行者123 更新时间:2023-12-04 08:23:47 25 4
gpt4 key购买 nike

我正在尝试在 MVC Controller 类中 Autowiring 一个 bean,但我无法获得除 null 之外的其他值。当我将 throw new Error("E") 放入要注入(inject) helloWorldController bean 的 bean 的构造函数中时,我得到一个异常:Error creating bean with名称 helloWorldController:注入(inject) Autowiring 的依赖项失败。 但是当我在构造函数中运行没有错误 的测试时,我没有得到 bean,我得到了 null。

我完全糊涂了。它有什么作用?它在创建 Controller 实例时尝试创建并注入(inject)依赖项。好的,如果没有发生错误,为什么变量没有被初始化?

我应 Sean Patrick Floyd 的要求延长了我的职位:

package testy.sprung;//import declarations ommitedimport testy.sprung.beany.AwiredBean;@Controllerpublic class HelloWorldController {    private Logger log = Logger.getLogger("springTestLogger");    @Autowired    private AwiredBean oz;    @RequestMapping("/sprung")    public ModelAndView base() {        log.debug("base URI");        ModelAndView mv = new ModelAndView();        mv.setViewName("firstPage");        return mv;    }    @RequestMapping(value="/{articel}/{subTitle}",method=RequestMethod.GET)    public ModelAndView szia(@PathVariable("articel") String articel, @PathVariable("subTitle") String st, @RequestParam(value="co", required=false) String co) {        log.debug("Path GET/{articel}/{subtitle}: " + articel + "/" + st + "?co=" + co);        ModelAndView mv = new ModelAndView();        mv.setViewName("index"); // now put index.jsp in /WEB-INF/files        mv.addObject("articel", articel);        mv.addObject("subtl", st);        mv.addObject("co", co);        mv.addObject("awir", oz); //but it is null        return mv;    }}

bean 实现任何空接口(interface):

package testy.sprung.beany;public class AwiredBeanImpl implements AwiredBean {    @Override    public String toString() {        return "CommonAutowired";    }    public AwiredBeanImpl() {        throw new Error("E");    }}

我在测试中运行它。测试失败,因为我的错误或 NullPointerException 被抛出:

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("file:WebContent/WEB-INF/sprung-servlet.xml")public class ZakladniExtendedTest extends TestCase {    private MockHttpServletRequest request;    private MockHttpServletResponse response;    private HelloWorldController controller;    @Inject    private ApplicationContext context;    public ZakladniExtendedTest() {        PropertyConfigurator.configure("t-resources/log4j.properties");    }    @Before //this method is called before each test    public void setUp() {        request = new MockHttpServletRequest();        response = new MockHttpServletResponse();        controller = new HelloWorldController();    }    @Test    public void testThemeResolverExists() { //this test works        assertTrue(context.containsBean("themeResolver"));    }    @Test    public void autowiringTest() throws Exception { //but this not        request.setRequestURI("/title/subtitle");        request.setMethod("GET");        request.setParameter("co", "param");        ModelAndView mav = new AnnotationMethodHandlerAdapter().handle(request, response, controller);        String viewName = mav.getViewName();        Map objects = mav.getModel();        assertEquals("index", viewName);        //NullPointerException follows:        assertEquals("CommonAutowired", objects.get("awir").toString());    }}

最佳答案

而不是 controller = new HelloWorldController(); 您应该注入(inject)您的 Controller ,以便它成为 Spring 管理的 bean。

关于model-view-controller - Autowiring 的 bean 在 MVC Controller 中为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6569700/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com