Mixing Annotations with faces-config.xml Settings in JSF 2.0

What happens when you annotate a bean, and edit the configuration in the faces-config.xml file at the same time?

With JavaServer Faces 2.0, we finally see the introduction of a standard, annotation based approach to component development. No longer do your JSF applications need to maintain a long and arduous faces-config.xml file. Instead, you can simply annotate your JavaBeans, and the JSF framework will manage your beans accordingly.


So, to have the JSF framework recognize a JavaBean, and manage the lifecycle of that instance, here’s how simple the annotation is:


public class SuperBean { /* stuff goes in here */}


And you can further annotate. So if you wanted the bean to be managed in the request scope, you simply add another annotation:


public class SuperBean { /* stuff goes in here */}  


And all of this replaces the need for a faces-config.xml file such as the following:


<?xml version='1.0' encoding='UTF-8'?>

<faces-config xmlns="http://java.sun.com/xml/ns/javaee"











Of course, the question always arises as to what happens if you both annotate the POJO, and you have a configuration in the faces-config.xml file. Well, at runtime, the faces-config.xml file trumps any annotations. So, if you have a bean whose behavior you want to change, but you can’t get in and edit the source code, all you have to do is configure a faces-config.xml file, and the corresponding settings will take precedence at runtime.

Dig Deeper on Front-end, back-end and middle-tier frameworks

App Architecture
Software Quality
Cloud Computing