So I'm trying to implement things mentioned in Spring's 3.1 blog post about From XML to #Configuration, but it doesn't want to work as supposed. Here is the web.xml (and that's the only xml) I'm using and the MvcFeatures and MvcBeans more or less are the same as in the blog just added few my beans.
<?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_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>dispatcher</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.example.config.MvcFeatures
com.example.config.MvcBeans
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file></welcome-file>
</welcome-file-list>
</web-app>
When trying to launch this thing up I get these messages in console:
21 Mar 2011 00:52:58,203 INFO org.springframework.web.context.support.AnnotationConfigWebApplicationContext []: No annotated classes found for specified class/package [com.example.config.MvcFeatures]
21 Mar 2011 00:52:58,203 INFO org.springframework.web.context.support.AnnotationConfigWebApplicationContext []: No annotated classes found for specified class/package [com.example.config.MvcBeans]
Any ideas what could be wrong? From what I understand i think it doesn't like the contextConfigLocation param values.
EDIT: Adding the MvcFeatures in case it helps..
#FeatureConfiguration
public class MvcFeatures {
/**
* Enables the Spring MVC #Controller programming model.
*/
#Feature
public MvcAnnotationDriven annotationDriven(ConversionService conversionService) {
return new MvcAnnotationDriven().conversionService(conversionService)
.argumentResolvers(new CustomArgumentResolver());
}
/**
* Maps '/' requests to the 'home' view.
*/
#Feature
public MvcViewControllers viewController() {
return new MvcViewControllers("/", "index");
}
/**
* Enables Spring's component scanning feature.
*/
#Feature
public ComponentScanSpec componentScan() {
return new ComponentScanSpec("com.example.controllers").excludeFilters(
new AnnotationTypeFilter(Configuration.class), new AnnotationTypeFilter(
FeatureConfiguration.class));
}
}
Try
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
com.example.config.**
</param-value>
</init-param>
Or set the delimiters accordantly to the docs:
Configuration locations must consist of one or more comma- or space-delimited
fully-qualified #Configuration classes. Fully-qualified packages may also
be specified for component-scanning
Richards, try these.
contextConfigLocation = com.example.config
mark MvcFeatures class with #Configuration and #EnableWebMvc
Related
im actually facing a problem related to memory leak using JSF2.2(Mojarra) and Tomcat7, i will proceed to the details:
The webpage contains many datatables, and have some options to select and export the data. Everything is stored in session (i readead all the good practices for JSF but in this case i been forced to use it)... unfortunately all the datatables are obligatory i know that this can consume too much memory, but i try to face it with session-timeout under 15 minutes. But even using it the memory grows to the max allowed of Tomcat7 and never release the memory back to the server, even after hours.
The webpage is actually working OK but with that big memory leak.
What im trying to achive is to release the memory of the sessions that already been timed out to the server again.
I have a little suspect that the problem is related with a WARNING that appears when the session is timed out and you try to do something in the app, on the logs print this:
de nov. 16, 2017 2:55:46 PM
org.springframework.web.context.request.FacesRequestAttributes
registerDestructionCallback WARNING: Could not register destruction
callback
[org.springframework.beans.factory.support.DisposableBeanAdapter#532ecd5b]
for attribute 'mainBean' because FacesRequestAttributes does not
support such callbacks
de nov. 16, 2017 2:55:46 PM
org.springframework.web.context.request.FacesRequestAttributes
registerDestructionCallback WARNING: Could not register destruction
callback
[org.springframework.beans.factory.support.DisposableBeanAdapter#5d5d8e9e]
for attribute 'otherBean' because FacesRequestAttributes does not
support such callbacks
I have a main bean that contains other beans with #Autowire all the beans are scoped in session #Scope=("session"):
mainBean.java
#Component
#Scope("session")
#Qualifier("mainBean")
public class MainBean {
#Autowired
private OtherBeanClass otherBean;
...
}
otherBean.java
#Component
#Scope("session")
#Qualifier("otherBean")
public class OtherBean {
...
}
web.xml of WEB-INF (I readed that i can just add the listener org.springframework.web.context.RequestContextListener but it results as ClassNotFoundException)
<?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_2_5.xsd"
version="2.5">
<display-name>MainApp v3.3.0</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>MyWebApp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MyWebApp</servlet-name>
<url-pattern>/messagebroker/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>/faces/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<context-param>
<description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<context-param>
<param-name>primefaces.THEME</param-name>
<param-value>main-css</param-value>
</context-param>
<context-param>
<param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
<param-value>resources.application</param-value>
</context-param>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<context-param>
<param-name>javax.faces.WEBAPP_RESOURCES_DIRECTORY</param-name>
<param-value>/WEB-INF/resources</param-value>
</context-param>
</web-app>
web.xml of Tomcat (Because it is a very big file i put only what i think is related to my problem):
<session-config>
<session-timeout>10</session-timeout>
</session-config>
If you want me to show something more to get a better approach just ask.
Thanks for reading and I hope you can help me.
EDIT: After the kolossus's helping tip
I managed to find the RequestContextListener inside my org.springframework.web-3.0.6.release.jar the correct declaration is:
(notice: .context.request.RequestContextListener and not .context.RequestContextListener)
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
Finally my #PreDestroy method is being called, but my server memory still on the max allowed.
Im new to spring with crystal reports I have created my application where I used some java annotation configuration. Now I am trying to integrate with crystal reports where I have to convert xml based web.xml to java based config
here is the web.xml code
<?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">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/appServlet/servlet-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/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>
<!-- crystal report in spring -->
<context-param>
<param-name>crystal_image_uri</param-name>
<param-value>/crystalreportviewers</param-value>
</context-param>
<context-param>
<param-name>crystal_image_use_relative</param-name>
<param-value>webapp</param-value>
</context-param>
<servlet>
<servlet-name>CrystalReportViewerHandler</servlet-name>
<servlet-class>com.crystaldecisions.report.web.viewer.CrystalReportViewerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CrystalReportViewerHandler</servlet-name>
<url-pattern>/CrystalReportViewerHandler</url-pattern>
<url-pattern>/faces/CrystalReportViewerHandler</url-pattern>
</servlet-mapping>
</web-app>
I have tried to mix it with java config but it fails,
Thanks in advance
To convert your basic web.xml to java configuration, either create a class by
implement WebApplicationInitializer or
extend AbstractAnnotationConfigDispatcherServletInitializer
regarding your crystal report you can create the Bean of type CrystalReportViewerServlet using #Bean Annotation.
for more information please check
- web.xml to java config
and register secondary servlet in spring
Thanks for to all who looked for a solution. I did the following
#Configuration
#ComponentScan(basePackages = { "t.g.app" })
public class ReportsConfig implements WebApplicationInitializer{
#Override
public void onStartup(ServletContext servletContext) throws ServletException{
servletContext.setInitParameter("crystal_image_uri","/crystalreportviewers");
servletContext.setInitParameter("crystal_image_use_relative", "webapp");
ServletRegistration.Dynamic crystalReportViewerHandler = servletContext
.addServlet("CrystalReportViewerHandler", new CrystalReportViewerServlet());
crystalReportViewerHandler.setLoadOnStartup(1);
crystalReportViewerHandler.addMapping("/CrystalReportViewerHandler");
}
}
There were also some security issues caused by spring security so i added some exceptions in security configuration.
My project was based completely on java-based configs. So instead of standart web.xml I had this:
public class MyWebAppInitializer implements WebApplicationInitializer {
#Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(WebAppConfiguration.class);
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
That work just fine, and I was able to get csrf token in Thymeleaf template like that:
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
But now I need to deploy my project to Google App Engine, and it requires to have web.xml otherwise it even doesn't start.
I added web.xml, and remove the java config above :
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.demshin.medpro.configuration.WebAppConfiguration</param-value>
</context-param>
<servlet>
<servlet-name>springServlet</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>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
And when I try to open my app's url, I get an exception:
org.thymeleaf.exceptions.TemplateProcessingException: Exception
evaluating SpringEL expression: "_csrf.token"
As I think, the problem is in corrupted filter chain.
But if I add this to web.xml:
<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>
instead of this java config:
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {
public SecurityWebApplicationInitializer() {
super(WebSecurityConfiguration.class);
}
}
, the problem is still there though the trace is a bit different.
So how this can be cured? Maybe there is a way to get rid of web.xml on Google App Engine?
Thank you.
What if you keep the java config and then create an empty web.xml e.g.
<?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">
</web-app>
I found some useful information here.
The problem is that WebApplicationInitializer requires Servlet 3.0, but Appengine supports only Servlet 2.5. So we have to use plain XML based config, at least for initialization. And configure Spring filter/servlet in web.xml manually.
Unfortunately I failed to make it work without completely moving spring configuration to xml, just adding spring-security.xml didn't work.
We can also vote for Servlet 3.0 support here, but it seems Google doesn't have any plans to add it, unfortunately.
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 am new to Spring and I know that is a newbie question, but I didn't find solution on web. I am developing application and I have to redirect all requests from base to index (for example mysite.com/ has to be redirected to mysite.com/index).
I am already finished this according to this:
I am created BaseController class and declared mapping for /.
Here is the code:
/**
* Index.
*
* #return the string
* #throws IOException
*/
#RequestMapping(method = RequestMethod.POST, value = "/")
public void index(HttpServletResponse response) throws IOException {
response.sendRedirect("/index");
}
It works well for request in format: mysite.com/appname/ and it redirects to mysite.com/index, but in that case I get 404 because it works with mysite.com/appname/index.
I know that this is related to setup, but I don't know what.
Here 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">
<!-- 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>
Note that, all is functional on mysite.com/appname/index but I need following format: mysite.com/index.
If you need some additional info, I will post it.
Can anybody help me, please? Thank you.
Maybe I'm misunderstanding your question, but you stated
Note that, all is functional on mysite.com/appname/index but I need
following format: mysite.com/index.
The /appname part is not a Spring configuration. It is a Servlet container configuration and is known as the context path.
Depending on your Servlet container, you can change the context path of each of your web applications. In this case, you would set it to /.
For Tomcat, you'll find how to change it here.