tags:

views:

35

answers:

2

Even though I have specified the scope type as method, it gets instantiated in CONVERSATION scope.

> UserHome userHome = (UserHome) Component.getInstance(UserHome.class, ScopeType.METHOD);

This is quite confusing, can someone explain this behavior?

+1  A: 

When you call

Component.getInstance(UserHome.class, ScopeType.METHOD);

Seam internal behavior is to call

Object result = Contexts.lookupInStatefulContexts(name);

lookupInStatefulContexts API says

Search for a named attribute in all contexts, in the following order: method, event, page, conversation, session, business process, application.

As your ScopeType.METHOD does not contain your UserHome.class component, The search go on until get its scope (StypeType.CONVERSATION, right ?)

UPDATE

I was under the impression that if you specify the ScopeType to getInstance method you will be able to create the object within that scope

If the target component does not have the desired scope associated, getInstance method does not create the component within that scope. Instead it performs a hierarchical search by using Contexts.lookupInStatefulContexts till get some assigned scope

If you want more than one scope can be assigned to a component, you must scecify it by using @Role (@Roles) annotation

@Name("user")
@Scope(ScopeType.EVENT)
@Role(name="loggedUser", scope=ScopeType.SESSION)
public class User { ... }

So you specify the desired scope

Component.getInstance(User.class, ScopeType.EVENT);

or

Component.getInstance(User.class, ScopeType.SESSION);

remember Seam performs lookup by field/property name

private @In User user; // Take ScopeType.EVENT as scope

private @In User loggedUser; // Take ScopeType.SESSION as scope
Arthur Ronald F D Garcia
Correct. Providing the scope type to `Component.getInstance` tells Seam in which context it should start searching for an instance.
kraftan
This definitely seems to be an overkill, I was under the impression that if you specify the ScopeType to getInstance method you will be able to create the object within that scope and destroy it (if it's of Type METHOD).
Samuel
I added @PreConstruct and @PreDestroy to see the invocation behavior and for some weird behavior all my SEAM objects get created and destroyed on each invocation. I sincerely don't understand this weird behavior, since I wanted to control the scope in which the objects were created.
Samuel
@Samuel If you use the component once per request and you didn't start a conversation with `@Begin`, the component will be destroyed after the request.
kraftan
+1  A: 

I assume your UserHome class extends Seam's EntityHome class. The super class of EntityHome, which is Home, is in scope ScopeType.CONVERSATION:

@Scope(ScopeType.CONVERSATION)
public abstract class Home<T, E> extends MutableController<T>

Either you did not override the scope in your UserHome declaration or Seam ignores @Scope annotations in subclasses if one of the super classes already have an @Scope annotation.

kraftan
These are seam generated classes and I didn't override any of the scope variables for Home interfaces, since I thought it was necessary for it to work properly as for as edit / update is concerned.
Samuel