| JBoss Seam tutorial 2 |
|
|
|
| Written by Mark S. | |||||
| Tuesday, 23 September 2008 09:07 | |||||
|
Several Java EE APIs (EJB, JSF, etc.), as well as popular open source frameworks like Spring, make use of the concept of dependency injection. Injection involves the automatic, runtime insertion of a property value into a bean. This simple concept can greatly simplify development, especially in a Java EE environment.
The Seam component model also supports a more general version of dependency injection, called bijection. Standard dependency injection involves a one-time initialization of a bean reference within a component, typically done by some kind of container or other runtime service. Seam introduces the notion of bijection as a generalization of injection.
Before seeing Seam bijection, let's see what is Simple Injection, from an Example
@Name("MyBean")
public class MyBean {
@In
private OtherBean otherBean;
public SomeBean getOtherBean() {
return otherBean;
}
public void setOtherBean(OtherBean b)
{
otherBean = b;
}
}
Here, we’re annotating the formBean property on the Seam component MyBean, telling Seam to inject the value of this property whenever MyBean is invoked. In this case, we’re using @In with no arguments to inject an otherBean instance in MyBean class.
JBoss Seam BijectionAs mentioned at the start of this section, Seam extends simple injection by introducing the concept of outjection, or the export of a component value from one component back into the scope where the component lives.
@Stateless
@Name("useraction")
public class UserAction implements UserItf {
@In @Out private User user;
@Out private List userList;
@PersistenceContext private EntityManager em;
public String addAndDisplay() {
em.persist (user);
user = new User ();
userList = em.createQuery("From User u order by u.name").getResultList();
return null;
}
The useraction component in Seam is the UserAction session bean, as specified by the @Name annotation on the class. The UserAction class has user and userList fields annotated with the @In and @Out annotations. The @In and @Out annotations are at the heart of the Seam programming model. So, let's look at exactly what they do here. The @In annotation tells Seam to assign the user component, which is composed from the JSF form data, to the user field (dependency injection) before executing any method in the session bean. You can specify an arbitrary name for the injected component in @In. But if there is no named specified, as it is here, Seam will just inject the component with the same type and same name as the receiving field variable. The @Out annotations tell Seam to assign values of the userList and user fields to the managed components of the same names after any method execution. We call this action "dependency outjection" in Seam. This way, in the addAndDisplay method, we simply need to update the user and userList field values and they will be automatically available on the web page.
<f:view> As you can see, the datatable references the userList, which is populated in the addAndDisplayMethod
Getter / Setter Based BijectionIn the above example, we demonstrated Seam bijection against field variables.
private User user;
private List userList;
@In public void setUser (User user) {
this.user = user;
}
@Out public User getUser () {
return user;
}
@Out public List getUserList () {
return userList;
}
You may wonder why not complicating so much the lift with getter/setter bijection ?
JBoss.org Search
Custom Search
Only registered users can write comments!
Powered by !JoomlaComment 3.26
3.26 Copyright (C) 2008 Compojoom.com / Copyright (C) 2007 Alain Georgette / Copyright (C) 2006 Frantisek Hliva. All rights reserved." |
|||||
| Last Updated ( Tuesday, 28 October 2008 09:50 ) |




In short, bijection lets you alias a context variable to a component instance variable, by specifying that the value of the instance variable is injected, outjected, or both. 