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 6023089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:57:12+00:00 2026-05-23T03:57:12+00:00

I am new to CDI and want to use this for a JSF2 application.

  • 0

I am new to CDI and want to use this for a JSF2 application. The class MyUser is a simple @Entity-Bean and a object is created in a @PostConstruct method in bean:

@Stateful
@Named @javax.faces.bean.SessionScoped
public class UserBean implements Serializable
{
    @Named
    private MyUser user;

    //setter and getter
    //@PostConstruct
}

Accessing the user in a JSF pages works like a charm: #{user.lastName}. But now I want to access this object from other beans, e.g. in this @ViewScopedBean:

@Named @javax.faces.bean.ViewScoped
public class TestBean implements Serializable
{       
    @Inject private MyUser user;
}

I want the current (logged in) MyUser user to be available in a couple of other beans, but I’m not sure how to do this. Simply @Injecting it did not work (and I’m pretty sure this would be a litte bit to simple).

13:56:22,371 ERROR [org.jboss.kernel.plugins.dependency.AbstractKernelController]
Error installing to Start: name=vfs:///Applications/Development/
jboss-6.0.0.Final/server/default/deploy/test.ear_WeldBootstrapBean state=Create:
org.jboss.weld.exceptions.DeploymentException: WELD-001408 Unsatisfied
dependencies for type [MyUser] with qualifiers [@Default] at injection
point [[field] @Inject private test.controller.mbean.TestBean.user]

What is the best approach to access the user from other beans? JSF1.2 style code like UserBean bean = (UserBean)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("UserBean"); seems seems to be old fashioned!

  • 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-23T03:57:13+00:00Added an answer on May 23, 2026 at 3:57 am

    First of all: You don’t want to directly inject entities. Entities are pretty independently controlled by the ORM-framework, and have their own life cycle. Don’t use them as managed beans.

    According to this definition, JPA
    entities are technically managed
    beans. However, entities have their
    own special lifecycle, state and
    identity model and are usually
    instantiated by JPA or using new.
    Therefore we don’t recommend directly
    injecting an entity class. We
    especially recommend against assigning
    a scope other than @Dependent to an
    entity class, since JPA is not able to
    persist injected CDI proxies.

    See here for details.

    To answer your question: You cannot “outject” something like an (authenticated) user, even though this was possible in Seam 2, the whole proxy mechanism of CDI doesn’t allow this anymore. What you need to do is the following:

    • Write a managed bean which handles the authentication and put it in the correct scope (probably session scope).
    • If login succeeds, use an attribute of this bean to store the authenticated user.
    • Use a producer method (probably with a qualifier like @LoggedIn) to make the user availabe in your application

    Inject the user like this:

    @Inject
    @LoggedIn
    private User user
    

    That’s the CDI-way 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

New to Regex. I want to validate to this format: Any character allowed, except
[My Setup: Java EE 6 application, with EJB3.1, CDI/Weld, JSF2 running on Glassfish 3.0.1]
New to Git and just started to use Github. Just created my first public
New to Entity Framework .Using EF4. I have found articles and managed to use
I try to use TJWS Embeddable Servlet Container to start RestEasy application using this
In my application, I have an @ApplicationScoped CDI bean to store some information from
New class is a subclass of the original object It needs to be php4
new on ruby and using windows xp and rails 3, i want to send
New to PHP and MySQL, have heard amazing things about this website from Leo
new to c#. I'm trying to make a simple system where I can search

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.