Spring MVC Mybatis 使用JavaConfig 配置 Autowired注入失败的问题
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
上面代码看只有unitTest里进行了autowired,程序启动后哪个位置报空指针?