1
votes

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;
    }
}
1
Put a breakpoint on the method and when hit, carefully read the call stack for clues.BalusC
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.Tiny
@Tiny Here, you can assume the bean is managed by JSF which can also process @PostConstruct.Sotirios Delimanolis
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).Tiny
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.Tiny

1 Answers

2
votes

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 :/