-
Mar 6th, 2010, 12:41 AM
#1
JDBCTemplates in a Java Web App
Hi all,
I'm completely new to Spring and trying to use JdbcTemplates in a simple java web application.(I'm not using Spring MVC or any other spring related libraries).
I could get my app work when I place my beans.xml inside the tomcat/bin. But I want to place my beans.xml file inside WEB-INF to be like, WEB-INF/beans.xml. However it is not clear for me about how to obtain my bean objects inside my test class. Kindly help me to find a resolution for this.
Thanks!
-
Mar 6th, 2010, 03:29 AM
#2
Hi,
You need to add the below entries in your web.xml.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
</listener>
By doing so the Spring Container will initialise all your beans present in your web.xml and will also inject in JDBC Template in reqd class if you have declared JDBC Template in your beans.xml
Kartik
-
Mar 9th, 2010, 09:30 AM
#3
JDBCTemplates in a Java Web App
kravicha,
Thanks for the reply.
I can add the listener to the web.xml but how do I obtain the bean from my class.
Lets say, I have a class with a getter and setter for a DataSource object and I have the bean information on my bean.xml and the listener statements on my web.xml. Now how can I get the DataSource object reference, coz when I try to use the getter method, it threw me an error saying, DataSource object is required.
I might be missing something here and thanks in advance for any comments / suggestions.
-
Mar 10th, 2010, 09:04 PM
#4
HI,
1)Actually you can rename your bean.xml as applicationContext.xml and place it in /WEB-INF/applicationContext.xml.No need of Listeners as mentioned above solution.
2)Then in your text test class where you want to access the bean,there you place the below code
ApplicationContext context =new XmlWebApplicationContext();
By doing so the XMLWebapplicationContext will serach for applicationContext.xml in your WEB-INF folder and initialise all the beans present in your applicationContext.
3) In your applicationContext,you will have something like
<bean name="xyz" class="XYZDaoSupport">
<property name="datasource" ref="datasource"/>
</bean>
<bean name="datasource" class="SomeThirdPartyDATASOURCE">
......
</bean>
Kartik
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules