0

in seam The @Role annotation lets us define an additional named role for a component, with a different scope — it lets us bind the same component class to different context variables. (Any Seam component instance may be bound to multiple context variables, but this lets us do it at the class level, and take advantage of auto-instantiation.)

@Name("user")
@Entity
@Scope(CONVERSATION)
@Roles({@Role(name="currentUser", scope=SESSION),
        @Role(name="tempUser", scope=EVENT)})
public class User { 

    ... 

}

what about spring?

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
saeed
  • 61
  • 1
  • 4

2 Answers2

1

There is no out of the box way to do this in Spring.

You can wire the same bean type in multiple scopes using XML or JavaConfig, but not from within the bean class. And: I don't see the necessity of that either. If you are using the same bean class in different scopes you probably have an architecture problem.

One possible solution:

Define an abstract class which holds the data and two subclasses that contain the Spring annotations, one per scope:

public abstract class User{
   // fields, getters , setters
}

@Component @Scope("session")
public class SessionUser extends User{}

@Component @Scope("request")
public class TempUser extends User{}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
0

I used JavaConfig:

@Bean
@Scope(value = "request", proxyMode = ScopedProxyMode.TARGET_CLASS)
public EntityService requestScopedEntityService() {
    return new EntityService();
}

@Bean
@Scope(value = "prototype")
public EntityService prototypeScopedEntityService() {
    return new EntityService();
}
Sanjay
  • 8,755
  • 7
  • 46
  • 62