I am creating a webapp with Mule 3 and Spring mvc 3, the problem is I am not able to get mule beans in spring controllers. My application context load with mule context and is not available in spring context, when I load application context in spring context then mule and spring both have different reference to a single bean. How can I get mule context in spring mvc controller so that I can refer to the same bean object. For ref my web.xml is defined below:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="return-label-service" version="2.5">
<display-name>return</display-name>
<context-param>
<param-name>org.mule.config</param-name>
<param-value>mule-return-label-flow.xml,applicationContext.xml </param-value>
</context-param>
<context-param>
<param-name>mule.serverId</param-name>
<param-value>return-label-service</param-value>
</context-param>
<listener>
<listener-class>
org.mule.config.builders.MuleXmlBuilderContextListener
</listener-class>
</listener>
<!-- <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath://applicationContext-web.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> -->
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
This is a possible duplicate of this question. Moreover, I think there is good documentation available here and here
Related
I'm using Spring Boot on weblogic 10.3.6; however we usually use the embedded tomcat during development.
I'd like to enable a specific Spring profile when on weblogic to accomodate for different configuration/beans to be used when on wl. Since I'm forced to use a web.xml on that ancient version of the application server, I'd like to use web.xml to activate the weblogic profile I need.
This is my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>my.class.annotated.with.SpringBootApplication</param-value>
</context-param>
<listener>
<listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextAttribute</param-name>
<param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
So far I've seen suggestion to use
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>weblogic</param-value>
</context-param>
or
<init-param>
<param-name>spring.profiles.active</param-name>
<param-value>weblogic</param-value>
</init-param>
in the DispatcherServlet configuration.
I even tried configuring an ApplicationContextInitializer, but SpringBootContextLoaderListener provides his own initializer.
Is there a proper way to accomplish what I want? I can't put weblogic configuration in main application.yml because that would break the embedded tomcat configuration we are using for development.
Basically I have this jetty server running at my local. But I can't access my index.jsp file I see like that
this is my web.xml file, You see I use Apache CXF, and also use Spring, Hibernate and Jetty
<?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">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,classpath:Spring-Security.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring Security Start -->
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<!-- Projenin ana url'inden itibaren spring security aktif ediliyor -->
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- Spring Security End -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
How can I resolve it. Where I am doing wrong ?
This has been answered in a few places on StackOverflow. You simply need to set the dirAllowed parameter to false on the default servlet. This can be done either in the WEB-INF/web.xml of the servlet descriptor or by providing a modified etc/webdefault.xml file (via the deploy module in Jetty, for example) which is loaded before any of the contexts.
In either file this would look like:
<servlet>
<servlet-name>default</servlet-name>
<servlet-class>org.eclipse.jetty.servlet.DefaultServlet</servlet-class>
....
<init-param>
<param-name>dirAllowed</param-name>
<param-value>false</param-value>
</init-param>
....
</servlet>
As user Eng.Fouad points out this can also be defined as a context parameter:
<context-param>
<param-name>org.eclipse.jetty.servlet.Default.dirAllowed</param-name>
<param-value>false</param-value>
</context-param>
To publish one java-web application in tomcat, I copy the project that named 'demo-mvc' to the tomcat's webapps folder.Then I visit "http://localhost:8080/demo-mvc/xx.jsp" in the chorme browser after starting tomcat but it prompts "The requested resource is not available".I tried editing the server.xml as follows
<Context docBase="D:\apache-tomcat-7.0.57\webapps\demo-mvc" path="/demo-mvc" reloadable="true" source="org.eclipse.jst.jee.server:website"/>
Finally it still does no effect.I am confused about where the problem is.
Try to clean your project from Project clean option and make sure you are mapping all your resources
In your web.xml the code should be like this:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
when I am removing listener part from web.xml my project is running fine. Please let me know why?
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>migration</display-name>
<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>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
This is a spring web application in eclipse.
ContextLoaderListener is optional. If all you're beans are in the (child) web context (DispatcherServlet) you can remove it.
How it works basically, Spring load a root WebApplicationContext via Spring’s ContextLoaderListener and a child WebApplicationContext via Spring’s DispatcherServlet. This results in a parent-child context hierarchy.
If you don't need anything in the root hierarchie then just keep the DispatcherServlet.
Further reading :
Role/Purpose of ContextLoaderListener in Spring?
ContextLoaderListener or not?
and of course http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/
Only if you have two config xml files. Say, SerivesContext.xml, ContextDAO.xml
If you have configured everything in one spring config file you don't need the
ContextLoaderListener, just the dispatcher servlet is sufficient.
If you use different ones , you can configure it as ,
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml,WEB-INF/spring-security.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
In Spring Web Applications, there are two types of container, each of which is configured and initialized differently. One is the Application Context and the other is the WebApplicationContext.
Purpose of ContextLoaderListener :
ContextLoaderListener is used to initialize Application Context. In this case, you will tell Spring to load all *-.context.xml files from the classpath. There is only one application context per application.
So you can configure this in web.xml as
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
Purpose of contextConfigLocation :
On the other hand, you have WebApplicationContext. WebApplicationContext is the child context of the Application Context. Each DispatcherServlet defined in a Spring web application will have an associated WebApplicationContext. You can initialize the WebApplicationContext as
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:dispatcher-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
NOTE : Here name of the XML must be like <servlet name>-servlet.xml. In our case name of servlet is dispatcher therefore, name of XML file is dispatcher-servlet.xml.
I hope this clears you when to use what & why.
I have 2 spring configurations :
spring-servlet.xml
spring-security.xml
needed to add this line to security:
<beans:import resource="spring-servlet.xml"/>
Now hibernate is ran twice, this is log screenshot :
my web.xml:
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
</param-value>
</context-param>
remove <beans:import resource="spring-servlet.xml"/>,
and set
<param-value>
/WEB-INF/spring-security.xml;/WEB-INF/spring-servlet.xml
</param-value>
and maybe you twice define sessionFactory bean. Remove one of them.
EDIT:
Ok, two context is normal. One - application context, loaded by ContextLoaderListener, should contains definition of sessionFactory, dao, services, etc. usually its name is applicationContext.xml
DispatcherServlet should contains only beans for MVC. You can define conext name in parameter:
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-servlet.xml</param-value>
</init-param>