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;
    }
}
ZZ 5
  • 1,744
  • 26
  • 41
  • 1
    Put a breakpoint on the method and when hit, carefully read the call stack for clues. – BalusC Jan 15 '14 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. – Tiny Jan 15 '14 at 20:06
  • 1
    @Tiny Here, you can assume the bean is managed by JSF which can also process `@PostConstruct`. – Sotirios Delimanolis Jan 15 '14 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). – Tiny Jan 16 '14 at 18:32
  • @Tiny I tried to use it like you proposed with beans that I play with and results are: bean posted here was initialized once, however request param was null (without request context listener it was initialized three times). Second bean that I haven't posted here was initialized twice, just as before. – ZZ 5 Jan 17 '14 at 14:49
  • 1
    If you need to set request parameters to a bean then you can use [``](http://stackoverflow.com/a/4889226/1391249) ([also](http://stackoverflow.com/a/6377957/1391249)) nested inside `` like in this case, ` `. Don't forget to remove the `@ManagedProperty` annotation before `private String username;` in your bean. – Tiny Jan 17 '14 at 17:14
  • @Tiny I did just as you suggested, however there was still problem with initialization. I've changed DynaImage to JSF and it's workaround, but it's ok for now. – ZZ 5 Jan 19 '14 at 17:12
  • 1
    **Disclaimer :** I don't know the structure of your application (and I'm a fresh beginner). I have an application that uses Spring Framework 4.0.0 GA, Spring Security 3.2.0 GA, Mojarra 2.2.5. My `applicationContext.xml` file looks like [this](http://pastebin.com/04DuqFt6), [`web.xml`](http://pastebin.com/Lm3PfevL), [`faces-config.xml`](http://pastebin.com/umfzb1YZ) and [here](http://pastebin.com/hfvhzJyP) is a sample bean. I have commented xml tags regarding Spring Security, PrimeFaces file upload etc. Could you please have a look at them, there might be something wrong in your configurations? – Tiny Jan 19 '14 at 18:25
  • 1
    Again in my application, beans are managed/maintained by Spring and not by JSF. Therefore, all of them are not JSF managed beans. If there is a different scenario in your application then, avoid all of my saying (I'm just leaning how these two frameworks work together). If you need a view scoped bean as provided by JSF later on then, [this](http://blog.harezmi.com.tr/spring-view-scope-for-jsf-2-users/) blog, (also [this](http://cagataycivici.wordpress.com/2010/02/17/port-jsf-2-0s-viewscope-to-spring-3-0/) and [this](http://comdynamics.net/blog/109/spring3-jsf2-view-scope/)) would be helpful. – Tiny Jan 19 '14 at 18:38
  • 1
    Presumably, some Spring versions may allow a view scope just like `Scope("view")` but I'm currently not familiar with it. (If you have wasted your time reading my comments and other things then, I really apologize to you :).) – Tiny Jan 19 '14 at 18:45
  • 1
    PS : One small correction. I was earlier using Spring Framework 3.2.0, 3.2.2, 3.2.3, Spring Security 3.2.0 M1, JSF 2.1 in which the same thing was working correctly. – Tiny Jan 19 '14 at 18:54
  • I saw your config files - mine seems pretty similar as I mentioned before, my base config was inherited from Spring Roo app so I guess it's ok. Thanks for your help, with my workaround I guess that problem is solved for now – ZZ 5 Jan 19 '14 at 19:22
  • 1
    Wouldn't it be helpful to others, if you add it as an answer? :) Thanks. – Tiny Jan 20 '14 at 10:04
  • Well I'm not sure, because it helps only this special case - I've changed to JSF 1.1 and as you mentioned before - removed @ManagedProperty. However the other bean is called twice – ZZ 5 Jan 20 '14 at 20:26
  • 1
    If this is the case, "*However the other bean is called twice*" then, you're probably mixing both Spring and JSF somewhere in your application. We can use either Spring or JSF but not both together. This could be very confusing. You may proceed with the next question after some investigation about the problem. I'm learning JavaEE almost all from [here](http://stackoverflow.com/users/157882/balusc) and [here](http://balusc.blogspot.in/) and I have learnt many such things thanks to BalusC. Respect to BalusC :). – Tiny Jan 21 '14 at 10:32
  • Well, yeah ; ) Most of my problems are solved by BalusC here on SO :) – ZZ 5 Jan 21 '14 at 16:37

1 Answers1

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

Community
  • 1
  • 1
xild
  • 187
  • 8
  • 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 – ZZ 5 Jan 16 '14 at 16:28
  • I've run app on Jetty 9 and init was called twice again – ZZ 5 Jan 16 '14 at 16:47
  • 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 { } ` – xild Jan 16 '14 at 16:56
  • 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 – ZZ 5 Jan 16 '14 at 17:19
  • `Users` or `Authentication` are static? if not where's Injection? They have init() methods with PostConstruct? O_o – xild Jan 16 '14 at 18:02
  • 'Users' has got static methods, authentication is initialized in init() method – ZZ 5 Jan 16 '14 at 18:07
  • I've followed Spring Roo config, so I guess it's ok – ZZ 5 Jan 16 '14 at 18:28