Hibernate 5.x + Spring 5.x,无法自动装配DAOImpl类中的

我正在编写一个非常简单的crm Web应用程序,并且在我的DAO类之一中自动装配了休眠的sessionFactory bean时遇到了问题。我已经在互联网上搜索了几天,我很困惑,因为我的配置似乎反映了据说正在网络上工作的那些配置。在这个项目中,我不使用xml。


Web servlet配置类


public class WebServletConfig implements WebApplicationInitializer

{


@Override

public void onStartup(ServletContext servletContext) throws ServletException

{

    AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();

    webContext.register(SpringConfig.class);

    webContext.setServletContext(servletContext);

    ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(webContext));

    servlet.setLoadOnStartup(1);

    servlet.addMapping("/");

}

}

春天的配置类


@Configuration

@EnableWebMvc

@ComponentScan("com.crmproject")

public class SpringConfig implements WebMvcConfigurer 

{


@Bean

public ViewResolver viewResolver()

{

    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();

    viewResolver.setViewClass(JstlView.class);

    viewResolver.setPrefix("/WEB-INF/pages/");

    viewResolver.setSuffix(".jsp");


    return viewResolver;

}



守候你守候我
浏览 210回答 1
1回答

守着星空守着你

问题出在我的Controller类中。我试图使用“ new CustomerDAOImpl()。testMethod();”创建CustomerDAOImpl对象。春天显然不喜欢它。我将其更改为“ @Autowired CustomerDAOImpl customerDAOImpl;”。现在可以正常使用了,NullPointerException消失了。真是愚蠢的错误,但同时又有很好的学习经验。感谢帮助!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java