java - Yet Another ApplicationContext is Null -


i'm having trouble autowiring spring bean in application. i've done before in other applications success, reason i'm getting stumped. i've googled around, , have found many folks having same troubles, , warnings go along using getapplicationcontext() approach no answers helped me. understand warnings , still have case want way, maybe i'll working using aspectj , compile time weaving.

here setup:

applicationcontext.xml

<?xml version="1.0" encoding="utf-8"?>     <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"         xmlns:oxm="http://www.springframework.org/schema/oxm"         xmlns:util="http://www.springframework.org/schema/util"         xmlns:mvc="http://www.springframework.org/schema/mvc"         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/mvc          http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd         http://www.springframework.org/schema/context         http://www.springframework.org/schema/context/spring-context-3.0.xsd         http://www.springframework.org/schema/oxm         http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd         http://www.springframework.org/schema/util         http://www.springframework.org/schema/util/spring-util-3.0.xsd">          <context:property-placeholder location="classpath:widgetclient.properties"/>         <context:component-scan base-package="com.mycompany.widget.domain" />         <context:component-scan base-package="com.mycompany.widget.services" />         <context:annotation-config />           <bean id="messagesource" class="org.springframework.context.support.resourcebundlemessagesource">             <property name="basename" value="messages" />         </bean>          <bean id="heartbeat" class="com.company.widget.domain.checkwidgetserverheartbeat" destroy-method="destroy">             <constructor-arg type="java.lang.integer" name="interval" value="${heartbeat.interval}"/>             <constructor-arg type="java.lang.integer" name="timeout" value="${heartbeat.timeout}"/>             <constructor-arg type="java.lang.integer" name="unhealthythreshold" value="${unhealthy.threshold}"/>             <constructor-arg type="java.lang.integer" name="healthythreshold" value="${healthy.threshold}"/>             <constructor-arg type="java.lang.integer" name="backupverifytime" value="${backup.verify.time}"/>             <constructor-arg type="java.lang.string"  name="apiserverurl" value="${api.server.url}" />         </bean>     </beans> 

baseservice.java

package com.company.widget.services;  import com.company.widget.domain.checkwidgetserverheartbeat; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.applicationcontext; import org.springframework.stereotype.component;  @component public class baseservice{      @autowired     private applicationcontext appcontext;      public baseservice(){}      public checkwidgetserverheartbeat getheartbeat(){         return (checkwidgetserverheartbeat) appcontext.getbean("heartbeat");     } } 

heartbeatservice.java

package com.company.widget.services;  import org.springframework.stereotype.service;  import org.apache.log4j.logger;  @service public class heartbeatservice extends baseservice{      private static final logger logger = logger.getlogger(heartbeatservice.class);      public heartbeatservice(){}      public boolean ishealthy(){         return getheartbeat().check();     } } 

companywidgetclient.java

package com.company.widget.domain;  import javax.servlet.http.httpservletrequest; import java.io.*; import java.util.locale; import java.util.resourcebundle; import java.net.httpurlconnection; import java.net.inetaddress; import java.net.uri; import java.net.url; import java.net.urlconnection; import java.net.unknownhostexception; import java.util.*;  import com.company.widget.services.heartbeatservice; import org.apache.log4j.logger; import org.springframework.beans.factory.annotation.autowired; import org.springframework.context.applicationcontext; import org.springframework.stereotype.component;  @component public class companywidgetclient{    @autowired private heartbeatservice heartbeatservice;  public companywidgetclient(string settingsxmlfilepath, httpservletrequest request){     if(settingsxmlfilepath != null){                     configuration = new companywidgetconfiguration(settingsxmlfilepath, request);     }     if(request != null){         this.request = request;     }      //use api server url settings file if present     if (configuration.getproperty("api_server_url") != null) {         this.apiserverurl = configuration.getproperty("api_server_url");         log.debug("set api_server_url " + this.apiserverurl + " found in settings.xml");     } }   public companywidgetclient(companywidgetconfiguration configuration, httpservletrequest request){     if(configuration != null){         this.configuration=configuration;     }     if(request != null){        this.request = request;     }     //use api server url settings file if present     if (configuration.getproperty("api_server_url") != null) {        this.apiserverurl = configuration.getproperty("api_server_url");        log.debug("set api_server_url " + this.apiserverurl + " found in settings.xml");     } }  public companywidgetclient(){}      private companywidgetresponse requestwidget(){         if(heartbeatservice.ishealthy()){             stuff...         } 

index.jsp

<%@ page import="com.company.widget.domain.*" %> <%@ page contenttype="text/html;charset=utf-8" language="java" %> <%     companywidgetclient widgetclient = new companywidgetclient("/web-inf/settings.xml", request);     string companywidgetcomponent = widgetclient.createwidget(); %> ... html stuff  ...  <div id="widget">     <%=companywidgetcomponent%> </div>  ... 

the error:

500  org.apache.jasper.jasperexception: exception occurred processing jsp page /index.jsp @ line 5  2: <%@ page contenttype="text/html;charset=utf-8" language="java" %> 3: <% 4:     companywidgetclient widgetclient = new companywidgetclient("/web-inf/settings.xml", request); 5:     string companywidgetcomponent = widgetclient.createwidget(); 6: %> 7: <!doctype html public "-//w3c//dtd xhtml 1.0 strict//en" 8: "http://www.w3.org/tr/xhtml1/dtd/xhtml1-strict.dtd">  stacktrace:     org.apache.jasper.servlet.jspservletwrapper.handlejspexception(jspservletwrapper.java:568)     org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:470)     org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390)     org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334)     javax.servlet.http.httpservlet.service(httpservlet.java:722) root cause  java.lang.nullpointerexception     com.company.widget.domain.companywidgetclient.requestwidget(companywidgetclient.java:553)     com.company.widget.domain.companywidgetclient.createwidget(companywidgetclient.java:603)     org.apache.jsp.index_jsp._jspservice(index_jsp.java:67)     org.apache.jasper.runtime.httpjspbase.service(httpjspbase.java:70)     javax.servlet.http.httpservlet.service(httpservlet.java:722)     org.apache.jasper.servlet.jspservletwrapper.service(jspservletwrapper.java:432)     org.apache.jasper.servlet.jspservlet.servicejspfile(jspservlet.java:390)     org.apache.jasper.servlet.jspservlet.service(jspservlet.java:334)     javax.servlet.http.httpservlet.service(httpservlet.java:722) 

observations:

i know i'm missing required default constructor present on companyclient object, , logs builds companyclient object on deployment. guess i'm not clear on scope when comes spring application context. understanding singleton bean should available using application context irrespective of scope of calling bean. , appreciated!

when instantiating companywidgetclient directly:

new companywidgetclient(...) 

you not giving spring di container chance inject instance of heartbeatservice - null.

when getting instance of companywidgetclient - retrieve instance spring applicaiton context (via getbean() or similar) - heartbeatservice have been autowired.


Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -