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
.