I'm building Java Web Project on Servlet 3.0, Spring 3, and some other stuff.
I'm trying to use Spring's #Sheduled task.
It is working fine, but when i undeploy war from Tomcat scheduled task continues running.How to stop it on Spring Container destroy?
UPDATE
I've found what was the root of my problem - i instantiated Spring context twice. First time in web.xml as
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring/servlet-context.xml
</param-value>
</context-param>
and second one in code like:
context = new ClassPathXmlApplicationContext(new String[]
{"/WEB-INF/spring/servlet- context.xml", "/WEB-INF/spring/beans.xml"});
So two instances of SheduledProcessor was created and ony one of them was destroyed. So spring destroys one it's aware about and second continues running making me to think that nothing was destroyed.
Removing ClassPathXmlApplicationContext solved my problem.
Controller.java
package a.b.c;
imports...
#Controller
#RequestMapping("/")
public class ReceiverController {
#RequestMapping(method = RequestMethod.GET)
public String welcome(ModelMap model) {
new ClassPathXmlApplicationContext("config.xml", ReceiverController.class);
return "index";
}
}
ScheduledProcessor.java
package a.b.c;
imports..
#Service
public class ScheduledProcessor {
private Logger logger = LoggerFactory.getLogger(ScheduledProcessor.class);
#Scheduled(fixedDelay = 30000)
public void process() {
logger.info("Processing task");
}
}
spring-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="a.b.c"/>
<task:annotation-driven/>
</beans>
Log file output :
03 Jul 2013 11:32:34,436 [ INFO ] (ScheduledProcessor.process:17)-> Processing task
11:33:04,439 [ INFO ] (ScheduledProcessor.process:17)-> Processing task
Jul 2013 11:33:06,913 [ INFO ] (XmlWebApplicationContext.doClose:1042)-> Closing
WebApplicationContext for namespace 'Test-servlet': startup date [Wed
Jul 03 11:32:21 EEST 2013]; root of context hierarchy 03 Jul 2013
11:33:06,914 [ INFO ]
(DefaultListableBeanFactory.destroySingletons:444)-> Destroying
singletons in
org.springframework.beans.factory.support.DefaultListableBeanFactory#1a56058:
defining beans
[scheduledProcessor,receiverController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor, etc..;
root of factory hierarchy
**03 Jul 2013 11:33:34,440 INFO -> Processing task
03 Jul 2013 12:34:0,440 INFO -> Processing task
Related
I start with Spring Web (following Spring documentation) and i have a little problem with the initialization
Every time i have the stack trace : "java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name."
Here are my files :
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<!-- Configure ContextLoaderListener to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<!-- Configuration locations must consist of one or more comma- or space-delimited
fully-qualified #Configuration classes. Fully-qualified packages may also be
specified for component-scanning -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.lacunasaurus.configuration.SpringApplicationConfiguration</param-value>
</context-param>
<!-- Bootstrap the root application context as usual using ContextLoaderListener -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Declare a Spring MVC DispatcherServlet as usual -->
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- Configure DispatcherServlet to use AnnotationConfigWebApplicationContext
instead of the default XmlWebApplicationContext -->
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<!-- Again, config locations must consist of one or more comma- or space-delimited
and fully-qualified #Configuration classes -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.lacunasaurus.configuration.SpringWebApplicationInitializer</param-value>
</init-param>
</servlet>
home.html
<!DOCTYPE html>
<html>
<head>
<title>Start Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<h1>Hello World!</h1>
Greeting : ${greeting}
</body>
</html>
ControllerHome
package com.lacunasaurus.web.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
#RequestMapping("/")
public class ControllerHome {
#RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("greeting", "Hello World from Spring 4 MVC");
return "welcome";
}
#RequestMapping(value = "/helloagain", method = RequestMethod.GET)
public String sayHelloAgain(ModelMap model) {
model.addAttribute("greeting", "Hello World Again, from Spring 4 MVC");
return "welcome";
}
}
Spring configuration
package com.lacunasaurus.configuration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
#Configuration
#ComponentScan(basePackages = {"com.lacunasaurus.service"})
public class SpringApplicationConfiguration {
}
Webinitializer
package com.lacunasaurus.configuration;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class SpringWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
#Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringApplicationConfiguration.class};
}
#Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
#Override
protected String[] getServletMappings() {
return new String[]{"/*"};
}
}
And my webconfig
package com.lacunasaurus.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
#Configuration
#EnableWebMvc
#ComponentScan(basePackages = "com.lacunasaurus.web.controllers")
public class WebConfig {
#Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/WEB-INF/views/");
viewResolver.setSuffix(".html");
return viewResolver;
}
}
I use maven and "4.3.4.RELEASE" spring version
My pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.lacunasaurus.vapologie</groupId>
<artifactId>test-web</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Vapologie Webapp</name>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.2.5.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>test</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.0</version>
<configuration>
<showWarnings>true</showWarnings>
</configuration>
</plugin>
</plugins>
</build>
</project>
and the full stack trace :
12-Dec-2016 23:16:35.543 SEVERE [localhost-startStop-1] org.apache.catalina.core.ContainerBase.addChildInternal ContainerBase.addChild: start:
org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/vapologie-web]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:725)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1780)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: Failed to register servlet with name 'dispatcher'.Check if there is another servlet registered under the same name.
at org.springframework.util.Assert.notNull(Assert.java:115)
at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.registerDispatcherServlet(AbstractDispatcherServletInitializer.java:98)
at org.springframework.web.servlet.support.AbstractDispatcherServletInitializer.onStartup(AbstractDispatcherServletInitializer.java:71)
at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:169)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5170)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 10 more
12-Dec-2016 23:16:35.544 SEVERE [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDescriptor Erreur lors du déploiement du descripteur de configuration C:\Users\Aurélien\AppData\Roaming\NetBeans\8.2\apache-tomcat-8.0.27.0_base\conf\Catalina\localhost\tes-web.xml
java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/vapologie-web]]
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:729)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:701)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:717)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:586)
at org.apache.catalina.startup.HostConfig$DeployDescriptor.run(HostConfig.java:1780)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Thank for your help :)
I finally find a way to initialize by java code :)
In my web.xml :
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>/</welcome-file>
</welcome-file-list>
</web-app>
And i change my WebConfig to extends the WebMvcConfigurerAdapter
public class VapologieWebConfig extends WebMvcConfigurerAdapter
But now i have warnings because spring can't redirect me to my view :(
[2016-12-13 03:38:10,170] Artifact test-web:war exploded: Artifact is being deployed, please wait...
13-Dec-2016 15:38:12.283 INFOS [RMI TCP Connection(3)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
13-Dec-2016 15:38:12.523 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization started
13-Dec-2016 15:38:12.672 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing Root WebApplicationContext: startup date [Tue Dec 13 15:38:12 CET 2016]; root of context hierarchy
13-Dec-2016 15:38:12.782 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class com.lacunasaurus.test.configuration.SpringApplicationConfiguration]
13-Dec-2016 15:38:13.213 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.<init> JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
13-Dec-2016 15:38:13.350 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.ContextLoader.initWebApplicationContext Root WebApplicationContext: initialization completed in 826 ms
13-Dec-2016 15:38:13.363 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization started
13-Dec-2016 15:38:13.371 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.prepareRefresh Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Dec 13 15:38:13 CET 2016]; parent: Root WebApplicationContext
13-Dec-2016 15:38:13.373 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.context.support.AnnotationConfigWebApplicationContext.loadBeanDefinitions Registering annotated classes: [class com.lacunasaurus.test.configuration.testWebConfig]
13-Dec-2016 15:38:13.568 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.<init> JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
13-Dec-2016 15:38:14.108 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.register Mapped "{[/],methods=[GET]}" onto public java.lang.String com.lacunasaurus.test.web.controllers.ControllerHome.sayHello(org.springframework.ui.ModelMap)
13-Dec-2016 15:38:14.110 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping.register Mapped "{[/helloagain],methods=[GET]}" onto public java.lang.String com.lacunasaurus.test.web.controllers.ControllerHome.sayHelloAgain(org.springframework.ui.ModelMap)
13-Dec-2016 15:38:14.167 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.initControllerAdviceCache Looking for #ControllerAdvice: WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Dec 13 15:38:13 CET 2016]; parent: Root WebApplicationContext
13-Dec-2016 15:38:14.436 INFOS [RMI TCP Connection(3)-127.0.0.1] org.springframework.web.servlet.DispatcherServlet.initServletBean FrameworkServlet 'dispatcher': initialization completed in 1072 ms
[2016-12-13 03:38:14,456] Artifact test-web:war exploded: Artifact is deployed successfully
[2016-12-13 03:38:14,456] Artifact test-web:war exploded: Deploy took 4 286 milliseconds
13-Dec-2016 15:38:14.865 AVERTISSEMENT [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/test/WEB-INF/views/home.html] in DispatcherServlet with name 'dispatcher'
13-Dec-2016 15:38:15.785 AVERTISSEMENT [http-nio-8080-exec-3] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/test/WEB-INF/views/home.html] in DispatcherServlet with name 'dispatcher'
13-Dec-2016 15:38:19.982 INFOS [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Déploiement du répertoire D:\Serveurs\apache-tomcat-8.5.9\webapps\manager de l'application web
13-Dec-2016 15:38:20.086 INFOS [localhost-startStop-1] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory D:\Serveurs\apache-tomcat-8.5.9\webapps\manager has finished in 103 ms
----------------- EDIT ------------------------
I found the solution !
In full java configuration, the web config class need to implement the servlet handling method to activate the configurer
#Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
Now I can show my home page :)
Thanks to everyone who help me to find the solution
After build my application with grade fat jar plugin as is configured bellow:
task fatJar(type: Jar) {
manifest {
attributes 'Implementation-Title': 'PACMAN',
'Implementation-Version': version,
'Main-Class': 'com.y.y.z.Mainclass'
}
baseName = project.name + '-all'
from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } }
with jar
}
And running this app (fat-jar with all dependencies) as follow:
-bash-3.2$ java -Dconf.properties=/app/home/wasa/sat/bin/app.properties -jar app-all-1.0.jar
Two thinks happens:
First is received the spring bootstrap start messages:
11/01/2016 11:38:59 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#11f23e5: startup date
[Mon Jan 11 11:38:59 BRST 2016]; root of context hierarchy
11/01/2016 11:39:00 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
And then after a while thi error/exception raises as described bellow:
11/01/2016 11:42:12 org.springframework.beans.factory.xml.XmlBeanDefinitionReader warning
Ignored XML validation warning
org.xml.sax.SAXParseException: schema_reference.4: Failed to read schema document
'http://www.springframework.org/schema/beans/spring-beans.xsd', because
1) could not find the document;
2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:195)
...
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:287)
at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:76)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadDocument(XmlBeanDefinitionReader.java:429)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:391)
I also have tried to download the spring-beans.xsd file definition and included it in the resources folder in my project without success.
I'm load the spring xml configuration files as showed bellow:
public static void main(String[] args) throws Exception {
AbstractApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
getAllActiveUsersInPeople(context);
}
My appicationContext.xml is defined as bellow:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
Someone has the similar problem or a possible solution to fix this issue?
Thanks
I'm trying to run my simple program, i added spring framework jar files and common-logging jar files too into my reference library.
Here is my first program says HelloWorld.java:
package com.rajendra.lesson01;
public class HelloWorld {
private String message;
public void setMessage(String message){
this.message=message;
}
public void getMessage(){
System.out.println("Messge: "+message);
}
}
Here is my another program says MainProgram.java:
package com.rajendra.lesson01;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainProgram {
public static void main(String[] args){
ApplicationContext ac=new ClassPathXmlApplicationContext("bean.xml");
HelloWorld hw=(HelloWorld) ac.getBean("helloWorld");
hw.getMessage();
}
}
and here is my last one bean.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloWorld" class="com.rajendra.lesson01.HelloWorld">
<property name="message" value="My name is foo.." />
</bean>
</beans>
Everything goes fine according to tutorial but my Output says:
Oct 20, 2014 9:12:19 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#1c12fb0: startup date [Mon Oct 20 09:12:19 IST 2014]; root of context hierarchy
Oct 20, 2014 9:12:19 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [bean.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [bean.xml]; nested exception is java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened because it does not exist
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:343)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:303)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:216)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:187)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:251)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:129)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:540)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:454)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.rajendra.lesson01.MainProgram.main(MainProgram.java:9)
Caused by: java.io.FileNotFoundException: class path resource [bean.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:329)
... 13 more
What's happening here please help? and also tell me how do you guys find the errors?
Help Would be appreciated!
http://www.tutorialspoint.com/spring/spring_hello_world_example.htm
In this example you have to put bean.xml out of the "com.rajendra.lesson01" package ... put it in resource package.
I hope this can help you.
I'm newly at Spring.
I tried to write easy project and stack with weird exception FileNotFoundException.
To my mind it is some problem with loading app-context.xml. But I sure that set correct absolute path.
Any suggestions?
Main code:
package com.prospring.ch2;
public class HelloWorldSpringDI {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
MessageRenderer mr = ctx.getBean("renderer", MessageRenderer.class);
mr.render();
}
}
Here content of app-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="provider" class="com.prospring.ch2.HelloWorldMessageProvider" />
<bean id="rendere" class="com.prospring.ch2.StandartOutMessageRenderer"
p:messageProvider-ref="provider" />
<!--<description>Example configuration to get you started.</description -->
<!-- context:component-scan base-package="com.prospring.ch2" /> -->
</beans>
snippet of console message:
14:24:06,360 INFO t.support.ClassPathXmlApplicationContext: 456 - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#3f62e847: startup date [Wed Nov 13 14:24:06 EET 2013]; root of context hierarchy
14:24:06,509 INFO eans.factory.xml.XmlBeanDefinitionReader: 315 - Loading XML bean definitions from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml]; nested exception is java.io.FileNotFoundException: class path resource [home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml] cannot be opened because it does not exist
I checked path to app-context.xml from terminal all looks ok:
nazar_art#nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ pwd
/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring
nazar_art#nazar-desktop:~/workspace/ch2/src/main/resources/META-INF/spring$ ls -lg
total 4
-rw-rw-r-- 1 nazar_art 867 Nov 13 14:25 app-context.xml
Here is looking of my struckture:
How to solve this trouble?
You are using the class path application context loader ClassPathXmlApplicationContext and passing an absolute path to it.
You need to use FileSystemXmlApplicationContext
If you want to use ClassPathXmlApplicationContext then you have to make sure your app-context.xml is in the class path of your application and pass the appropriate resource name, which is different from the absolute path on a filesystem.
Use either
public static void main(String[] args) {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"/home/nazar_art/workspace/ch2/src/main/resources/META-INF/spring/app-context.xml");
...
}
or
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"META-INF/spring/app-context.xml");
...
}
FileSystemXmlApplicationContext
ClassPathXmlApplicationContext
When starting the a servlet (from eclipse), I'm getting the following error:
It's tomcat 7 and the servlet is REST.
INFO: validateJarFile(C:\beezer\WebServerWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BeezerServer\WEB-INF\lib\geronimo-j2ee_1.4_spec-1.1.jar) - jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
448 [localhost-startStop-1] ERROR org.apache.wink.server.internal.servlet.RestServlet - com.test.AddressBookApplication<br />
java.lang.ClassNotFoundException: com.test.AddressBookApplication<br />
at
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1711)<br/>
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1556)<br/>
at java.lang.Class.forName0(Native Method)<br/>
at java.lang.Class.forName(Class.java:169)<br/>
at org.apache.wink.server.internal.servlet.RestServlet.getApplication(RestServlet.java:144)<br/>
at org.apache.wink.server.internal.servlet.RestServlet.createRequestProcessor(RestServlet.java:105)<br/>
at org.apache.wink.server.internal.servlet.RestServlet.init(RestServlet.java:81)<br/>
at javax.servlet.GenericServlet.init(GenericServlet.java:160)<br/>
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)<br/>
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)<br/>
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)<br/>
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)<br/>
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)<br/>
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)<br/>
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)<br/>
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)<br/>
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)<br/>
at java.util.concurrent.FutureTask.run(FutureTask.java:138)<br/>
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)<br/>
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)<br/>
at java.lang.Thread.run(Thread.java:662)<br/>
Aug 30, 2012 7:49:41 PM org.apache.catalina.core.ApplicationContext log<br/>
INFO: Marking servlet JAX-RS Servlet as unavailable<br/>
Aug 30, 2012 7:49:41 PM org.apache.catalina.core.StandardContext loadOnStartup<br/>
SEVERE: Servlet /BeezerServer threw load() exception<br/>
javax.servlet.UnavailableException: com.test.AddressBookApplication<br/>
at org.apache.wink.server.internal.servlet.RestServlet.init(RestServlet.java:91)<br/>
at javax.servlet.GenericServlet.init(GenericServlet.java:160)<br/>
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1266)<br/>
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1185)<br/>
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1080)<br/>
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5027)<br/>
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5314)<br/>
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)<br/>
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)<br/>
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)<br/>
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)<br/>
at java.util.concurrent.FutureTask.run(FutureTask.java:138)<br/>
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)<br/>
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)<br/>
at java.lang.Thread.run(Thread.java:662)<br/>
Aug 30, 2012 7:49:41 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
My web.XML:
<?xml version="1.0" encoding="UTF-8"?><br/> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee<br/> http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<display-name>BeezerServer</display-name>
<servlet>
<description>JAX-RS Tools Generated - Do not modify</description>
<servlet-name>JAX-RS Servlet</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.test.AddressBookApplication</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JAX-RS Servlet</servlet-name>
<url-pattern>/jaxrs/*</url-pattern>
</servlet-mapping>
Does anyone has a clew?
thanks!
First check that this class really exists. Check class name and package. Does it belong to the web project or dependent project?
If everything is OK, refresh project, then run Project/Clean, then clean and re-publish tomcat. If it does not work create server again (I mean in eclipse). If it still does not work go to C:\beezer\WebServerWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BeezerServer\WEB-INF\classes (the path is taken from your log) and check that the class file is there.
Good luck and welcome to the club of people that spend night fighting against the bugs in plugin of tomcat for eclipse...
Get rid of C:\beezer\WebServerWorkspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\BeezerServer\WEB-INF\lib\geronimo-j2ee_1.4_spec-1.1.jar It contains the class Servlet.java, which should be provided by the AS itself. If you are using Maven, mark the dependency as provided (<scope>provided</scope>)
Btw. the jar is still needed for compilation, but not in runtime.
EDIT: Oh, now I've noticed, it is only INFO in the log. So it is not the source of the problem, the root cause is the ClassNotFound ex. Make sure the class is on the classpath.