我正在尝试使用@AnnotationDrivenConfig。目前,来自@component,@ service e.t.c注释类的@Autowired字段为空。 Maven似乎无法解析我的项目的@AnnotationDrivenConfig依赖项。通常会在春季xml中处理这个问题。如何使用Spring 4.1.5为Java配置设置解决此问题?
这是堆栈跟踪:
DEBUG: org.springframework.web.servlet.DispatcherServlet - Handler execution resulted in exception java.lang.NullPointerException
at com.reviewsystem.crazytenants.application.controllers.interceptors.SiteSwitcher.preHandle(MySwitcher.java:23)
at org.springframework.web.servlet.HandlerExecutionChain.applyPreHandle(HandlerExecutionChain.java:134)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
以下是代码:
MySwitcher.java
package x.y.z
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import x.y.z.MySwitcherHandler;
@Component
public class MySwitcher extends HandlerInterceptorAdapter {
public MySwitcher(){
super();
}
public boolean preHandle( HttpServletRequest request, HttpServletResponse response, Object handler ) throws Exception {
mySwitcherHandler.setup( request, response );
return true;
}
@Autowired
private MySwitcherHandler mySwitcherHandler;
}
MySwitcherHandler.java
package x.y.z
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.mobile.device.Device;
import org.springframework.mobile.device.site.SitePreference;
import org.springframework.stereotype.Service;
@Service
public class MySwitcherHandler {
public MySwitcherHandler(){}
public MySwitcherHandler( HttpServletRequest request, HttpServletResponse response ){
setup( request, response );
}
public void setup( HttpServletRequest request, HttpServletResponse response ){
System.out.println( "running MySiteSwitcherHandler.setup( HttpServletRequest request, HttpServletResponse response )" );
}
public void setup( SitePreference sitePreference, Device device ){
System.out.println( "running MySiteSwitcherHandler.setup( SitePreference sitePreference, Device device )" );
}
public void update( SitePreference sitePreference, Device device ){
System.out.println( "running MySiteSwitcherHandler.update( SitePreference sitePreference, Device device )" );
}
}
AppConfig.java
package x.y.z;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
//@AnnotationDrivenConfig
@ComponentScan( basePackages = {"x.y.z."} )
@Configuration
@EnableWebMvc
public class AppConfig extends WebMvcConfigurerAdapter {
@Override
public void configureDefaultServletHandling( DefaultServletHandlerConfigurer configurer ) {
configurer.enable();
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
@Override
public void addArgumentResolvers( List<HandlerMethodArgumentResolver> argumentResolvers ) {
argumentResolvers.add( new ServletWebArgumentResolverAdapter( new DeviceWebArgumentResolver() ));
argumentResolvers.add( new ServletWebArgumentResolverAdapter( new SitePreferenceWebArgumentResolver() ));
}
@Override
public void addInterceptors( InterceptorRegistry registry ) {
registry.addInterceptor( new DeviceResolverHandlerInterceptor() );
registry.addInterceptor( new SitePreferenceHandlerInterceptor() );
registry.addInterceptor( new MySwitcher() );
}
/* ... */
}
答案 0 :(得分:1)
对于要由Spring注入的对象,它必须是Spring bean。取代
@Override
public void addInterceptors( InterceptorRegistry registry ) {
registry.addInterceptor( new DeviceResolverHandlerInterceptor() );
registry.addInterceptor( new SitePreferenceHandlerInterceptor() );
registry.addInterceptor( new MySwitcher() );
}
通过
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(deviceResolverHandlerInterceptor());
registry.addInterceptor(sitePreferenceHandlerInterceptor());
registry.addInterceptor(mySwitcher());
}
@Bean
public DeviceResolverHandlerInterceptor deviceResolverHandlerInterceptor() {
return new DeviceResolverHandlerInterceptor();
}
@Bean
public SitePreferenceHandlerInterceptor sitePreferenceHandlerInterceptor() {
return new SitePreferenceHandlerInterceptor();
}
@Bean
public MySwitcher mySwitcher() {
return new MySwitcher();
}