PDA

View Full Version : Bean instantiated twice



Josué Nogueira
Nov 19th, 2009, 02:47 PM
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?:confused:

Regards,

Josué Nogueira

chudak
Nov 20th, 2009, 11:41 AM
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?:confused:

Regards,

Josué Nogueira

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

Josué Nogueira
Nov 20th, 2009, 11:58 AM
Hi chudak,

thanks for answer my question.


I´ve configurated my web.xml like the following:


<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/Spring.xml</param-value>
</context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListe ner</listener-class>
</listener>


and to get my beans I have the following 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.

chudak
Nov 20th, 2009, 12:42 PM
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.

Josué Nogueira
Nov 20th, 2009, 12:53 PM
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!

chudak
Nov 20th, 2009, 01:01 PM
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.

Josué Nogueira
Nov 20th, 2009, 01:11 PM
thanks chudak, I´ll revew my architecture!

Best Regards,


Josué Nogueira

Josué Nogueira
Nov 20th, 2009, 01:21 PM
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...

chudak
Nov 20th, 2009, 01:53 PM
Did you remove the static initializer that creates your bean factory outside of the web.xml?

Post your whole web.xml (use code tags)

Josué Nogueira
Nov 20th, 2009, 01:56 PM
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.



<?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.ContextLoaderListe ner</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

Mark Fisher
Nov 20th, 2009, 01:58 PM
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

Josué Nogueira
Nov 20th, 2009, 02:01 PM
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!

Mark Fisher
Nov 20th, 2009, 02:04 PM
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.

chudak
Nov 20th, 2009, 02:06 PM
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.