java - Spring DI NullPointerException @Autowired 失败

标签 java spring dependency-injection nullpointerexception vaadin

我叫 Daniel,是 Java 和 Web 应用程序的初学者。 我有一个问题,因为我遇到 NullPointerException 问题。

我学习Spring依赖注入(inject)。我犯了错误,但我找不到它。 谢谢您的帮助。

public class EmailServiceImpl implements MessageService {
    @Override
    public String getMessage(String messageName) {
        return "EMAIL_SERVICE";
    }
}

public class NotifyServiceImpl implements MessageService {
    @Override
    public String getMessage(String messageName) {
        return "NOTIFY_SERVICE";
    }
}

@Component
public interface MessageService {
    public String getMessage(String messageName);
}

@Configuration
public class DIConfiguration {

    @Bean
    public MessageService getMessageService(){
        return new NotifyServiceImpl();
    }
}


//vaadin init first class    
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(DIConfiguration.class);
        context.refresh();
        MessageService message = context.getBean(MessageService.class);

        Notification.show(message.getMessage("dfsa"));
//it works



        //context.close();
//different class 
//it doesn't work
@Autowired
    private MessageService messages;

Notification.show(messages.getMessage("first"));

//堆栈跟踪//

    /home/daniel/dev/apache-tomcat-8.0.3/bin/catalina.sh run
