So I have a bean, ItemHolder, in which I have defined a @PostConstruct method:
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class ItemHolder {
private List<Item> items;
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
@PostConstruct
private void init() {
items = new ItemList().getItems(); // returns list of items
}
}
However, I get a "HTTP Status 500 - An error occurred performing resource injection on managed bean itemHolder" message, when I try to display the value in my xhtml file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<h:title></h:title>
</h:head>
<h:body>
<h1>Welcome to the shop #{login.username}.</h1>
#{itemHolder.items.get(0)};
</h:body>
</html>
However, when I change the @PostConstruct method in my bean to the following:
@PostConstruct
private void init() {
items = new ArrayList<Item>();
items.add(new Item(302, "Name", "URL", 3000, 50, "Description"));
}
Then I get no errors and the output displays just fine in my xhtml page.
The getItems() method that I called in the first @PostConstruct does throw some exceptions, but the exceptions are handled in that method and not @PostConstruct so I don't believe that shouldn't matter. It does also take about 5 seconds to process though. Not sure if that is a problem. Apart from that it's working just fine, just not with JSF and the PostConstruct-annotation.