Announcement Announcement Module
Collapse
No announcement yet.
reference externalContext in applicationContext.xml Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • reference externalContext in applicationContext.xml

    Hi,

    How can I get a reference to the externalContext in my applicationContext.xml file? I would like to inject a directory path into my beans like such:
    <bean id="myBean" class="MyBean" scope="singleton">
    <property name="path" ref="#{externalContext.realPath('someDirectory')}"/>
    ...

    Which hopefully executes the externalContext.getRealPath("someDirectory") method and set it to myBean.path. Here's the stacktrace I'm getting if I run the code above:

    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSetService' defined in class path resource [config/applicationContext.xml]: Cannot resolve reference to bean '#{externalContext}' while setting bean property 'defaultFileSystemPath'; nested exception is org.springframework.beans.factory.BeanExpressionException: Expression parsing failed; nested exception is org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 0): Field or property 'externalContext' cannot be found on object of type 'org.springframework.beans.factory.config.BeanExpressionContext'
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    	at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1360)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1118)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
    	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)

    I'm using Spring 3.1 with MyFaces 2.1.5 on Tomcat 7.0.27. Will appreciate any help

  • #2
    That isn't going to work... externalcontext is only available for an incoming request not at startup of your application. Instead of the externalcontext you will have to work with the servletcontext to get the full path.

    Comment

    Working...
    X