Results 1 to 3 of 3

Thread: JSF2 and Spring 3.1.1. Spring sometimes seemingly not initialising?

  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Default JSF2 and Spring 3.1.1. Spring sometimes seemingly not initialising?

    Good evening all,

    I've been working on a JSF 2 app with Spring 3.1.1 for the last few weeks.

    Somewhere along the line, a strange problem started occurring when upon redeploy or Tomcat (6 or 7) restart, the JSF managed backing bean doesn't get its required Spring beans injected.

    The JSF beans are annotated (rather than using the faces-config.xml file). The JSF bean in question is the backing bean for the first page of the webapp.

    It is annotated as
    Code:
    @ManagedBean(name = "indexBackingBean")
    @SessionScoped
    .

    The Spring bean is defined in the applicationContext-beans.xml file. In my JSF bean, it's annotated as
    Code:
    @ManagedProperty(value="#{mainService}")
    Sometimes, when the web app starts up, everything is fine but sometimes, and even after repeated redeploys and Tomcat restarts, the managed property in my JSF backing bean is null and there is no sign of Spring in the resulting NullPointerException stacktrace.

    Below are relevant snippets of the config files (class names, package names and noise removed).

    In the web.xml file I have:
    Code:
    <?xml version="1.0"?> 
    <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"
    	   id="myproject" metadata-complete="false"
    	   version="2.5">
    <context-param>
    		<param-name>contextConfigLocation</param-name>
    		<param-value>
    			/WEB-INF/applicationContext-beans.xml
    			/WEB-INF/applicationContext-persistence.xml
    		</param-value>
    	</context-param>
    	
    	<listener>
    		<listener-class>
    			org.springframework.web.context.ContextLoaderListener
    		</listener-class>
    	</listener>
    	
    	<listener>
    		<listener-class>
    			org.springframework.web.context.request.RequestContextListener
    		</listener-class>
    	</listener>
    
            <context-param>
    		<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    		<param-value>server</param-value>
    	</context-param>
    
    	<context-param>
    		<param-name>javax.faces.CONFIG_FILES</param-name>
    		<param-value>/WEB-INF/faces-config.xml</param-value>
    	</context-param>   
        
    	<!-- Faces Servlet -->
    	<servlet>
    		<servlet-name>Faces Servlet</servlet-name>
    		<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    		<load-on-startup>1</load-on-startup>
    	</servlet>
    ...
    faces-config.xml
    Code:
    <?xml version="1.0"?>
    <faces-config 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-facesconfig_2_5.xsd"
    	version="2.5">
    
    	<application>
    		<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    	</application>
    </faces-config>
    applicationContext-beans.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <!--
      - Application context definition for TCL Wholesale EBill site on Hibernate.
      -->
    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    		xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
    		xmlns:tx="http://www.springframework.org/schema/tx"
    		xsi:schemaLocation="
    			http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    			http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    			http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    	<context:component-scan base-package="com.mypackage" />
    	<context:annotation-config />
    
            <bean id="mainService" class="com.mypackage.MainService" scope="singleton">
    	</bean>
    </beans>
    Any suggestions? It has started doing it on the Linux development server as well as the Windows PC I've been working on so it doesn't appear to be environmental.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    Could be a loading issue not sure why though, also hot redeploys can lead to errors as some static references are kept (which might be a reason for this issue). Another thing could be the mixing of different annotations, you have @ManagedBean which comes from the CDI packages and @ManagedProperty belongs to the older JSF annotations, mixing those sometimes causes issues (at least it did for us on Glassfish and Websphere ).

    I would enable debugging and see what is happening and see if the problem can be determined or that at least a common pattern can be established.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Jul 2012
    Posts
    2

    Default

    Thanks for the reply.

    I spent some time looking at the annotations used etc and couldn't see anything wrong with them.

    I've since discovered that deleting the Tomcat work directory fixes the problem (i.e. upon restarting Tomcat after clearing the directory, the app deploys and starts up correctly).

    If it shows up again even with this deployment step, I'll need to look at it again but for the moment, I'm chalking it up as work-around-able!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •