此内容没有您所选择的语言版本。

5.3. Bijection


Dependency injection or inversion of control (IoC) allows one component to reference another by having the container "inject" the component into a setter method or instance variable. In previous dependency injection implementations, injection occurred at component construction, and the reference did not change for the lifetime of the component instance. This is reasonable for stateless components — from the client's perspective, all instances of a particular stateless component are interchangeable. However, Seam emphasizes the use of stateful components, so traditional dependency injection as a construct is less useful. Seam introduces the notion of bijection as a generalization of injection. In contrast to injection, bijection is:
contextual
Bijection is used to assemble stateful components from various different contexts. A component from a wider context can even refer to a component from a narrower context.
bidirectional
Values are injected from context variables into attributes of the invoked component, and returned (via outjection) to the context, allowing the invoked component to manipulate contextual variable values simply by setting its own instance variables.
dynamic
Since the value of contextual variables changes over time, and since Seam components are stateful, bijection takes place every time a component is invoked.
In essence, 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. Annotations are used to enable bijection.
The @In annotation specifies that a value should be injected, either into an instance variable:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  @In User user; 
  ... 
}
Copy to Clipboard Toggle word wrap
or into a setter method:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  User user; 
  @In 
  public void setUser(User user) { this.user=user; } 
  ...
  }
Copy to Clipboard Toggle word wrap
By default, Seam performs a priority search of all contexts, using the name of the property or instance variable being injected. You may wish to specify the context variable name explicitly, using, for example, @In("currentUser").
If you want Seam to create an instance of the component, where there is no existing component instance bound to the named context variable, you should specify @In(create=true). If the value is optional (it can be null), specify @In(required=false).
For some components, specifying @In(create=true) each time it is used can be repetitive. In such cases, annotate the component @AutoCreate. This way, it will always be created whenever required, even without the explicit use of create=true.
You can even inject the value of an expression:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  @In("#{user.username}") String username; 
  ... 
}
Copy to Clipboard Toggle word wrap
Injected values are disinjected (that is, set to null) immediately after method completion and outjection.
(More information about component lifecycle and injection can be found in the next chapter.)
The @Out annotation specifies that an attribute should be outjected, either from an instance variable:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  @Out User user; 
  ... 
}
Copy to Clipboard Toggle word wrap
or from a getter method:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  User user; 
  
  @Out 
  public User getUser() { 
    return user; 
  } 
  ... 
}
Copy to Clipboard Toggle word wrap
An attribute may be both injected and outjected:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  @In 
  @Out User user; 
  ... 
}
Copy to Clipboard Toggle word wrap
or:
@Name("loginAction") 
@Stateless 
public class LoginAction implements Login { 
  User user;
   
  @In 
  public void setUser(User user) { 
    this.user=user; 
  }
   
  @Out 
  public User getUser() { 
    return user; } 
    ... 
}
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat