我有一个带有模块的 EAR:
- foo-api.jar
- foo-impl.jar
- 拦截器.jar
在 foo-api 中有:
@Local
FooService // (interface of a local stateless session bean)
在 foo-impl 中有:
@Stateless
FooServiceImpl implements FooService //(implementation of the foo service)
在我想要的 interceptor.jar 中
public class BazInterceptor {
@EJB
private FooService foo;
@AroundInvoke
public Object intercept( final InvocationContext i) throws Exception {
// do someting with foo service
return i.proceed();
}
问题是:
Java EE 5 兼容应用程序服务器(例如 JBoss 5)是否会注入(inject)拦截器? 如果不是,访问 session bean 的好的策略是什么?
考虑:
- 部署顺序/竞争条件
最佳答案
是的,注入(inject)应该发生在拦截器中,例如 An Introduction to the Java EE 5 Platform 中提到的文章(粗体是我的):
Easier Access to Resources Through Dependency Injection
Dependency injection is a pattern in which an object's dependencies are supplied automatically by an entity external to that object. The object is not required to request these resources explicitly, for example, by looking them up in a naming service. In the Java EE 5 platform, dependency injection can be applied to all resources that a component needs, effectively hiding the creation and lookup of resources from application code. Dependency injection can be applied throughout Java EE 5 technology -- in EJB software containers, web containers, and clients.
To request injection of a resource, a component uses the
@Resource
annotation or, in the case of some specialized resources, the@EJB
and@WebServiceRef
annotations. Following are some of the many resources that can be injected:
SessionContext
objectDataSources
objectUserTransaction
EntityManager
interfaceTimerService
interface- Other enterprise beans
- Web services
- Message queues and topics
- Connection factories for resource adapters
- Environment entries (for example, strings, integers, and so on)
Resource injection can be requested by any component class, that is, any class whose life cycle is managed by the container. In the EJB software container, components that support injection include the following:
- EJB technology components
- Interceptors
- Message handlers for Java API for XML Web Services (JAX-WS) and Java API for XML-based RPC (JAX-RPC)
In web containers, components that support injection are the following:
- Servlets, servlet filters, event listeners
- Tag handlers, tag library event listeners
- Managed beans
In the client container, the main class and the
login
callback handler components support injection.
另见 EJB Interceptors JBoss EJB 3.0 教程部分:
Just like a bean class, an interceptor can be the target of Dependency injection. The format for how this works is the same, and the injection works off the same ENC as the bean to which the interceptor is bound.
...
Remember that interceptors follow the same lifecycle as the bean they are bound to. The interceptors are created at the same time as the bean instance is created, and dependency injection occurs before the first business method is called.
资源
- An Introduction to the Java EE 5 Platform
- EJB 3.0 教程
- EJB Interceptors (参见注入(inject)部分)
关于java - 我可以将 SessionBean 注入(inject) Java EE AroundInvoke-Interceptor 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/906786/