1

I use,

  • JSF
  • Spring
  • OCPSoft Rewrite
  • Glassfish 4 / Jetty 9

I've noticed that my beans invoke @PostConstruct's init() method twice. Here's sample bean that got initialized twice, if you'll need web.xml or anything else, just post it - I ran out of ideas.

@ManagedBean(name = "userBean")
public class UserBean implements Serializable {

    private static final long serialVersionUID = -1347081883455053542L;
    @ManagedProperty(value = "#{param.username}")
    private String username;
    private Users user;
    private Authentication authentication;
    private StreamedContent avatar;

    @PostConstruct
    public void init() {
        System.out.println("userbean init and username: " + username);
        user = Users.findByUsername(username);
        authentication = SecurityContextHolder.getContext()
                .getAuthentication();
        if (user == null) {
            Navigator.redirect("/601");
            return;
        }
        if (user.isKeepPrivate() == true && !username.equals(authentication.getName())) {
            Navigator.redirect("/600");
            return;
        }
        avatar = new DefaultStreamedContent(UserUtils.getAvatar(user), "image/png");
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public StreamedContent getAvatar() {
        return avatar;
    }

    public void setAvatar(StreamedContent avatar) {
        this.avatar = avatar;
    }
}
16
  • 1
    Put a breakpoint on the method and when hit, carefully read the call stack for clues. Commented Jan 15, 2014 at 18:43
  • 1
    If you have configured to have your beans managed by Spring then, annotations like @ManagedBean, @ManagedProperty would simply be ignored. You would need to have equivalent Spring annotations instead. Commented Jan 15, 2014 at 20:06
  • 1
    @Tiny Here, you can assume the bean is managed by JSF which can also process @PostConstruct. Commented Jan 15, 2014 at 20:49
  • 1
    If beans were managed by Spring (the Spring configuration file, applicationContext.xml or alike), what did you get with these annotations, @Controller, Scope("request")? (A view scope as and when required, needs to be customized, of course as it is not available in Spring directly). Commented Jan 16, 2014 at 18:32
  • 1
    If you need to set request parameters to a bean then you can use <f:viewParam> (also) nested inside <f:metadata> like in this case, <f:metadata> <f:viewParam name="username" id="username" converter="#{converterIfNeeded}" value="#{userBean.username}" valueChangeListener="#{valueChangedListenerIfNeeded}"/> </f:metadata>. Don't forget to remove the @ManagedProperty annotation before private String username; in your bean. Commented Jan 17, 2014 at 17:14

1 Answer 1

2

we have this problem here, but is a problem with WebSphere 6. (runaway from websphere :D)

So... we do a little workaround to use @PostConstruct...
Maybe can help you...

public boolean firstInit() {
    boolean firstInit= false;
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            firstInit= context != null  && context.getExternalContext().getRequestParameterMap().containsKey(ResponseStateManager.VIEW_STATE_PARAM);
        } catch (Exception e) {
            firstInit= false;
        }
        return firstInit;
    }
public void init(){
if (firstInit()) return;
//init methods
}

And @PostConstruct method called twice for the same request this can help you too...

obs: i cant write comments :/

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for your answer, however your solution doesn't work for me. TBH it wouldn't be a big problem for now, but when there's request parameter then in the second PostConstruct's call it's always null
I've run app on Jetty 9 and init was called twice again
Hmm... do you have other class in init() with @postconstruct? And if you use Spring why dont use @Named instead @ManagedBean? Try this too. code @Named @RequestScoped public class UserBean implements Serializable { }
When I used @Named the bean got initialized at the server start up so it get's request parameter username, which is null. What I wanted to achieve is whenever user opens a page with request parameter username it shows the profile page for the username in request parameter. However other beans got initialized twice as well, so that's general problem for me
Users or Authentication are static? if not where's Injection? They have init() methods with PostConstruct? O_o
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.