• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Accessing external Spring config file from Plain Servlet

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to access few Bean files which is outside my Ear file(or application) from a servlet. The front end is HTML. I have in my web.xml the following:

<param-name>contextConfigLocation</param-name>
<param-value>
jndi://java:comp/env/url/ExternalConfig
</param-value>
</context-param>

JNDI mapping is pointing to a spring file in my local machine. I need to get a bean in servlet and display its value. The bean is defined in Spring file as below:

<bean id="uploadLocation" class="java.lang.String" scope="prototype">
<constructor-arg type="java.lang.String">
<value>/opt/temp</value>
</constructor-arg>
</bean>

I need to get the value "/opt/temp" in servlet. Can you please tell me how this can be achieved in a simple servlet that extends HttpServlet.

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First a couple of things. You bean



can be easier written as



There is no need to make a simple String be scoped "prototype"

For you getting that bean in your Servlet.

There is ServletContextUtils.getRequiredApplicationContext(pass in ServletContext) this will return the ApplicationContext, where you can then call getBean on to get the uploadLocation value.

Thanks

Mark
 
pradeep chellappan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Mark,

I want to make sure that i get the Application context through the JNDI defined in Context param. This is critical for me , as in my app we have different versions of the similar file with minor changes for various test environements.

So, i want to know how this can be achieved in my servlet, just by passing servletcontext can i get JNDI value which inturns point to the External spring file?

Thanks for your time.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pradeep chellappan wrote:I need to access few Bean files which is outside my Ear file(or application) from a servlet. The front end is HTML. I have in my web.xml the following:

<param-name>contextConfigLocation</param-name>
<param-value>
jndi://java:comp/env/url/ExternalConfig
</param-value>
</context-param>

JNDI mapping is pointing to a spring file in my local machine. I need to get a bean in servlet and display its value. The bean is defined in Spring file as below:



I don't think spring supports resolving jndi:// as a protocol from to pick up the configuration file - the prefix it supports for resources is "classpath:" or "http:" or "file:"
Honestly JNDI looks like an over kill to me for what your trying to achieve ...


 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Mercs wrote:

pradeep chellappan wrote:I need to access few Bean files which is outside my Ear file(or application) from a servlet. The front end is HTML. I have in my web.xml the following:

<param-name>contextConfigLocation</param-name>
<param-value>
jndi://java:comp/env/url/ExternalConfig
</param-value>
</context-param>

JNDI mapping is pointing to a spring file in my local machine. I need to get a bean in servlet and display its value. The bean is defined in Spring file as below:



I don't think spring supports resolving jndi:// as a protocol from to pick up the configuration file - the prefix it supports for resources is "classpath:" or "http:" or "file:"
Honestly JNDI looks like an over kill to me for what your trying to achieve ...




Exactly. That is what I was trying to also say in my post.

If you have difficulty or a problem that seems difficult, it means that there is a simpler solution for your problem.

Mark
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic