Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1106057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:43:57+00:00 2026-05-17T01:43:57+00:00

@Path(value = /user) @Stateless public class UserService { @Inject private UserManager manager; @Path(value =

  • 0
@Path(value = "/user")
@Stateless
public class UserService {

    @Inject
    private UserManager manager;

    @Path(value = "/create")
    @GET
    @Produces(value = MediaType.TEXT_PLAIN)
    public String doCreate(@QueryParam(value = "name") String name) {
        manager.createUser(name);

        return "OK";
    }
}

here is the user manager impl

public class UserManager {

    @PersistenceContext(unitName = "shop")
    private EntityManager em;

    public void createUser(String name) {
        User user = new User();
        user.setName(name);
        // skip some more initializations 
        em.persist(user);
    }
}

the problem is if i do not mark UserService as @Stateless then the manager field is null

but if i mark @Stateless, i can have the manager field injected, and the application works as i can get the data saved into db

just wondering, what is the reason behind this?

and is this the preferred way to wiring the application?

well, i am thinking to pull out the EntityManager to a producer, so that it can be shared

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:43:57+00:00Added an answer on May 17, 2026 at 1:43 am

    the problem is if I do not mark UserService as @Stateless then the manager field is null

    For injection to occur, the class has to be a managed component such as Enterprise Beans, Servlets, Filters, JSF managed beans, etc or CDI managed bean (this is the new part with Java EE 6, you can make any class a managed bean with CDI).

    So, if you don’t make your JAX-RS endpoint an EJB, how to enable injection? This is nicely explained in JAX-RS and CDI integration using Glassfish v3:

    There are two ways CDI managed beans
    are enabled:

    1. instantiated by CDI, life-cycle managed by Jersey. Annotate with
      @ManagedBean and optionally annotate
      with a Jersey scope annotation.

    2. instantiated and managed by CDI. Annotate with a CDI scope annotation,
      like @RequestScoped (no @ManagedBean
      is required)

    I also suggest checking the resources below.

    and is this the preferred way to wiring the application?

    I’d say yes. CDI is very nice and… don’t you like injection?

    well, I am thinking to pull out the EntityManager to a producer, so that it can be shared

    Shared between what? And why? In you case, you should use an EntityManager with a lifetime that is scoped to a single transaction (a transaction-scoped persistence context). In other words, don’t share it (and don’t worry about opening and closing it for each request, this is not an expensive operation).

    References

    • JPA 2.0 Specification
      • Section 7.6 “Container-managed Persistence Contexts”
      • Section 7.6.1 “Container-managed Transaction-scoped Persistence Context”
      • Section 7.6.2 “Container-managed Extended Persistence Context”

    Resources

    • Dependency Injection in Java EE 6 – Part 1
    • Introducing the Java EE 6 Platform: Part 1
    • TOTD #124: Using CDI + JPA with JAX-RS and JAX-WS
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know there is NSDesktopDirectory that will get me the path to current user
I am using dynamic parameter to get value from user when he force build.User
As the question states, I am trying to get a Path value, using JFileChooser.
I've been segmenting my full referral path to the user defined value using advanced
I have a folder path stored in the property nightly.basepath (here the value would
Is there some way to provide a default value to the url/path helpers? I
I would like to convert something like: <property name='aoeu' value='a,o,e,u'/> to: <path id='ueoa'> <pathelement
I'm making an application that updates a user's location and path in real time
def confirmemail(request,token): user = Users.objects.get(email_token = token) if user: user.email_token = '' user.email_token_expiry =
I've created a method in C# as you can see below public static IEnumerable<User>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.