Using CATALINA_BASE:   /home/daniel/.IntelliJIdea13/system/tomcat/Unnamed_MyProject
Using CATALINA_HOME:   /home/daniel/dev/apache-tomcat-8.0.3
Using CATALINA_TMPDIR: /home/daniel/dev/apache-tomcat-8.0.3/temp
Using JRE_HOME:        /usr/local/java/jdk1.8.0_05
Using CLASSPATH:       /home/daniel/dev/apache-tomcat-8.0.3/bin/bootstrap.jar:/home/daniel/dev/apache-tomcat-8.0.3/bin/tomcat-juli.jar
[2014-10-21 04:05:04,571] Artifact MyProject:war exploded: Server is not connected. Deploy is not available.
21-Oct-2014 16:05:05.150 INFO [main] org.apache.catalina.core.AprLifecycleListener.init The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /home/daniel/dev/idea-IU-135.1289/bin::/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
21-Oct-2014 16:05:05.455 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
21-Oct-2014 16:05:05.472 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
21-Oct-2014 16:05:05.476 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["ajp-nio-8009"]
21-Oct-2014 16:05:05.478 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read
21-Oct-2014 16:05:05.478 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 632 ms
21-Oct-2014 16:05:05.505 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service Catalina
21-Oct-2014 16:05:05.505 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet Engine: Apache Tomcat/8.0.3
21-Oct-2014 16:05:05.515 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
21-Oct-2014 16:05:05.521 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["ajp-nio-8009"]
21-Oct-2014 16:05:05.522 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 43 ms
Connected to server
[2014-10-21 04:05:05,687] Artifact MyProject:war exploded: Artifact is being deployed, please wait...
21-Oct-2014 16:05:05.769 WARNING [RMI TCP Connection(2)-127.0.0.1] org.apache.catalina.startup.SetContextPropertiesRule.begin [SetContextPropertiesRule]{Context} Setting property 'antiJARLocking' to 'true' did not find a matching property.
paź 21, 2014 4:05:08 PM org.apache.catalina.core.ApplicationContext log
INFO: Spring WebApplicationInitializers detected on classpath: [org.springframework.boot.autoconfigure.jersey.JerseyAutoConfiguration@7846684d]
[2014-10-21 04:05:08,836] Artifact MyProject:war exploded: Artifact is deployed successfully
[2014-10-21 04:05:08,836] Artifact MyProject:war exploded: Deploy took 3 149 milliseconds
paź 21, 2014 4:05:09 PM com.vaadin.server.DefaultDeploymentConfiguration checkProductionMode
WARNING: 
=================================================================
Vaadin is running in DEBUG MODE.
Add productionMode=true to web.xml to disable debug features.
To show debug window, add ?debug to your application URL.
=================================================================
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework addAtmosphereHandler
INFO: Installed AtmosphereHandler com.vaadin.server.communication.PushHandler$1 mapped to context-path: /
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework addAtmosphereHandler
INFO: Installed the following AtmosphereInterceptor mapped to AtmosphereHandler com.vaadin.server.communication.PushHandler$1
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework doInitParams
WARNING: SessionSupport error. Make sure you define org.atmosphere.cpr.SessionSupport as a listener in web.xml instead
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework autoConfigureService
INFO: Atmosphere is using org.atmosphere.cpr.DefaultAnnotationProcessor for processing annotation
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.DefaultAnnotationProcessor configure
INFO: AnnotationProcessor class org.atmosphere.cpr.DefaultAnnotationProcessor$ServletContainerInitializerAnnotationProcessor being used
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.DefaultAnnotationProcessor fallbackToManualAnnotatedClasses
WARNING: Unable to detect annotations. Application may fail to deploy.
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework autoDetectWebSocketHandler
INFO: Auto detecting WebSocketHandler in /WEB-INF/classes/
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework initWebSocket
INFO: Installed WebSocketProtocol org.atmosphere.websocket.protocol.SimpleHttpProtocol 
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework configureAtmosphereInterceptor
INFO: Installing Default AtmosphereInterceptor
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.CorsInterceptor : CORS Interceptor Support
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.CacheHeadersInterceptor : Default Response's Headers Interceptor
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.PaddingAtmosphereInterceptor : Browser Padding Interceptor Support
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.AndroidAtmosphereInterceptor : Android Interceptor Support
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.HeartbeatInterceptor : Heartbeat Interceptor Support
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.SSEAtmosphereInterceptor : SSE Interceptor Support
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.JSONPAtmosphereInterceptor : JSONP Interceptor Support
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.JavaScriptProtocol : Atmosphere JavaScript Protocol
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.WebSocketMessageSuspendInterceptor : org.atmosphere.interceptor.WebSocketMessageSuspendInterceptor
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.OnDisconnectInterceptor : Browser disconnection detection
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework newAInterceptor
INFO:   org.atmosphere.interceptor.IdleResourceInterceptor : org.atmosphere.interceptor.IdleResourceInterceptor
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework configureAtmosphereInterceptor
INFO: Set org.atmosphere.cpr.AtmosphereInterceptor.disableDefaults to disable them.
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Using EndpointMapper class org.atmosphere.util.DefaultEndpointMapper
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Using BroadcasterCache: org.atmosphere.cache.UUIDBroadcasterCache
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Default Broadcaster Class: org.atmosphere.cpr.DefaultBroadcaster
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Broadcaster Polling Wait Time 100
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Shared ExecutorService supported: true
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Messaging Thread Pool Size: Unlimited
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Async I/O Thread Pool Size: 200
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Using BroadcasterFactory: org.atmosphere.cpr.DefaultBroadcasterFactory
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Using WebSocketProcessor: org.atmosphere.websocket.DefaultWebSocketProcessor
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: HttpSession supported: true
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Atmosphere is using DefaultAtmosphereObjectFactory for dependency injection and object creation
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Atmosphere is using async support: org.atmosphere.container.Tomcat7Servlet30SupportWithWebSocket running under container: Apache Tomcat/8.0.3 using javax.servlet/3.0
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework info
INFO: Atmosphere Framework 2.1.2.vaadin5 started.
paź 21, 2014 4:05:09 PM org.atmosphere.cpr.AtmosphereFramework interceptor
INFO: Installed AtmosphereInterceptor  Track Message Size Interceptor using | with priority BEFORE_DEFAULT 
16:05:10.740 [http-nio-8080-exec-6] DEBUG o.s.core.env.StandardEnvironment - Initializing new StandardEnvironment
16:05:10.746 [http-nio-8080-exec-6] DEBUG o.s.core.env.StandardEnvironment - Adding [systemProperties] PropertySource with lowest search precedence
16:05:10.748 [http-nio-8080-exec-6] DEBUG o.s.core.env.StandardEnvironment - Adding [systemEnvironment] PropertySource with lowest search precedence
16:05:10.748 [http-nio-8080-exec-6] DEBUG o.s.core.env.StandardEnvironment - Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]
16:05:10.776 [http-nio-8080-exec-6] DEBUG o.s.c.a.ClassPathBeanDefinitionScanner - JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
16:05:10.797 [http-nio-8080-exec-6] INFO  o.s.c.a.AnnotationConfigApplicationContext - Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@2a521c9d: startup date [Tue Oct 21 16:05:10 CEST 2014]; root of context hierarchy
16:05:10.798 [http-nio-8080-exec-6] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Bean factory for org.springframework.context.annotation.AnnotationConfigApplicationContext@2a521c9d: org.springframework.beans.factory.support.DefaultListableBeanFactory@167180b0: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,DIConfiguration]; root of factory hierarchy
16:05:10.812 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
16:05:10.813 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
16:05:10.834 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor' to allow for resolving potential circular references
16:05:10.837 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
16:05:10.873 [http-nio-8080-exec-6] DEBUG o.s.c.a.ConfigurationClassBeanDefinitionReader - Registering bean definition for @Bean method pl.infotower.spring.DIConfiguration.getMessageService()
16:05:10.982 [http-nio-8080-exec-6] DEBUG o.s.c.a.ConfigurationClassEnhancer - Successfully enhanced pl.infotower.spring.DIConfiguration; enhanced class name is: pl.infotower.spring.DIConfiguration$$EnhancerByCGLIB$$5075f3a1
16:05:10.982 [http-nio-8080-exec-6] DEBUG o.s.c.a.ConfigurationClassPostProcessor - Replacing bean definition 'DIConfiguration' existing class name 'pl.infotower.spring.DIConfiguration' with enhanced class name 'pl.infotower.spring.DIConfiguration$$EnhancerByCGLIB$$5075f3a1'
16:05:10.985 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
16:05:10.986 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
16:05:10.987 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor' to allow for resolving potential circular references
16:05:10.988 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
16:05:10.988 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
16:05:10.988 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
16:05:10.989 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor' to allow for resolving potential circular references
16:05:10.989 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
16:05:10.989 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
16:05:10.989 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor' to allow for resolving potential circular references
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor' to allow for resolving potential circular references
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
16:05:10.996 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
16:05:10.997 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
16:05:10.997 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor' to allow for resolving potential circular references
16:05:10.997 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
16:05:11.003 [http-nio-8080-exec-6] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource@603be86e]
16:05:11.007 [http-nio-8080-exec-6] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster@7c485f83]
16:05:11.009 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@167180b0: defining beans [org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,DIConfiguration,org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor,org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor,getMessageService]; root of factory hierarchy
16:05:11.010 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalConfigurationAnnotationProcessor'
16:05:11.010 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalAutowiredAnnotationProcessor'
16:05:11.010 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalRequiredAnnotationProcessor'
16:05:11.010 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.internalCommonAnnotationProcessor'
16:05:11.010 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'DIConfiguration'
16:05:11.010 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'DIConfiguration'
16:05:11.013 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'DIConfiguration' to allow for resolving potential circular references
16:05:11.054 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'DIConfiguration'
16:05:11.054 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importAwareProcessor'
16:05:11.054 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.context.annotation.ConfigurationClassPostProcessor.enhancedConfigurationProcessor'
16:05:11.055 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating shared instance of singleton bean 'getMessageService'
16:05:11.055 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Creating instance of bean 'getMessageService'
16:05:11.058 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'DIConfiguration'
16:05:11.095 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Eagerly caching bean 'getMessageService' to allow for resolving potential circular references
16:05:11.099 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Finished creating instance of bean 'getMessageService'
16:05:11.103 [http-nio-8080-exec-6] DEBUG o.s.c.a.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@56c62d60]
16:05:11.103 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
16:05:11.107 [http-nio-8080-exec-6] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemProperties]
16:05:11.107 [http-nio-8080-exec-6] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Searching for key 'spring.liveBeansView.mbeanDomain' in [systemEnvironment]
16:05:11.108 [http-nio-8080-exec-6] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source. Returning [null]
16:05:11.108 [http-nio-8080-exec-6] DEBUG o.s.b.f.s.DefaultListableBeanFactory - Returning cached instance of singleton bean 'getMessageService'
21-Oct-2014 16:05:15.516 INFO [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory /home/daniel/dev/apache-tomcat-8.0.3/webapps/manager
paź 21, 2014 4:05:16 PM com.vaadin.server.DefaultErrorHandler doDefault
SEVERE: 
java.lang.NullPointerException
    at pl.infotower.views.HelpView$1.buttonClick(HelpView.java:52)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.vaadin.event.ListenerMethod.receiveEvent(ListenerMethod.java:508)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:198)
    at com.vaadin.event.EventRouter.fireEvent(EventRouter.java:161)
    at com.vaadin.server.AbstractClientConnector.fireEvent(AbstractClientConnector.java:979)
    at com.vaadin.ui.Button.fireClick(Button.java:393)
    at com.vaadin.ui.Button$1.click(Button.java:57)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:168)
    at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:118)
    at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:287)
    at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:180)
    at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:93)
    at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:41)
    at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1402)
    at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:305)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:301)
    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:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:74)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1015)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:652)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1575)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1533)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

