2
votes

I am using spring 4, with javax annotations (JSR-330). In maven

     <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

So I use @Named, @Inject and @Resource from javax.annotation.* from javax.inject.* instead of Spring @Autowire, @Component , ... and spring supports them quite well. Good example at: http://www.mkyong.com/spring3/spring-3-and-jsr-330-inject-and-named-example/

My problem is that I can't have a bean in session scope with javax annotation so I used @Named and spring @Scope: org.springframework.context.annotation.Scope

Can I create a session scope bean by using javax annotations ?!

2
Please check out my answer. Hope it serves the purpose! - SyntaX

2 Answers

1
votes

As per this blog and the Official specification, JSR 330 does not support scopes like request or session out of the box. You will have to create your own annotations for this.

Let's look, how to do this for the request scope.

Create your own scope annotation:

@Scope
@Documented
@Retention(RUNTIME)
public @interface Request {
}

Extend Jsr330ScopeMetadataResolver to map your annotation on Spring's scope:

public class CustomScopeMetadataResolver extends Jsr330ScopeMetadataResolver {

  public Jsr330SpringScopeMetadataResolver() {
    registerScope(Request.class.getName(), WebApplicationContext.SCOPE_REQUEST);
  }

}

Use your custom resolver in your Spring configuration:

<context:component-scan base-package="my.package"
  scope-resolver="my.resolver.package.CustomScopeMetadataResolver" />

Reference Credit: Spring and JSR 330 scopes

0
votes

You must use the Annotation: @SessionScoped

Example:

@SessionScoped
public class DukesBday{
...
}