Mastering Java EE Development with WildFly
上QQ阅读APP看书,第一时间看更新

Bean types

Generics are introduced in Java EE 5 and they are propagated in different Java technologies. CDI supports generics too but it needs a particular declaration to restrict a type for a bean.

The annotation is @Typed. Here we will see its use. It’s important to declare @Typed when you would inject a typed bean. For example:

public class TransientOrderManager implements Comment<String>, Serializable {...}

Suppose we try to inject it into another bean as follows:

@Inject
private Comment<String> orderManager;

It will not be recognized and an exception will be thrown. To resolve the problem, you need to put the annotation:

@Typed
public class TransientOrderManager implements Comment<String>, Serializable {...}