I decided to move into world of Java EE from SE. I analyzed where I should start and most appealing option was spring MVC. I am unable to move forward because mapping for page is just not found.
I already checked some solutions here on stackoverflow and also on other sites on internet. Everything looks like it should just work, but there are no expected results.
I am using Maven with this pom.xml configuration
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TDC</groupId>
<artifactId>webApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>webApp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I would like to define servlet to handle pages using web.xml configuration
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>webApp</display-name>
<servlet>
<servlet-name>IndexPage</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>IndexPage</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
IndexPage-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="tdc.web" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
I am starting application with class App.class
#SpringBootApplication
#EnableWebMvc
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
Also using controller IndexController.class
#Controller
#RequestMapping(value = "/")
public class IndexController {
#RequestMapping(method = RequestMethod.GET)
public String index(ModelMap model) {
model.addAttribute("message", "MVC index page");
return "index";
}
}
And i want make application entry point at index.jsp
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%# page import="java.time.LocalDateTime"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Just title</title>
</head>
<body>
<p>
Hello! Today is
<%=LocalDateTime.now()%></p>
<p>And now another messages ${message}</p>
</body>
</html>
My file structure is: https://imgur.com/a/WJg6ksA
My expectation was to see page, but instead of this I only get default 404 error: https://imgur.com/DS2zGAs
Also i noticed in log of spring application Page not found exception and also, when registering new servlet it should be also logged right ? There is only mention about default servlet.
Here is application log
INFO 16860 --- [ main] tdc.web.App : Starting App v0.0.1-SNAPSHOT on Cycan with PID 16860 (C:XXXX)
INFO 16860 --- [ main] tdc.web.App : No active profile set, falling back to default profiles: default
INFO 16860 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
INFO 16860 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
INFO 16860 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
INFO 16860 --- [ main] org.apache.jasper.servlet.TldScanner : 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.
INFO 16860 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
INFO 16860 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3752 ms
INFO 16860 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
INFO 16860 --- [ main] tdc.web.App : Started App in 4.843 seconds (JVM running for 5.509)
INFO 16860 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
INFO 16860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
INFO 16860 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 6 ms
WARN 16860 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping for GET /IndexPage
I am not sure what I missed. I will appreciate any answer :)
I usually never post questions on stackoverflow because I always found answers but in this case I spent a full week trying to get eclipse to debug my remote WAR file but impossible. My war project is just an Hello Word with only one file BackTest.java
package alxFinancial.dispatcher;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import javax.servlet.*;
import java.io.*;
/**
* Servlet implementation class BackTest
*/
#WebServlet("/BackTest")
public class BackTest extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* #see HttpServlet#HttpServlet()
*/
public BackTest() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter pw = response.getWriter();
pw.println("<html>");
pw.println("<head><title>Hello World</title></title>");
pw.println("<body>");
pw.println("<h1>Hello World</h1>");
pw.println("</body></html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException,IOException
{
doGet(request, response);
}
}
I created an ant build.xml file to compile the war file. I made sure to include the source,vars and lines and debug="true" in the debugger option.
<project name="orderManager" default="war_dispatcher" basedir=".">
<!-- set global properties for this build -->
<property name="build" location="build"/>
<property name="lib" location="build/lib"/>
<property name="jar_path"
location="/home/alex/Documents/Projects/Finance/Framework/Java/jars"/>
<property name="lib_alxFinancial" location="build/lib/alxFinancial"/>
<property name="lib_dispatcher" location="build/lib/dispatcher"/>
<property name="target" location="build/target"/>
<property name="webxml" location="ressources/dispatcher/webContent/WEB-
INF/web.xml"/>
<property name="web_directory" location="ressources/dispatcher/webContent"/>
<property name="src" location="src"/>
<property name="src_dispatcher" location="src/dispatcher"/>
<property name="src_alxFinancial" location="src/alxFinancial"/>
<property name="main-class" value="orderManager.Test" />
<property name="client-main-class" value="orderManager.Client" />
<property name="server-main-class" value="orderManager.OrderServer" />
<property name="quoteUpdate-main-class" value="quoteUpdate.Test" />
<property name="strategyManager-main-class"
value="strategyManager.StrategyManagerServer" />
<property name="ressources" value="ressources" />
<path id="dispatcher.classpath">
<fileset dir="/home/alex/Documents/Projects/Finance/Framework/Java/jars">
<include name="**/*.jar"/>
</fileset>
</path>
<target name="init">
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
<mkdir dir="${lib}"/>
<mkdir dir="${lib_alxFinancial}"/>
<mkdir dir="${lib_dispatcher}"/>
<mkdir dir="${target}"/>
</target>
<target name="build_dispatcher" depends="init" description="Compile
dispatcher source">
<javac srcdir="${src_dispatcher}" destdir="${lib_dispatcher}" debug="true"
includeantruntime="false" debuglevel="lines,vars,source" source="1.8"
target="1.8">
<classpath refid="dispatcher.classpath"/>
</javac>
</target>
<target name="war_dispatcher" depends="build_dispatcher">
<war warfile="${target}/Dispatcher.war" webxml="${webxml}">
<classes dir="${lib_dispatcher}" />
<fileset dir="${web_directory}">
<exclude name="WEB-INF/web.xml" />
</fileset>
</war>
</target>
<target name="copy_dispatcher" depends="war_dispatcher" description="Copy
dispatcher war to tomcat webapp directory">
<copy file="${target}/Dispatcher.war" tofile="${tomcat}/Dispatcher.war"
overwrite="true"/>
</target>
</project>
My ant script compile my class,create the war file and copy it into the tomcat/webapp/ folder.
I also configured my catalina.sh to be able to accept remote debugging as follow.
#
JPDA_OPTS="-
agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y"
#
and finally I set up a remote java application in eclipse with the port 8000 and the host localhost.When I run http://localhost:8080/Dispatcher/BackTest I can see my nice Hello word appearing but eclipse does not stop in the breakpoints I set in doGet.I also tried using jdb as a debugger, thinking it was an eclipse problem but I can't see the code:
alex#XXX:~/alx_git/orderManager$ jdb -attach localhost:8000 -sourcepath
src/dispatcher/
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
> stop in alxFinancial.dispatcher.BackTest.doGet
Set breakpoint alxFinancial.dispatcher.BackTest.doGet
>
Breakpoint hit: "thread=http-nio-8080-exec-5",
alxFinancial.dispatcher.BackTest.doGet(), line=23 bci=0
http-nio-8080-exec-5[1] next
>
Step completed: "thread=http-nio-8080-exec-5",
alxFinancial.dispatcher.BackTest.doGet(), line=24 bci=8
http-nio-8080-exec-5[1] next
>
Step completed: "thread=http-nio-8080-exec-5",
alxFinancial.dispatcher.BackTest.doGet(), line=25 bci=15
I give you guys more information on the version of ant,javac,java,eclipse and tomcat.
ant Apache Ant(TM) version 1.10.3 compiled on March 24 2018
javac javac 1.8.0_131
java java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
eclipse Version: Neon.3 Release (4.6.3)
Build id: 20170314-1500
Server version: Apache Tomcat/8.0.24
Server built: Jul 1 2015 20:19:55 UTC
Server number: 8.0.24.0
OS Name: Linux
OS Version: 4.15.0-34-generic
Architecture: amd64
JVM Version: 1.8.0_131-b11
JVM Vendor: Oracle Corporation
I am really sorry for asking this question but I am stuck.I really tried before but I can't get eclipse to debug my code.
I am getting closer now. If I create a java dynamic web project,include my BackTest.java class in it and Export War File to my tomcat server, it works,I can stop at my breakpoint. Whereas if I use new Java project with existing ant build file I can't get eclipse to debug it. May be some missing library in the WARÂ file ?
I hope you can help me.
Alex
I have this error when run junit test in spring mvc project.
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'createWebSocketContainer' defined in net.ebaolife.tpa.websocket.WebSocketConfig: Invocation of init method failed; nested exception is java.lang.IllegalStateException: A ServletContext is required to access the javax.websocket.server.ServerContainer instance
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1583)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:732)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:128)
at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:108)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 24 more
Caused by: java.lang.IllegalStateException: A ServletContext is required to access the javax.websocket.server.ServerContainer instance
at org.springframework.util.Assert.state(Assert.java:392)
at org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean.afterPropertiesSet(ServletServerContainerFactoryBean.java:102)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1642)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
... 39 more
I have add below jar. And spring version is 4.3.3.RELEASE
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-websocket</artifactId>
<version>8.0.28</version>
<scope>test</scope>
</dependency>
I found this bug in spring forum https://jira.spring.io/browse/SPR-14367.
But it only provided a workaround in spring boot, and it says it is fixed in spring 4.3.1 .
Test class is :
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration({ "classpath*:applicationContext*.xml" })
public class Test {
...
}
In test class add annotation #WebAppConfiguration, this error is gone.
I have a java spring mvc web application which use Spring 3.2 and Tomcat 8. I use quartz 2.2 to schedule a task. My controller class is:
#Controller
public class StatusController implements Job
{
#Autowired
WebContentDefinitionService webContentDefinitionService;
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
System.out.println("Starting Job");
try
{
webContentDefinitionService.deletePurgedContents();
webContentDefinitionService.moveContentsToPurged();
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Executed Job");
}
}
and I have a QuartzInitializer class as follows:
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebListener;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.ee.servlet.QuartzInitializerListener;
import org.quartz.impl.StdSchedulerFactory;
#WebListener
public class QuartzListener extends QuartzInitializerListener {
#Override
public void contextInitialized(ServletContextEvent sce) {
super.contextInitialized(sce);
ServletContext ctx = sce.getServletContext();
StdSchedulerFactory factory = (StdSchedulerFactory) ctx.getAttribute(QUARTZ_FACTORY_KEY);
try {
Scheduler scheduler = factory.getScheduler();
JobDetail jobDetail = JobBuilder.newJob(StatusController.class).build();
Trigger trigger = TriggerBuilder.newTrigger().withIdentity("simple").withSchedule(
CronScheduleBuilder.cronSchedule("0 0/5 * * * ?")).startNow().build();
scheduler.scheduleJob(jobDetail, trigger);
scheduler.start();
} catch (Exception e) {
ctx.log("There was an error scheduling the job.", e);
}
}
}
My porm.xml file is:
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>BasicQuartz</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>BasicQuartz</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Now everything works fine for me until I try to stop my tomcat server. I get error telling that there are multiple threads started by the quartz initializer, but unable to stop them and this may lead to memory leak.
I found lot of similar instances, but could not find a solution that can be applied in my case since I am not using my web.xml file to initialize the quartz scheduler. The error I get on my console is as follows:
Aug 14, 2015 6:25:43 PM org.apache.catalina.core.ApplicationContext log
INFO: Closing Spring root WebApplicationContext
Aug 14, 2015 6:25:43 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesJdbc
WARNING: The web application [smartwcm-admin] registered the JDBC driver [org.postgresql.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Aug 14, 2015 6:25:43 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [smartwcm-admin] appears to have started a thread named [DefaultQuartzScheduler_Worker-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:568)
Aug 14, 2015 6:25:43 PM org.apache.catalina.loader.WebappClassLoaderBase clearReferencesThreads
WARNING: The web application [smartwcm-admin] appears to have started a thread named [DefaultQuartzScheduler_Worker-2] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
java.lang.Object.wait(Native Method)
I had the same issue, which I solved with the following setting
SchedulerFactoryBean.setWaitForJobsToCompleteOnShutdown(true);
Have you tried to shut the Scheduler down, when stopping the application? Scheduler (like other threadpools) has a shutdown() and a shutdown(boolean waitForTasks) method.
I am doing with Spring #MVC3 and I've got a problem. Spring beans are created, but #Autowired doesn't work in a class. Here is my setting and source code:
xxx-servlet-.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.lodestone.ccah.controller"/>
<mvc:annotation-driven />
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
</beans>
beans.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws"
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
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:website.properties" />
</bean>
<bean id="contextApplicationContextProvider" class="com.lodestone.ccah.util.ApplicationContextProvider"></bean>
<bean id="staticVars" class="com.lodestone.ccah.util.StaticVars">
<property name="awsServerIp" value="${website.AWSServerIP}" />
<property name="timeOutConnection" value="${website.timeOutConnection}" />
<property name="timeOutReceive" value="${website.timeOutReceive}" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="${db.url}?useJDBCCompliantTimezoneShift=true" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
<property name="maxIdle" value="10" />
<property name="maxActive" value="100" />
<property name="maxWait" value="10000" />
<property name="validationQuery" value="select 1" />
<property name="testOnBorrow" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="1200000" />
<property name="minEvictableIdleTimeMillis" value="1800000" />
<property name="numTestsPerEvictionRun" value="5" />
<property name="defaultAutoCommit" value="true" />
</bean>
<!-- DAO settings -->
<bean id="mediaDao" class="com.lodestone.ccah.dao.MediaDao">
<!--<property name="dataSource" ref="dataSource"></property>-->
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="mediaService" class="com.lodestone.ccah.service.MediaService"></bean>
</beans>
Tomcat Log:
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization started
INFO : org.springframework.web.context.support.XmlWebApplicationContext - Refreshing Root WebApplicationContext: startup date [Thu Mar 08 13:21:49 EST 2012]; root of context hierarchy
INFO : org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [beans.xml]
DEBUG: org.springframework.beans.factory.xml.DefaultDocumentLoader - Using JAXP provider [org.apache.xerces.jaxp.DocumentBuilderFactoryImpl]
DEBUG: org.springframework.beans.factory.xml.PluggableSchemaResolver - Loading schema mappings from [META-INF/spring.schemas]
DEBUG: org.springframework.beans.factory.xml.PluggableSchemaResolver - Loaded schema mappings: {http://www.springframework.org/schema/tx/spring-tx-2.5.xsd=org/springframework/transaction/config/spring-tx-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org/springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://cxf.apache.org/schemas/configuration/http-conf.xsd=schemas/configuration/http-conf.xsd, http://www.w3.org/TR/xmldsig-core/xmldsig-core-schema.xsd=schemas/xmldsig-core-schema.xsd, http://www.springframework.org/schema/security/spring-security-3.0.3.xsd=org/springframework/security/config/spring-security-3.0.3.xsd, http://www.springframework.org/schema/security/spring-security-2.0.1.xsd=org/springframework/security/config/spring-security-2.0.1.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.w3.org/2006/07/ws-policy.xsd=schemas/ws-policy-200607.xsd, http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http://cxf.apache.org/schemas/jaxrs.xsd=schemas/jaxrs.xsd, http://cxf.apache.org/schemas/configuration/wsrm-manager.xsd=schemas/configuration/wsrm-manager.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://schemas.xmlsoap.org/ws/2004/08/addressing=schemas/wsdl/addressing.xsd, http://www.springframework.org/schema/security/spring-security-2.0.xsd=org/springframework/security/config/spring-security-2.0.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd=schemas/oasis-200401-wss-wssecurity-secext-1.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://schemas.xmlsoap.org/wsdl/=schemas/wsdl/wsdl.xsd, http://cxf.apache.org/schemas/bindings/object.xsd=schemas/bindings/object.xsd, http://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/flex/spring-flex-1.0.xsd=org/springframework/flex/config/xml/spring-flex-1.0.xsd, http://cxf.apache.org/schemas/configuration/jms.xsd=schemas/configuration/jms.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://www.springframework.org/schema/jdbc/spring-jdbc.xsd=org/springframework/jdbc/config/spring-jdbc-3.0.xsd, http://cxf.apache.org/schemas/configuration/http-jetty.xsd=schemas/configuration/http-jetty.xsd, http://schemas.xmlsoap.org/wsdl/http/=schemas/wsdl/http.xsd, http://cxf.apache.org/schemas/wsdl/jms.xsd=schemas/wsdl/jms.xsd, http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd=schemas/oasis-200401-wss-wssecurity-utility-1.0.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://schemas.xmlsoap.org/ws/2005/02/rm/wsrm-policy.xsd=schemas/configuration/wsrm-policy.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/security/spring-security-2.0.4.xsd=org/springframework/security/config/spring-security-2.0.4.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://schemas.xmlsoap.org/wsdl/2003-02-11.xsd=schemas/wsdl/wsdl.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://cxf.apache.org/schemas/ws/addressing.xsd=schemas/ws-addr-conf.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://cxf.apache.org/schemas/wsdl/http-conf.xsd=schemas/wsdl/http-conf.xsd, http://www.w3.org/2001/xml.xsd=schemas/xml.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms.xsd=org/springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http://www.w3.org/2007/02/ws-policy.xsd=schemas/ws-policy-200702.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/tx/spring-tx-3.0.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://cxf.apache.org/schemas/policy.xsd=schemas/policy.xsd, http://schemas.xmlsoap.org/ws/2004/09/policy/ws-policy.xsd=schemas/ws-policy-200409.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://cxf.apache.org/schemas/configuration/cxf-beans.xsd=schemas/configuration/cxf-beans.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://cxf.apache.org/schemas/core.xsd=schemas/core.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/jms/spring-jms-3.0.xsd=org/springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://cxf.apache.org/schemas/configuration/security.xsd=schemas/configuration/security.xsd, http://www.springframework.org/schema/tx/spring-tx.xsd=org/springframework/transaction/config/spring-tx-3.0.xsd, http://www.springframework.org/schema/security/spring-security-3.0.4.xsd=org/springframework/security/config/spring-security-3.0.4.xsd, http://cxf.apache.org/schemas/configuration/soap.xsd=schemas/configuration/soap.xsd, http://www.springframework.org/schema/security/spring-security-2.0.2.xsd=org/springframework/security/config/spring-security-2.0.2.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd, http://cxf.apache.org/schemas/jaxws.xsd=schemas/jaxws.xsd, http://cxf.apache.org/schemas/simple.xsd=schemas/simple.xsd, http://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-3.0.4.xsd, http://www.springframework.org/schema/tx/spring-tx-2.0.xsd=org/springframework/transaction/config/spring-tx-2.0.xsd, http://www.springframework.org/schema/security/spring-security-3.0.xsd=org/springframework/security/config/spring-security-3.0.xsd, http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd=org/directwebremoting/spring/spring-dwr-2.0.xsd, http://cxf.apache.org/schemas/configuration/wsrm-manager-types.xsd=schemas/configuration/wsrm-manager-types.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-3.0.xsd}
DEBUG: org.springframework.beans.factory.xml.PluggableSchemaResolver - Found XML schema [http://www.springframework.org/schema/beans/spring-beans-3.0.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-3.0.xsd
DEBUG: org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader - Loading bean definitions
DEBUG: org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loaded 7 bean definitions from location pattern [classpath:beans.xml]
DEBUG: org.springframework.web.context.support.XmlWebApplicationContext - Bean factory for Root WebApplicationContext: org.springframework.beans.factory.support.DefaultListableBeanFactory#70d9cc1a: defining beans [propertyConfigurer,contextApplicationContextProvider,staticVars,dataSource,mediaDao,jdbcTemplate,mediaService]; root of factory hierarchy
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'propertyConfigurer'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'propertyConfigurer'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'propertyConfigurer' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'propertyConfigurer'
INFO : org.springframework.beans.factory.config.PropertyPlaceholderConfigurer - Loading properties file from class path resource [website.properties]
DEBUG: org.springframework.web.context.support.XmlWebApplicationContext - Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMessageSource#b56efe]
DEBUG: org.springframework.web.context.support.XmlWebApplicationContext - Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicationEventMulticaster#789e60f]
INFO : org.springframework.beans.factory.support.DefaultListableBeanFactory - Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory#70d9cc1a: defining beans [propertyConfigurer,contextApplicationContextProvider,staticVars,dataSource,mediaDao,jdbcTemplate,mediaService]; root of factory hierarchy
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'propertyConfigurer'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'contextApplicationContextProvider'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'contextApplicationContextProvider'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'contextApplicationContextProvider' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'contextApplicationContextProvider'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'staticVars'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'staticVars'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'staticVars' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'staticVars'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'dataSource'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'dataSource'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'dataSource' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'dataSource'
**DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mediaDao'**
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'mediaDao'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'mediaDao' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'mediaDao'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'jdbcTemplate'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'jdbcTemplate'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'jdbcTemplate' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'dataSource'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking afterPropertiesSet() on bean with name 'jdbcTemplate'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'jdbcTemplate'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating shared instance of singleton bean 'mediaService'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Creating instance of bean 'mediaService'
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Eagerly caching bean 'mediaService' to allow for resolving potential circular references
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Finished creating instance of bean 'mediaService'
DEBUG: org.springframework.web.context.support.XmlWebApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor#29d03e78]
DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
DEBUG: org.springframework.web.context.ContextLoader - Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
INFO : org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 393 ms
Mar 8, 2012 1:21:49 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Mar 8, 2012 1:21:49 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Mar 8, 2012 1:21:49 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/16 config=null
Mar 8, 2012 1:21:49 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 3687 ms
The problem bean is mediaDao. But it is created in the Spring container. And here is the Java source the issue is happening.
NoteService.java:
public class MediaService {
#Autowired private MediaDao mediaDao;
private static final Logger logger = Logger.getLogger("NoteDao");
/**
* #param note
* #return
*/
public long addNote(Note note) {
logger.info(" MediaService.addNote starts");
long insertedId = -1;
try {
insertedId = mediaDao.add(note);
} catch (SQLException e) {
logger.error(e);
}
logger.info(" MediaService.addNote ends");
return insertedId;
}
}
The mediaDao is null but other Java sources with #Autowired are OK. I don't know what is missing?
My thought is, have you annotated the service with #Service annotation, if not then spring does not recognize that as spring managed class and it may not inject the DAO for you. Either it should Service or Component annotated. Also as told by Dave check the package whether the package is scanned by spring context or not. You have given the component scan for controller package. I am not sure whether the MediaService is also placed in the same package. Just check all these factors.
You're not scanning the package of the class, so the annotation will be ignored. Add its package to a component scan element as you're already doing for the controller package.
The following is the correct form.
#Autowired private MediaDaoInterface mediaDaoInt;