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

  • Home
  • SEARCH
  • 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 8138297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T11:26:53+00:00 2026-06-06T11:26:53+00:00

I have a BaseBean with a @PostConstruct, and a bean extending it on which

  • 0

I have a BaseBean with a @PostConstruct, and a bean extending it on which i would like to call another @PostConstruct. I have read several places where it said it was possible, however, it seems the @postConstruct on the extending class is called first (if the second is called at all). I then get a NPE on “context” because I’m assuming the super bean’s PostConstruct has already been called.

Is this do-able? If so what am I doing wrong?

Base bean:

@ManagedBean
@RequestScoped
public class BaseBean {
@ManagedProperty(value = "#{contextBean}")
  private ContextBean contextBean;
  Context context;
@PostConstruct
public void setupContext() {
    context = getContextBean().getContext();
}

Extending bean:

@ManagedBean
@RequestScoped
public class SmartBoxSearchBean extends BaseBean {
@PostConstruct
public void setUp() {
    jsonHelper = context.get(SmartBoxJsonHelper.class);
}

Thanks,
Yotam.

  • 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-06-06T11:26:57+00:00Added an answer on June 6, 2026 at 11:26 am

    The @PostConstruct of the superclass of a backing bean is not called at all when the managed bean is constructed. It’s only called when a completely separate managed bean instance of that superclass is been constructed by e.g. using #{baseBean} in EL in your case. You effectively end up with two entirely separate instances #{baseBean} and #{smartBoxSearchBean} wherein the class’ own @PostConstruct method is invoked independently on the managed bean class itself.

    This design is somewhat strange. A superclass of a backing bean is normally not to be used as a managed bean at all.

    I suggest to revise your approach as follows:

    public abstract class BaseBean {
    
        @ManagedProperty("#{contextBean}")
        private ContextBean contextBean;
    
        public Context getContext() {
            return contextBean.getContext();
        }
    
    }
    

    and

    @ManagedBean
    @RequestScoped
    public class SmartBoxSearchBean extends BaseBean {
    
        @PostConstruct
        public void setUp() {
            jsonHelper = getContext().get(SmartBoxJsonHelper.class);
        }
    
    }
    

    Or maybe this, if you don’t need ContextBean for other purposes at all

    public abstract class BaseBean {
    
        @ManagedProperty("#{contextBean.context}")
        private Context context;
    
        public Context getContext() {
            return context;
        }
    
    }
    

    Note that @ManagedProperty works just fine when declared in a superclass this way.


    Update: depending on the functional requirements, you can also decouple the beans and just inject the #{baseBean} in the {smartBoxSearchBean}.

    @ManagedBean
    @RequestScoped
    public class BaseBean {
    
        @ManagedProperty("#{contextBean}")
        private ContextBean contextBean;
        private Context context;
    
        @PostConstruct
        public void init() {
            context = contextBean.getContext();
        }
    
    }
    

    and

    @ManagedBean
    @RequestScoped
    public class SmartBoxSearchBean {
    
        @ManagedProperty("#{baseBean}")
        private BaseBean baseBean; 
    
        @PostConstruct
        public void setUp() {
            jsonHelper = baseBean.getContext().get(SmartBoxJsonHelper.class);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a table with 3 million rows and would like to partition it. There
have a php code like this,going to convert it in to C#. function isValid($n){
Have a simple one-off tasks which needs a progress bar. OpenSSL has a useful
have a help file which i have created in Notepad++ with the following syntax
have some headers like: HTTP/1.1 100 Continue HTTP/1.1 302 Found HTTP/1.1 200 OK HTTP/1.1
Have a linking (or ref) table which has a dual primary key. Need to
Have advancements in CPU design like dynamic instruction scheduling narrowed the performance gap between
Have anyone used Redmine Documentor which lets you convert PHP to HTML to Redmine
Have the following scenario. I have a few form, which essentially have a few
Have an app that can use tts to read text messages. It can also

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.