Announcement Announcement Module
Collapse
No announcement yet.
Bean instantiated twice Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Bean instantiated twice

    Hello forum,

    I have the following situation:

    I´ve integrated JSF and Spring in my application. So, when I debug my code, I can see that my managed beans ( I´ve put a resolver in faces-config.xml to indicate that Spring have to resolve the properties name ) are instantiated twice.

    Does anyone knows why that´s occurs?

    Regards,

    Josué Nogueira

  • #2
    Originally posted by Josué Nogueira View Post
    Hello forum,

    I have the following situation:

    I´ve integrated JSF and Spring in my application. So, when I debug my code, I can see that my managed beans ( I´ve put a resolver in faces-config.xml to indicate that Spring have to resolve the properties name ) are instantiated twice.

    Does anyone knows why that´s occurs?

    Regards,

    Josué Nogueira
    If they are instantiated twice your configuration is somehow loading the application context twice.

    Comment


    • #3
      Hi chudak,

      thanks for answer my question.


      I´ve configurated my web.xml like the following:
      Code:
      <context-param>  
         <param-name>contextConfigLocation</param-name>  
         <param-value>classpath:/Spring.xml</param-value>  
       </context-param> 
      
       <listener>
           <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
       </listener>
      and to get my beans I have the following code:

      Code:
       public static synchronized ApplicationContext getSpringContext() {
      		
      		if(springContext == null){
      			springContext = new ClassPathXmlApplicationContext("Spring.xml");
      		}
      		
      		
      		return springContext;
      	}
         public static synchronized Object getBean(String name){
      	   return getSpringContext().getBean(name);
         }
      Do you think that there is something wrong???

      Thanks in advance.

      Comment


      • #4
        You are loading the application context in your web.xml and then loading it again statically. Just like I said: you are loading your context twice.

        Comment


        • #5
          Originally posted by chudak View Post
          You are loading the application context in your web.xml and then loading it again statically. Just like I said: you are loading your context twice.
          How could I retrieve the Context previously loaded by the server?? ( the current context )

          Thank´s again!

          Comment


          • #6
            Why are you trying to access beans from the context? You're missing the whole point of dependency INJECTION. With a web application it is usually very easy to have spring wire up everything for you. This works with spring MVC and struts.

            The only time I needed to access beans directly from the context was in a swing application and in MDB's that used components that spring managed.
            Last edited by chudak; Nov 20th, 2009, 02:17 PM.

            Comment


            • #7
              thanks chudak, I´ll revew my architecture!

              Best Regards,


              Josué Nogueira

              Comment


              • #8
                I´ve changed a bean and now, my attribute is injected by the context file. But I still can see that my managed bean JSF is still instantiated twice.
                But the second instance of my managed bean is a proxy.

                Do you have any idea why this is happening? Cause I´ve removed the second instance is a proxy...

                Comment


                • #9
                  Did you remove the static initializer that creates your bean factory outside of the web.xml?

                  Post your whole web.xml (use code tags)

                  Comment


                  • #10
                    Originally posted by chudak View Post
                    Did you remove the static initializer that creates your bean factory outside of the web.xml?

                    Post your whole web.xml (use code tags)
                    This is my web.xml file.

                    Code:
                    <?xml version="1.0"?>
                    <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">
                     <description>XXXXXXXXXXXXXXX</description>
                     <display-name>XXX</display-name>
                     <context-param>   
                      <param-name>com.sun.faces.disableVersionTracking</param-name>   
                      <param-value>true</param-value>   
                    </context-param> 
                     <context-param>
                      <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
                      <param-value>.xhtml</param-value>
                     </context-param>
                     <context-param>
                      <param-name>facelets.REFRESH_PERIOD</param-name>
                      <param-value>2</param-value>
                     </context-param>
                     <context-param>
                      <param-name>facelets.DEVELOPMENT</param-name>
                      <param-value>true</param-value>
                     </context-param>
                     <context-param>
                      <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
                      <param-value>server</param-value>
                     </context-param>
                     <context-param>
                      <param-name>org.apache.myfaces.NUMBER_OF_VIEWS_IN_SESSION</param-name>
                      <param-value>40</param-value>
                     </context-param>
                     <context-param>
                      <param-name>com.sun.faces.validateXml</param-name>
                      <param-value>true</param-value>
                     </context-param>
                     <context-param>
                      <param-name>com.sun.faces.verifyObjects</param-name>
                      <param-value>true</param-value>
                     </context-param>
                     <context-param>
                      <param-name>org.richfaces.SKIN</param-name>
                      <param-value>#{skinMB.skinAtual}</param-value>
                     </context-param>
                     <context-param>
                      <param-name>org.richfaces.CONTROL_SKINNING</param-name>
                      <param-value>enable</param-value>
                     </context-param>
                     <context-param>  
                       <param-name>contextConfigLocation</param-name>  
                       <param-value>classpath:/Spring.xml</param-value>  
                     </context-param> 
                     <!--
                    		<filter> <filter-name>Filtro Login</filter-name>
                    		<filter-class>com.trigueirofontes.si.controle.filter.LoginFilter</filter-class>
                    		</filter>
                    	-->
                     <filter>
                      <display-name>RichFaces Filter</display-name>
                      <filter-name>richfaces</filter-name>
                      <filter-class>org.ajax4jsf.Filter</filter-class>
                     </filter>
                     <!--
                     		<filter-mapping> <filter-name>Filtro Login</filter-name>
                    		<url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher>
                    		<dispatcher>FORWARD</dispatcher> <dispatcher>INCLUDE</dispatcher>
                    
                    		</filter-mapping>
                    	-->
                     <filter-mapping>
                      <filter-name>richfaces</filter-name>
                      <servlet-name>Faces Servlet</servlet-name>
                      <dispatcher>REQUEST</dispatcher>
                      <dispatcher>FORWARD</dispatcher>
                      <dispatcher>INCLUDE</dispatcher>
                     </filter-mapping>
                    <!--  
                      <filter>
                       <filter-name>hibernateFilter</filter-name>
                       <filter-class>br.com.trigueirofontes.si.visao.HibernateFilter</filter-class>
                     </filter>
                    
                     <filter-mapping>
                        <filter-name>hibernateFilter</filter-name>
                        <url-pattern>*</url-pattern>
                     </filter-mapping>
                     --> 
                     <servlet>
                      <servlet-name>Faces Servlet</servlet-name>
                      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
                      <load-on-startup>1</load-on-startup>
                     </servlet>
                     <listener>
                         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                     </listener>
                     <servlet-mapping>
                      <servlet-name>Faces Servlet</servlet-name>
                      <url-pattern>*.jsf</url-pattern>
                     </servlet-mapping>
                     <session-config>
                      <session-timeout>30</session-timeout>
                     </session-config>
                     <welcome-file-list>
                      <welcome-file>index.jsp</welcome-file>
                     </welcome-file-list>
                     <error-page>
                      <exception-type>java.lang.SecurityException</exception-type>
                      <location>/paginas/login/login.jsf</location>
                     </error-page>
                     <!-- === Erros === -->
                     <error-page>
                      <error-code>303</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>400</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>401</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>403</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>404</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>500</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>501</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>502</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <error-code>503</error-code>
                      <location>/paginas/erro/erro.jsf</location>
                     </error-page>
                     <error-page>
                      <exception-type>javax.faces.application.ViewExpiredException</exception-type>
                      <location>/paginas/erro/erro.xhtml</location>
                     </error-page>
                     <login-config>
                      <auth-method>BASIC</auth-method>
                     </login-config>
                    </web-app>
                    Thank´s again

                    Comment


                    • #11
                      Hey, I don't want to interrupt this current thread, but next time please post web-related threads in the Web forum. This forum category is intended for the Spring Integration project.

                      Thanks,
                      Mark

                      Comment


                      • #12
                        Originally posted by Mark Fisher View Post
                        Hey, I don't want to interrupt this current thread, but next time please post web-related threads in the Web forum. This forum category is intended for the Spring Integration project.

                        Thanks,
                        Mark
                        Sorry Mark.

                        I´ve created this thread here cause the problem occured after the integration of the Spring and JSF.

                        Sorry for my mistake!

                        Comment


                        • #13
                          No problem. As you can probably imagine, it's a common mistake since "integration" sounds very generic. However, we have a project called "Spring Integration", and this forum category is intended for that.

                          Comment


                          • #14
                            Originally posted by Josué Nogueira View Post
                            Sorry Mark.

                            I´ve created this thread here cause the problem occured after the integration of the Spring and JSF.

                            Sorry for my mistake!
                            Spring Integration doesn't mean 'integrating' spring in your application. It means the Spring Integration Framework (EAI patterns):

                            http://www.springsource.org/spring-integration

                            Mark, you may want to just move this thread to the appropriate forum. It's clearly misplaced.

                            Comment

                            Working...
                            X