File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Accessing external Spring config file from Plain Servlet

 
pradeep chellappan
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • 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.

 
Mark Spritzler
ranger
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Saifuddin Merchant
Ranch Hand
Posts: 607
Firefox Browser Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 17276
6
IntelliJ IDE Mac Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic