Spring app doesn't find datasource in config files - spring

When I deploy my spring app (Spring 2.5.6) onto a Tomcat (6.0), start up fails with
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateSessionFactory' defined in class path resource [C.xml]: Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined
My web.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="[snip]" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>[snip]</display-name>
<welcome-file-list>
[snip]
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:All.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>[snip]</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>[snip]</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
All.xml imports a number of configuration files:
<import resource="A.xml"/>
<import resource="B.xml"/>
<import resource="C.xml"/>
...
B.xml defines a data source:
<jee:jndi-lookup id="dataSource" jndi-name="[snip]"/>
C.xml creates a hibernate session factory, referencing the data source from B.xml:
<bean id="hibernateSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>[snip]</value>
<value>[snip]</value>
</list>
</property>
<property name="hibernateProperties">
<props>
[snip]
</props>
</property>
<property name="dataSource" ref="dataSource" />
</bean>
When I deploy my WAR file, above exception occurs. My questions are:
Why?
Why does the same exception not occur in my JUnit test that I wrote to test the validity of the spring configuration? Here's the test:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(locations = { "/All.xml" })
public class ImfSpringConfigurationTest {
[snip]]
}
Note: In the JUnit test, I use a different All.xml file to replace B.xml with a file that defines a data source without jndi lookup, like so:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName = "oracle.jdbc.driver.OracleDriver"
[snip] />
**
Turns out, this configuration is correct. It was the deploy of another module operating on some of the same config files that threw the exception, not this deploy process.

Try pull in the top level context using a wildcard operator :
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/spring*.xml</param-value>
</context-param>

Related

SimpleUrlHandlerMapping not working for url with few extentions like dsm or ds

I am trying to implement SimpleUrlHandlerMapping in Spring. I am using Spring 4.2.5 version.
Following is my mapping
<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 class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/hello1.dsm">hc</prop>
</props>
</property>
</bean>
<bean id="hc" class="com.vaannila.HelloWorldController" >
<property name="message" value="Hello World!" />
</bean>
....
</beans>
When I run tomcat, I get the info message on console saying
org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/hello1.dsm] onto handler 'hc'
But When I hit the url "http://localhost:8080/SpringExample5/hello1.dsm" in my browser I get the requested resource is not available error i.e 404.
Later when I change key to "/hello1.htm", it worked fine with respective url. I am wondering is there any rule on url extention while mapping url to controller.
In web.xml, The dispatcher servlet url pattern is configured to .htm extention
<?xml version="1.0" encoding="UTF-8"?>
............
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>............
and hence It is working fine for ".htm" extention.
To make it work for other extentions we need to set corresponding url-pattern in web.xml file.

#Transaction at Service layer not working on DAO layer in spring

I am using #Transactional (org.springframework.transaction.annotation) at the service method from where I call the DAO layer to fetch data from DB.
However, I am getting following error
org.hibernate.HibernateException: No Session found for current thread
on
sessionFactory.getCurrentSession()
I have added package scanning to different files as suggested by other posts.Still unable to get the error.
Please help.Thanks a million in advance.
Following are the files I have used.
applicationContext.xml
<mvc:annotation-driven />
<context:annotation-config/>
<context:component-scan base-package="com.BASE_PCKG"></context:component-scan>
spring-servlet.xml
<mvc:annotation-driven />
<context:component-scan base-package="com.BASE_PCKG.SUB_PCKG.web"></context:component-scan>
Web.xml
...
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/applicationContext.xml</param-value>
</context-param>
....
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/spring-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Also I have configured the transaction manager in applicationContext file as :
<bean id="txManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" primary="true">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<tx:annotation-driven transaction-manager="txManager"/>
Add #Transactional on daoImpl class and Add transaction manager in configuration file:
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
Calling the method via proxy would help and fix the issue.
Calling the same method from same class would not help transaction work.

Spring ReloadableResourceBundleMessageSource configuration

I develop a simple Spring application which is my university task. There are 3 configuration files: web.xml, core-context.xml, dispatcher-servlet.xml and 1 file with default properties which is called messages.properties and is located in /WEB-INF/ folder.
In my application I have the following configuration of ReloadableResourceBundleMessageSource and it works fine:
core-context.xml
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames" value="/WEB-INF/messages" />
<property name="useCodeAsDefaultMessage" value="true" />
</bean>
web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:core-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
But it turned out that my task instruction says that I should configure ReloadableResourceBundleMessageSource bean in dispatcher-servlet.xml. The problem is that whenever I remove the above configuration from core-context.xml and put it in dispatcher-servlet.xml my locals are no longer displayed.
Could you explain to me why the problem occurs?
What is a difference between putting bean configuration inside core-context.xml and dispatcher-servlet.xml?
You have not posted your dispatcher-servlet.xml but I believe it is not being initialized by the spring container because it is not declared anywhere. If you must have another file called dispatcher-servlet.xml as you specify then you can just import it in your core-context.xml. This should solve your problem.

