| JBoss Seam tutorial 2 |
| Written by Mark S. | |||||
|
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.
<f:view> As you can see, the datatable references the userList, which is populated in the addAndDisplayMethod by the following query
Getter / Setter Based BijectionIn the above example, we demonstrated Seam bijection against field variables. You can also biject components against getter and setter methods. For instance, take a look at the following code:
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 ? the real value of this change is that you can add custom logic to manipulate the bijection process. For instance, you can validate the injected object or retrieve the outjected object on the fly from the database.
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." |



In short, 