当我使用时

Notification.show(messages.getMessage("first")); 

抛出 NullPointerEx。

最佳答案

Vaadin 对象本身不受 Spring 容器控制。因此@Autowired HelpView 中使用的注释类(Class)不被尊重。要使示例正常工作,您需要进行一些集成工作。在互联网上的博客和教程中可以找到几种不同的方法。

一种无需过多更改示例代码即可工作的方法是使用@AspectJ。我将使用 Maven 作为管理工具来解释它。如果您以其他方式管理项目,请根据需要采取步骤。

要点:使用 @Configurable 注释需要 Autowiring /注入(inject)功能的 vaadin 类(或任何其他非 Spring 类)。 。不要忘记添加<context:spring-configured />应用程序上下文文件中的条目如下所示。因此,AspectJ 将完成所需的所有魔法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;

@Configured
public class HelpView extends VerticalLayout implements View {

    @Autowired
    private MessageService messages;

    ...

让 AspectJ 工作

AspectJ 本身需要一些配置。首先必须调用AspectJ 编译器。使用 Maven,您可以通过使用以下插件来实现这一点(将 xml 复制到 pom.xml 作为 <plugins /> 的子项):

        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>test-compile</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

其次,需要AspectJ运行时Spring的AOP资源。将以下内容复制到您的 Maven <dependencies />元素(在 pom.xml 中):

    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.6.11</version>
    </dependency>   
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>3.2.1.RELEASE</version>
    </dependency>

最后,您需要在applications context 文件(Bean.xml、root-context.xml、applicationContext.xml 或任何 bean 声明 xml 文件的名称)中添加一个配置条目。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:spring-configured />

版本 我选择的版本号在我的环境中可以很好地协同工作。也许人们必须根据他或她的环境来改变它们。

关于java - Spring DI NullPointerException @Autowired 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26481513/

相关文章:

java - 编码测试在 Ant 中失败但在 Eclipse 中有效

java - 编写需要 2 个列表的单元测试

spring - 在 SecurityContext 中找不到身份验证对象 - Spring 5

c++ - 具有许多类的程序中的依赖倒置 (C++)

java - 在 gwt 中提示用户退格键和浏览器后退按钮

java - &、|、^ 是按位运算符还是逻辑运算符?

c# - 将具有通用和具体实现的存储库注入(inject) Controller

android - 通过构建器注入(inject)的 Hilt 适配器为空

java - 在 RESTful API 和 WSO2 的背景下,Apache Tomcat 和 Apache Axis2 之间的关系是什么

spring integration leader 选举 jdbc 无法断开连接