- JBoss Weld CDI for Java Platform
- Ken Finnegan
- 480字
- 2021-08-13 16:49:55
Client proxies
The reference to a bean injected into an injection point, or obtained by programmatic lookup, is usually not a direct reference to an instance of a bean, unless the injected bean is of @Dependent
scope.
Instead of the actual bean instance, Weld injects a client proxy that is responsible for ensuring only the bean instance associated with the current context has a method invoked on it. That might sound confusing, but it will become clearer with an example.
@RequestScoped public class RequestBean { ... } @ApplicationScoped public class ApplicationBean { @Inject RequestBean bean; }
Given the two beans we just defined, we would not want the same @RequestScoped
bean to be used by all requests to our application, as there is only one instance of the @ApplicationScoped
bean. The client proxy is injected into the @ApplicationScoped
bean instead of an instance of the @RequestScoped
bean and is responsible for retrieving the bean instance from the current request scope whenever a method is called. Through the client proxy, Weld is able to have two different requests using the same @ApplicationScoped
bean, while calling methods on their respective @RequestScoped
bean instances without us needing to do any special wiring or configuration.
A client proxy is also beneficial in situations where we have a bean in a scope that can be serialized to disk, such as @SessionScoped
, and it has references to beans in a scope that can be retrieved at will, such as @ApplicationScoped
. It certainly does not provide any benefit to serialize a bean instance that can be retrieved whenever it's needed, so the client proxy is serialized in its place.
Note
The client proxy being serialized to disk instead of the actual bean instance has the added benefit of not recursively serializing to disk a potentially large tree of bean instances that have references to other beans.
Unproxyable bean types
Due to limitations of the Java language, there are some legal bean types that are not able to have a client proxy created for them by the container. If an injection point tries to inject a bean type that is unable to be proxied, and it is not declared in the @Dependent
scope, the container will abort deployment with an appropriate error message.
The following bean types are unable to have a client proxy created for them:
- Classes without a non-private constructor with no parameters, that is, a default constructor
- Classes declared
final
or withfinal
methods - Primitive types
- Array types
Here are some tips on how to resolve an unproxyable dependency error, such as the ones just mentioned:
- Add a default constructor to the bean type being injected
- Create an interface that can be implemented by the bean being injected and change the injection point to use the interface instead of the implementation
- If none of the previous work, we can set the scope to be
@Dependent
- DB2 V9權(quán)威指南
- Mastering OpenLayers 3
- VMware View Security Essentials
- 兩周自制腳本語(yǔ)言
- PostgreSQL Cookbook
- 區(qū)塊鏈架構(gòu)與實(shí)現(xiàn):Cosmos詳解
- jQuery EasyUI網(wǎng)站開(kāi)發(fā)實(shí)戰(zhàn)
- 技術(shù)領(lǐng)導(dǎo)力:程序員如何才能帶團(tuán)隊(duì)
- Web Application Development with R Using Shiny(Second Edition)
- VSTO開(kāi)發(fā)入門(mén)教程
- 編寫(xiě)高質(zhì)量代碼:改善C程序代碼的125個(gè)建議
- Learning Firefox OS Application Development
- 編譯系統(tǒng)透視:圖解編譯原理
- SQL Server 2016數(shù)據(jù)庫(kù)應(yīng)用與開(kāi)發(fā)習(xí)題解答與上機(jī)指導(dǎo)
- Elasticsearch Server(Third Edition)