Spring MVC Mybatis 使用JavaConfig 配置 Autowired注入失败的问题
< 返回列表时间: 2017-10-14来源:开源中国
HDC调试需求开发(15万预算),能者速来!>>>
Junit测试成功,注入成功。
但是启动应用后,注入的实体报空指针异常。
请问这种问题是什么原因呢,谢谢。
三个配置类分别如下
一、WebInit 类 import com.lotus.api.RootConfig; import com.lotus.api.WebConfig; import org.springframework.web.filter.CharacterEncodingFilter; import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; import org.springframework.web.util.Log4jConfigListener; import javax.servlet.Filter; import javax.servlet.ServletContext; import javax.servlet.ServletRegistration; public class WebInit extends AbstractAnnotationConfigDispatcherServletInitializer { @Override protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; } @Override protected Class<?>[] getServletConfigClasses() { return new Class[]{WebConfig.class}; } @Override protected String[] getServletMappings() { return new String[]{"/"}; } @Override protected Filter[] getServletFilters() { //字符集拦截器 CharacterEncodingFilter characterEncodingFilter = new CharacterEncodingFilter(); characterEncodingFilter.setEncoding("UTF-8"); characterEncodingFilter.setForceEncoding(true); return new Filter[]{characterEncodingFilter}; } @Override protected void registerContextLoaderListener(ServletContext servletContext) { servletContext.setInitParameter("log4jConfigLocation" , "classpath:log4j.properties"); servletContext.addListener(Log4jConfigListener.class); } @Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { registration.setInitParameter("throwExceptionIfNoHandlerFound", "true"); } }
二、WebConfig package com.lotus.api; import com.lotus.api.interceptor.SecretInterceptor; import com.lotus.service.ServiceNativeConfig; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; @Configuration @EnableWebMvc @ComponentScan("com.lotus.api.web") public class WebConfig extends WebMvcConfigurerAdapter { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new SecretInterceptor()).addPathPatterns("/**"); } }
三、RootConfig package com.lotus.api; import com.lotus.service.ServiceNativeConfig; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.FilterType; import org.springframework.context.annotation.Import; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @Configuration @ComponentScan(basePackages = {"com.lotus.api"}, excludeFilters = {@ComponentScan.Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class)}) @Import(ServiceNativeConfig.class) public class RootConfig { }
使用Junit测试的代码如下: import com.lotus.api.RootConfig; import com.lotus.service.attachment.AttachmentService; import com.lotus.service.content.ContentService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {RootConfig.class}) public class WebTest { @Autowired private AttachmentService attachmentService; @Autowired private ContentService contentService; @Test public void test(){ attachmentService.add(); System.out.println("SECCESS"); System.out.println(contentService); }
热门排行