configure BasicDataSource as bean in web.xml

I'm trying to configure org.apache.commons.dbcp.BasicDataSource as bean in web.xml under a tomcat project using tomcat 6. (it's red5 with tomcat, we can ignore that the main server is actually red5 because i actually run jsp files under port 5080 and don't connect to the red5 directly using RTMP protocol)
my web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/ j2ee/web-app_2_4.xsd"
version="2.4">
<display-name>gamesisland-login-red5</display-name>
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/[myapp]</param-value>
</context-param>
<listener>
<listener-class>org.red5.logging.ContextLoggingListener</listener-class>
</listener>
<filter>
<filter-name>LoggerContextFilter</filter-name>
<filter-class>org.red5.logging.LoggerContextFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>LoggerContextFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>rtmpt</servlet-name>
<servlet-class>org.red5.server.net.rtmpt.RTMPTServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/fcs/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/open/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/close/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/send/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>rtmpt</servlet-name>
<url-pattern>/idle/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>Forbidden</web-resource-name>
<url-pattern>/streams/*</url-pattern>
</web-resource-collection>
<auth-constraint/>
</security-constraint>
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/red5-web.properties" />
</bean>
<bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${db.driver}</value></property>
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.username}</value></property>
<property name="password"><value>${db.password}</value></property>
<property name="poolPreparedStatements"><value>true</value></property>
<property name="maxActive"><value>10</value></property>
<property name="maxIdle"><value>10</value></property>
</bean>
</web-app>
my red5-web.properties:
webapp.contextPath=/myapp
webapp.virtualHosts=*
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://127.0.0.1:3306/dbname
db.username=user
db.password=pass
does tomcat automatically searches for WEB-INF/web.xml for configuration?
why don't I get any relevant errors to the creation of idDataSource ?
I really don't have any clue how to pinpoint or debug the problem.
any assistance would be greatly appreciated.
thank you!
kfir
I don't know nothing about Red5, but it seems like you trying to put Spring beans directly inside web.xml, which is wrong. You are suppose to create a separate Spring configuration file that will be picked up by Springs' ContextLoaderListener. First, add this to your web.xml:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
Than create applicationContext.xml file under /WEB-INF:
<?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-3.0.xsd">
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="/WEB-INF/red5-web.properties" />
</bean>
<bean id="idDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName"><value>${db.driver}</value></property>
<property name="url"><value>${db.url}</value></property>
<property name="username"><value>${db.username}</value></property>
<property name="password"><value>${db.password}</value></property>
<property name="poolPreparedStatements"><value>true</value></property>
<property name="maxActive"><value>10</value></property>
<property name="maxIdle"><value>10</value></property>
</bean>
</beans>
Of course all the <bean/> declarations should go away from web.xml.
Second thought: looking at this document it seems like Red5 uses a file named red5-web.xml, please go through this documentation carefully.

Tomcat unable to find jsp in war file

I recently switched one of my static html files to a Spring controller that uses a JSP to render its view. I use jetty to test locally and local testing shows the page rendering fine. Upon deploying to our test server, which uses Tomcat 6.0.26, I get the following exception:
javax.servlet.ServletException: Could not get RequestDispatcher for [/WEB-INF/jsp/index.jsp]: check that this file exists within your WAR
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:219)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have confirmed that the JSP exists in the war I deploy and the exploded directory that tomcat creates upon deployment. Here is what my web.xml and front-controller-servlet.xml files look like respectively (web.xml shorted slightly):
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Initialize the Spring DispatcherServlet -->
<servlet>
<servlet-name>gwtrpc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map the DispatcherServlet to only intercept RPC requests -->
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>*.gwtrpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/search.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>front-controller</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
front-controller-servlet.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:context="http://www.springframework.org/schema/context"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/search.html">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="com.company.search.web.server.controller.IndexController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
The difference between my dev instance and test instance is that the dev instance is deployed to the root (localhost/search.html) whereas the test instance is deployed to server.com/appname/search.html. Appname is the name of the *.war file I deploy. I have tried adding the full path as the prefix to the jps files (/appname/WEB-INF/jsp/) and a number of other combinations with no luck. I have verified that the jsp-api jars are in the tomcat lib directory. My tomcat install is the basic/default install.
The issue was caused by the default jsp servlet being removed from the default web.xml of the tomcat install. This had been done by a former co-worker and not to my knowledge. Replacing the test server's web.xml with the default web.xml (which included url-patterns/servlets for handling jsp files) corrected the issue.

Resources