掲示板

Injecting session bean into request bean redeploy problems

9年前 に Davy Vanclee によって更新されました。

Injecting session bean into request bean redeploy problems

New Member 投稿: 1 参加年月日: 14/08/28 最新の投稿
I've run into a problem when trying to inject a session-scoped bean into a request-scoped bean. I've included a MWE at the end of this post and tried to pinpoint the exact situation in which the module stops working.

The first time I deploy the module (which is actually a Liferay portlet) with the injected session-scoped bean, everything works as expected. I can redeploy as much as I like, and it seems to keep on working until I actually let the container create the beans and session. From then on, every time I redeploy I get the following exception:

Caused by: java.lang.IllegalArgumentException: Can not set com.test.sessionscope.TestSessionBean field com.test.sessionscope.TestBean.bean to com.test.sessionscope.TestSessionBean

Injection and scoping is accomplished using Spring, and the used servlet container is Tomcat.

A minimal (not always) working example:

TestSessionBean

    @Named
    @Scope("session")
    public class TestSessionBean implements Serializable {
    
    }


TestBean

    @Named
    @Scope("request")
    public class TestBean implements Serializable {
    
    	@Inject private TestSessionBean bean;
    
    	private String name;
    
    	@PostConstruct
    	public void init() {
    		name = "session test";
    	}
    
    	public String getName() {
    		return name;
    	}
    }


XHTML

    <f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
    	<h:head>
    	</h:head>
    	<h:body>
    		#{testBean.name}
    	</h:body>
    </f:view>
thumbnail
9年前 に Olaf Kock によって更新されました。

RE: Injecting session bean into request bean redeploy problems

Liferay Legend 投稿: 6403 参加年月日: 08/09/23 最新の投稿
Also asked on stackoverflow. Please crossreference such locations yourself so that others can find answers in case they've already been given on the other platform
thumbnail
9年前 に Vernon Singleton によって更新されました。

RE: Injecting session bean into request bean redeploy problems

Expert 投稿: 315 参加年月日: 13/01/14 最新の投稿
Hi Davy,

Davy Vanclee:
I've run into a problem when trying to inject a session-scoped bean into a request-scoped bean.

Thanks for reporting this.

Could you give our jsf2-spring-portlet demo a try in your environment?

It does not inject a session scoped bean into a request scoped bean, but it does inject a custom "view" scoped bean into a request scoped bean. I took the liberty of changing this injection for the Backing and View bean to use @Scope("session") instead of @Scope("view"), and the portlet works fine. It appears that under the covers, spring is being a good JSF citizen when their SpringBeanFacesELResolver returns the name of the @Named Bean that spring is managing, and the ensuing bean creation is handled by the JSF impl and the Liferay Faces Bridge in the same way that the @SessionScoped annotation gets handled with proper portlet namespacing in the PortletSession.

So it appears that spring is not detecting that it is in a portlet, it is simply participating in the JSF EL Resolver chain.

When I deploy the jsf2-spring-portlet, and create such a session, and then redeploy the portlet, I am not getting the IllegalArgumentException you noted in your post ... it just works.

Hope that helps,
Vernon
9年前 に Davy Vanclee によって更新されました。

RE: Injecting session bean into request bean redeploy problems (回答)

New Member 投稿: 1 参加年月日: 14/11/05 最新の投稿
Hi Vernon,

I tried the jsf2-spring-portlet in our environment and it just worked, even after redeployments. To pinpoint the problem in my portlet, I began replacing bits and pieces from the demo-portlet with code from my portlet until it faulted. Apparently, the problem was due to the following property in the liferay-portlet.xml:
<private-session-attributes>false</private-session-attributes>


When I set this property to the default value (true), everything seemed to work fine, even after redeployments. This property was clearly a mistake and had no place in my portlet. However, I'm still not quite sure why in this specific case a session-scoped bean cannot be injected into another bean with a smaller scope...

Anyway, the problem is solved now, so thanks for the effort and quick response!
thumbnail
9年前 に Vernon Singleton によって更新されました。

RE: Injecting session bean into request bean redeploy problems

Expert 投稿: 315 参加年月日: 13/01/14 最新の投稿
Glad that is working for you now.

And thanks for reporting back your final findings, someone else will surely benefit!