java - Can not assign ServletContext spring 3 -


i trying use servletcontext.getrealpath in util class load file resource (not part of unit testing) not work.

i tried both use "implements servletcontextaware":

@component public class utils implements servletcontextaware{     private servletcontext servletcontext;      @override     public void setservletcontext(servletcontext servletcontext) {         this.servletcontext = servletcontext;          system.out.println("**** "+servletcontext);     } } 

which throws npe since servletcontext not assigned spring.

and @autowired route:

@component public class utils{      @autowired     private servletcontext servletcontext; 

which throws nosuchbeandefinitionexception when tomcat being starting:

caused by: org.springframework.beans.factory.nosuchbeandefinitionexception: no qualifying bean of type [javax.servlet.servletcontext] found dependency: expected @ le ast 1 bean qualifies autowire candidate dependency. dependency annotations: {@org.springframework.beans.factory.annotation.autowired(required=true)} 

i adding initialization code in case doing wrong prevents spring inject right bean.

public class webappinitializer implements webapplicationinitializer {      private static logger log = loggerfactory.getlogger(webappinitializer.class);      @override     public void onstartup(servletcontext servletcontext) {         webapplicationcontext rootcontext = createrootcontext(servletcontext);          configurespringmvc(servletcontext, rootcontext);          filterregistration.dynamic corsfilter = servletcontext.addfilter("corsfilter", corsfilter.class);         corsfilter.addmappingforurlpatterns(null, false, "/*");  //        configurespringsecurity(servletcontext, rootcontext);     }      private webapplicationcontext createrootcontext(servletcontext servletcontext) {         annotationconfigwebapplicationcontext rootcontext = new annotationconfigwebapplicationcontext();  //        rootcontext.register(coreconfig.class, securityconfig.class);         rootcontext.register(coreconfig.class);         rootcontext.refresh();          servletcontext.addlistener(new contextloaderlistener(rootcontext));         servletcontext.setinitparameter("defaulthtmlescape", "true");          return rootcontext;     } 

coreconfig.class:

@configuration public class coreconfig {      @bean     public captionfixture createcaptionfixture() {         return new captionfixture();     }      @bean     public utils createutils () {         return new utils();     } } 

utils class servlet context.

i have looked @ suggested answers: here , here , didnt work.

the issue calling refresh() without servletcontext being registered, none available when beans initialized.

get rid of call

rootcontext.refresh(); 

the contextloaderlistener take care of calling refresh(). constructor javadoc explains happens when applicationcontext passed argument isn't refreshed.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -