Spring individual context not inheriting root context - spring-mvc

I have two context xml resides in WEB-INF folder; applicationContext.xml and app-servlet.xml. I declare this in app-servlet.xml
<context:component-scan base-package="com.training.hibernate.controller"/>
and this in applicationContext.xml
<context:component-scan base-package="com.training.hibernate.services"/>
<context:component-scan base-package="com.training.hibernate.dao"/>
I got this error
BeanCreationException: Error creating bean with name 'personController': Injection of autowired dependencies failed;
If I move the component scanning of services and dao in app-servlet.xml, I got no error. I assume the app-servlet.xml is not inheriting the applicationContext.xml
This is 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>Spring Web Application</display-name>
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>app</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>

Here is some direction I can help you with to clarify different contexts. There are two main contexts usually in a Spring based web app :
Application Context : The context which contains the beans like services,dao etc.
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
And then there is dispatcher servlet's context. This contains web specific beans . This context has access to beans defined via ContextLoaderListener above.
<servlet>
<servlet-name>app</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:spring-config/app-servlet.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
Please verify, you have got this right.

Related

Servlet Mappings Jetty 8

I have a webapp that works with Tomcat 8 and Jetty 8. Recently, we have introduced servlet-mappings
<servlet>
<servlet-name>page</servlet-name>
<jsp-file>/Dashboard.html</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>page</servlet-name>
<url-pattern>/editjob</url-pattern>
</servlet-mapping>
When you put an url with "http://localhost:8080/app/editjob", in tomcat is redirected to "http://localhost:8080/app/Dashboard.html". But when we deploy it using embed Jetty, a 404 Not Found it is being returned. It seems that the servlet-mapping is not working.
HTTP ERROR 404
Problem accessing /app/page. Reason:
Not Found
Powered by Jetty://
I can enter in the app by using directly
http://localhost:8080/app/Dashboard.html. Without mappings, we have no problem with jetty
This is the code for embedding Jetty:
final Server server = new Server();
SocketConnector connector = new SocketConnector();
connector.setMaxIdleTime(-1);
connector.setSoLingerTime(-1);
connector.setPort(8080);
server.setConnectors(new Connector[]{connector});
final WebAppContext context = new WebAppContext();
context.setServer(server);
context.setContextPath("app");
context.setWar(location.toExternalForm());
server.setHandler(context);
context.start();
server.join();
This is the web.xml with the mappings.
<?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">
<display-name>app</display-name>
<servlet>
<servlet-name>ServletAdaptor</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ServletAdaptor</servlet-name>
<url-pattern>/webresources/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>login</servlet-name>
<jsp-file>/Login.html</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<servlet-name>page</servlet-name>
<jsp-file>/Dashboard.html</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>
org.springframework.web.context.ContextLoaderServlet
</servlet-class>
</servlet>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/beans.xml</param-value>
</context-param>
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<init-param>
<param-name>cors.allowGenericHttpRequests</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.allowSubdomains</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE, OPTIONS</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Origin, Content-Type, X-Requested-With, Access-Control-Allow-Origin</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>cors.maxAge</param-name>
<param-value>-1</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>page</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>page</servlet-name>
<url-pattern>/jobs</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Index.html</welcome-file>
</welcome-file-list>
</web-app>
I have tried to define the servlet-mappings when i start the server, but did not work.
This mappings are working perfectly with Tomcat, but Jetty...I dont know which could be the problem.
Thanks in advance.
Problem accessing /app/page.
That URL is not mapped anywhere in your pasted web.xml
The servlets with urls patterns (defined in <servlet-mapping> entries) are:
/webresources/*
/login
/home
/jobs
None of those will match on a request URL of /page, resulting in the DefaultServlet (which is default mapped at /) returning a 404 for that requested resource.
Also, you haven't setup JSP in your embedded-jetty environment, so no JSP is being used.
Copied from prior answer https://stackoverflow.com/a/28483349/775715
If that code snippet represents how you are initializing your webapp, then you are missing a lot of JSP initialization steps.
There's an example project produced by the Jetty Project showing how to use JSP with embedded Jetty at
https://github.com/jetty-project/embedded-jetty-jsp
Pay attention to ...
The dependencies you need
The required ClassLoader type
The scratchDir declaration
The ServletContainerInitializer setup
The Jsp Servlet Holder and mappings
The InstanceManager
(just to name a few big ones)

Context initialization failed when deploying Spring project on Tomcat

I am using JavaConfig for bootstrapping configuration, and it works well when I run local as Spring Boot App. But when I create a war file and try to deploy on a remote Tomcat7 server it complains about there is no web context loader:
ERROR o.s.web.context.ContextLoader - Context initialization failed
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/ < NONE> ]; nested exception is java.io.FileNotFoundException: Could not open ServletContext resource [/< NONE>]
Do I have to declare a web context when I am using the Spring Boot Servlet Initializer ?
This is my current web.xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>my service</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.myserviceclass.login.WebConfig
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

Quartz Scehduler 1.8.6 is executing twice in Spring 3.0

When my cron job runs the method is invoked twice.
Given below is the web.xml file.
This is my web.xml
<?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" 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>uinvoiceUI</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/dispatcher-servlet.xml,
/WEB-INF/spring-security.xml
</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/config/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error/404.jsp</location>
</error-page>
<error-page>
<error-code>403</error-code>
<location>/WEB-INF/jsp/error/access_denied.jsp</location>
</error-page>
<error-page>
<exception-type>javax.servlet.ServletException</exception-type>
<location>/WEB-INF/jsp/error/400.jsp</location>
</error-page>
<error-page>
<exception-type>java.io.IOException</exception-type>
<locenter code hereation>/WEB-INF/jsp/error/403.jsp</location>
</error-page>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
The method is invoked properly, and the functionality is also working.
The thing is the method is invoked twice within just fraction of seconds.
The quartz is configured in the beans.xml file and from there it calls a method, and the method is executed.
I have found on the other links that contextLoaderListener is invoking the method once and once the quartz is running.
But I cannot rule out the contextLoaderListener from the web.xml.
If I do so the application wont work.
I guess, that particular cron job is present (configured accidently) both in application context and servlet context. Resulting in duplicate invocation.

No bean named ' springSecurityFilterChain' is defined in spring security application

hi im creating a spring mvc application with spring security in it..but when i add security filters in web.xml it gives me no bean named exception(Maven project)
my web.xml
<?xml version="1.0" encoding="UTF-8"?>
<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"
id="scala-spring-hibernate" version="2.5">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-context-data.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-context-web.xml,
/WEB-INF/spring-context-data.xml,
/WEB-INF/spring-security.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<filter>
<filter-name>methodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>methodFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<!-- Spring Security -->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping> <filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern> </filter-mapping>
<welcome-file-list>
<welcome-file>login.html</welcome-file>
</welcome-file-list>
</web-app>
Mostly you are missing minimal configuration required to create springSecurityFilterChain bean.
In the file spring-security.xml, make sure <http> element is configured using Spring Security namespace.

Tomcat stops work when I access 2 pages in same tomcat

I know it could be bad question, but maybe someone had this before.
I create 2 pages with frameworks listed below and put them in same tomcat7 with different names. When I access both of them, Tomcat stops responding and I get this error:
Exception in thread "http-bio-80-exec-9" Exception in thread "http-bio-80-exec-1" Exception in thread "http-bio-80-exec-10" Exception in thread "http-bio-80-exec-13" Exception in thread "Timer-1" Exception in thread "Timer-0"
When I use only one of them, everything works as expected.
First time I tried deploy them, I got error about using same log or something. So I updated web.xml file with
<context-param>
<param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>
</context-param>
and websites "worked". Just I get this error.
Web.xml looks similar in both project:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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>Spring Security Tutorial</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jExposeWebAppRoot</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
/WEB-INF/applicationContext.xml
/WEB-INF/spring-quartz.xml
</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>dark-hive-custom</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<context-param>
<param-name>javax.faces.FACELETS_SKIP_COMMENTS</param-name>
<param-value>true</param-value>
</context-param>
<!-- PRIMEFACES -->
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>thresholdSize</param-name>
<param-value>104857600</param-value>
</init-param>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>D:/tmp/</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
<!-- JSF mapping -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<security-constraint>
<display-name>Restrict direct access to XHTML files</display-name>
<web-resource-collection>
<web-resource-name>XHTML files</web-resource-name>
<url-pattern>*.xhtml</url-pattern>
</web-resource-collection>
<auth-constraint />
</security-constraint>
<!-- Map these files with JSF -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/jsp/home.jsf</welcome-file>
</welcome-file-list>
<error-page>
<exception-type>org.springframework.security.access.AccessDeniedException</exception-type>
<location>/jsp/access/login.jsf</location>
</error-page>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
</web-app>

Resources