3

I wanted to use @PostConstruct to initialize a bean in my webapp but I can't get it to work.

I've recreated the problem in a new project and it still won't work.

Am I missing something obvious here? As far as I can tell my init() method fulfills all the requirements listed in @PostConstruct API reference.

MyBean.java:

@ManagedBean
@RequestScoped
public class MyBean {
    @ManagedProperty(value="15")
    private int number = 10;

    @PostConstruct
    public void init(){
        number = 20;
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }
}

number.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">

<h:head>
    <title>Simple JSF Facelets page</title>
</h:head>

<h:body>
    Number = #{myBean.number}
</h:body>

</html>

I would expect Number = 20 but I get Number = 15.

Tiny
  • 27,221
  • 105
  • 339
  • 599
ddx
  • 33
  • 1
  • 3
  • This should work. Which server do you use? Which JSF version? You can add a print statement in your `init()` method to see if it gets called. – unwichtich Jan 23 '14 at 20:26
  • I'm using glassfish3.1.2 with Mojarra 2.1.6 (SNAPSHOT 20111206) – ddx Jan 23 '14 at 20:32
  • Strange. It seems the @ManagedProperty is recognized, but the @ PostConstruct doesn't. Really strange. – Eduardo Briguenti Vieira Jan 23 '14 at 20:41
  • SNAPSHOT might be the problem. You might want to avoid using it moving towards the [latest version](http://repo1.maven.org/maven2/org/glassfish/javax.faces/). [Mojarra 2.2.5](http://repo1.maven.org/maven2/org/glassfish/javax.faces/2.2.5/) (or [api](https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-api/2.2.5/), [impl](https://maven.java.net/content/repositories/releases/com/sun/faces/jsf-impl/2.2.5/)) is available today. – Tiny Jan 23 '14 at 21:16
  • It's indeed a bug. You should avoid snapshot versions. By the way, GlassFish 4, and as mentionned above, Mojarra 2.2+ are available. –  Jan 23 '14 at 21:46

2 Answers2

0

@PostConstruct seems to be called before the injection with @ManagedProperty, assuming you have MyFaces 2.0, as they say here.

Make sure you are using Mojarra 2.1 because it should work.

You might try to debug to know if your init() method is called before the injection, or never called.

Community
  • 1
  • 1
Happy
  • 1,815
  • 2
  • 18
  • 33
  • I think it's not getting called at all. When I comment out @ManagedProperty i get Number = 10, with private int number; (no initializer) i get Number = 0 – ddx Jan 23 '14 at 20:59
  • 1
    What about [that](http://ovaraksin.blogspot.fr/2011/05/what-to-do-if-postconstruct-not-called.html)? – Happy Jan 23 '14 at 21:08
  • [That](http://ovaraksin.blogspot.fr/2011/05/what-to-do-if-postconstruct-not-called.html) did it! :-) Thanks – ddx Jan 23 '14 at 22:41
-1

By default, Spring will not aware of the @PostConstruct and @PreDestroy annotation. To enable it, you have to either register CommonAnnotationBeanPostProcessor or specify the <context:annotation-config /> in bean configuration file.

Anthed
  • 139
  • 1
  • 6
  • 2
    Does it make a difference if you replace your class annotation ManagedBean by Named ? – Anthed Jan 23 '14 at 20:22
  • Interesting things to read here : http://palkonyves.blogspot.fr/2012/12/ive-been-using-postconstruct-wrong-way.html – Anthed Jan 23 '14 at 20:25