Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how do i solve this error caused due to problem in spring initiation on tomcat

 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ear.context' defined in ServletContext resource [/WEB-INF/bean.xml]:
Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.context.support.ClassPathXmlApplicationContext]:
Constructor threw exception; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException:
IOException parsing XML document from class path resource [C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml]; nested exception is java.io.FileNotFoundException:
class path resource [C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:254)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:925)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:835)
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the exception is telling you that C:/Program Files/Apache Software Foundation/Tomcat 5.5/server/context.xml does not exist.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes..
i need to have context.xml file in classpath right...??? i.e. in the deployed war file...
how do i do that....??
This is the hierarchy: /WEB-INF/
beans.xml
context.xml
web.xml
The web and beans.xml file is being accessed...why is it then giving me exception in context.xml
This is my beans.xml file:
<bean id="ear.context" class="org.springframework.context.support.ClassPathXmlApplicationContext">
<constructor-arg index="0">
<list>
<value>/WEB-INF/context.xml</value>
</list>
</constructor-arg>
</bean>

It seems that constructor of ClassPathXmlApplicationContext cannot get context.xml file right..???
How do i solve this probelm??
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, put your context.xml in WEB-INF/classes and use classpath:context.xml in your bean definition.
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush Mangal wrote:Well, put your context.xml in WEB-INF/classes and use classpath:context.xml in your bean definition.


You can keep the file in the WEB-INF directory. Just use the prefix "http:" in where you load it.

so if it was in creation of ApplicationContext, you would have new ClassPathXmlApplicationContext("http:WEB-INF/context.xml")

as an example

Mark
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spritzler wrote:
Piyush Mangal wrote:Well, put your context.xml in WEB-INF/classes and use classpath:context.xml in your bean definition.


You can keep the file in the WEB-INF directory. Just use the prefix "http:" in where you load it.

so if it was in creation of ApplicationContext, you would have new ClassPathXmlApplicationContext("http:WEB-INF/context.xml")

as an example

Mark



Are you sure, ClassPathXmlApplicationContext("http:WEB-INF/context.xml") will work? I have never seen that before.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks!!!
It did work.

How do we call a function from a service.java file from bean.
I am done with constructor using <constructor-arg> tag and for setters <property> tags...
how a call a method?
For example:


in bean.xml file:
<bean id="login" class="loginService">
</bean>
This wont work....
How do we call a service method from bean and also hoe do we pass parameters to it?
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try MethodInvokingFactoryBean.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okk...
But i tried out the following code....its not working..
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you share what exception or error you are getting..
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The error clearly says that your loginDao does not have login(a,b,c); Could you please post your LoginDao (interface or class whatever is the case).
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
WOuldn't you just call getBean after the applicationContext has been created and then you call the method?

Or are you trying to figure out how you can hook into the lifecycle of an ApplicationContext and have a method run right after Spring has instantiated the object, like a bootstrap/init method.

With init method there are three ways

1) Annotate the method with @PostConstruct and add <context:annotation-config/> to your xml, remember you have to add the context namespace
2) In your xml bean definition add the attribute init-method="methodNameHere" to your <bean> tag
3) Have you class implement InitializingBean interface with the one method afterPropertiesSet().

Since you bean is already in xml I recommend #2. But the method cannot take any arguments, so your login_function does not match that. And I don't see any reason why you would want to call that method in the lifecycle callbacks. That looks to me like you want to call that method yourself in your code.

Mark
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sayaly,
Why do you want to invoke a method on a bean when application context is loaded?
Normally MethodInvokingFactoryBean is used to implement factory pattern.
As suggested by Mark, you should be calling loginService from your code somewhere.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Piyush and Mark...
M trying the same thing...
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My UI is andorid.
i am writing normal java files.
Say for example loginActivity:
to call servlet i use following code:



Now instead of calling Mainservlet i need to call the the service right???how do i do that??
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You have an android application from where you capture companyId, username and password. All these details are passed on to MainServlet through post method.

Till this point everything looks fine. My question is how are you calling loginService through MainServlet and how does the code that you posted earlier fit into all of these?



 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let me explain you my goal first.

I am done with UI part using android and server coding using servlet and other java files.
Each activity calls the MainServlet file wherein i map the user request ti appropriate service file.
In MAinServlet;s post method i check the type:

loginService file has all the business logic
loginDAO file has code to query the database
loginValue file has all getters and setters.
THis all is working fine....

Without disturbing the UI , i am just trying to convert the server side to one which fits in springwork.
How do i do that??

This is the hierarchy: /WEB-INF/
beans.xml
context.xml
web.xml
The web and beans.xml file is being accessed...why is it then giving me exception in context.xml??

This is my beans.xml file:


and in context.xml file i have declared all my beans say <bean id="login" class"loginService"></bean> <bean id="menu" class"menuService"></bean>

From android side how do i call the login bean so as it trigger loginService class and how to i handle argument passing issue?
Should i remove the MainServlet file completely?M completely stuck.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh..
defining contect.xml in WEB_INF/classes solved my issue of getting exception for class path.
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

As far as I understand , you have a client application which is on Android platform and a web application which this android application talks to.

So your android application has to go through HttpClient to connect to this web application. I would suggest to develop your web application using Spring MVC that will make

your job easier.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes..
My web app(WebProject under eclipse) follows the MVC achitecture.. (service class,DAO class and valueobject class)..
and my android app talks to web app using http post method successfully...

Then what i have done is..
I created a SpringProject under eclipse and added the same MVC architecture(service class,DAO class and valueobject class) to it along with bean.xml file..

How to i call a call a particular service.java file under spring project from android?

if its a standalone application then we can use:

 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not directly call service.java from android application.
I would recommend to go through http://static.springsource.org/docs/Spring-MVC-step-by-step/ link. Spring MVC is a web framework by spring source based on MVC architecture which will suit your
requirement.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Help me with foll error:



i have Mainservlet file in default package..
also mapped it in web.xml file and added <bean id="main" class="MainServlet"></bean> in beans.xml
Then why i get the above exception???
 
Piyush Mangal
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you have your MainServlet in your WEB-INF/classes folder?

i have Mainservlet file in default package..
also mapped it in web.xml file and added <bean id="main" class="MainServlet"></bean> in beans.xml
Then why i get the above exception???


I am a bit confused why would you have a bean definiton for MainServlet.
Servlets are loaded and instantiated by web container, you should not instantiate MainServlet class yourself.
 
Sayaly Kolhe
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh yes..
Mainservlet class i mapped in web.xml and removed it from bean file...
Got it working..

Thanks..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic