Spring ReloadableResourceBundleMessageSource configuration - spring

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.

Related

Multiple CXF bundles in OSGI binding to same address

I have multiple WARs that expose CXF JAXRS endpoints. They have similar web.xml
<web-app id="app1">
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.osgi.web.context.support.OsgiBundleXmlWebApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:META-INF/spring/*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>app1</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>app1</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
They share common Spring configuration (named common-rest.xml)
<beans>
<import resource="classpath:META-INF/cxf/cxf.xml" />
<context:annotation-config />
<bean id="httpDestinationRegistry"
class="org.apache.cxf.transport.http.DestinationRegistryImpl" />
<bean id="baseJaxRSServer"
abstract="true"
lazy-init="false"
class="org.apache.cxf.jaxrs.spring.JAXRSServerFactoryBeanDefinitionParser.SpringJAXRSServerFactoryBean"
init-method="create"
p:address="${http.server}/" />
</beans>
And every bean has similar configuration
<beans>
<import resource="classpath:META-INF/app/common-rest.xml" />
<bean id="app1JaxRSServer"
parent="baseJaxRSServer"
p:serviceBeans-ref="app1ServiceBeans" />
</beans>
Exact path is defined in each bundle's MANIFEST
Web-ContextPath: app1
The problem is I can't make multiple bundles work together. With single bundle it's working OK, but if I try to run another one I get exception for creating app1JaxRSServer bean
org.apache.cxf.service.factory.ServiceConstructionException: There is an endpoint already running on /.
Using Karaf 4.0.9, CXF 3.1.13, Spring 3.2.18
The reason is
<url-pattern>/*</url-pattern>
If you have multiple bundles with this settings their endpoints will clash.
Try to use different prefixes per bundle.
I've found the solution, there was a problem with DestionationRegistry which was created by default by CXF HTTP Transport OSGI Bundle (the one that registered default /cxf/* endpoint). I had to disable this bundle by setting
org.apache.cxf.osgi.http.transport.disable = true
in Karaf properties

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 app doesn't find datasource in config files

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>

Spring, Need to use a a bean declared in a ApplicationContextFactory servlet, in a DispatcherServlet

i have a web.xml with these 2 servlet:
<servlet>
<servlet-name>ApplicationContextFactory</servlet-name>
<servlet-class>com.bamboo.common.factory.ApplicationContextFactory</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
AND
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
I need to use these bean declared on the ApplicationContextFactory:
<bean id="**catalogFacadeTarget**" class="com.bamboo.catW3.business.impl.CatalogFacadeImpl">
<property name="categoryDAO"><ref local="categoryDAOTarget"/></property>
<property name="containerDAO"><ref local="containerDAOTarget"/></property>
<property name="productDAO"><ref local="productDAOTarget"/></property>
<property name="productOptionDAO"><ref local="productOptionDAOTarget"/></property>
<property name="productStatusDAO"><ref local="productStatusDAOTarget"/></property>
<property name="userDAO"><ref local="userDAOTarget"/></property>
</bean>
in the dispatcher-servlet like this:
<bean name="welcome"
class="com.bamboo.catW3.business.impl.Welcome">
<property name="successView">
<value>welcome</value>
</property>
<property name="catalogFacadeImpl"><ref local="**categoryDAOTarget**"/> </property>
</bean>
Is it posible some how? Thank you!
You can't share contexts between servlets.
If you need to share beans, then you need to move the shared beans out of the ApplicationContextFactory servlet's context and into the root webapp context, using a ContextLoaderListener declared in web.xml. Both servlets will then be able to use the beans defined in that root context.
(I'd give you a link, but springsource.org seems be down at the moment).

Resources