aspose file tools*
The moose likes Spring and the fly likes Accessing external  Spring config file from Plain Servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Accessing external  Spring config file from Plain Servlet" Watch "Accessing external  Spring config file from Plain Servlet" New topic
Author

Accessing external Spring config file from Plain Servlet

pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 29
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

Joined: Feb 05, 2001
Posts: 17259
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
pradeep chellappan
Greenhorn

Joined: Oct 20, 2005
Posts: 29
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

Joined: Feb 08, 2009
Posts: 606

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 ...



Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17259
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Accessing external Spring config file from Plain Servlet