0
# Dependency Injection
1
2
CDI and javax.inject APIs for dependency injection, contextual lifecycle management, events, and interceptors.
3
4
## Core Injection Annotations
5
6
```java { .api }
7
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
8
@Retention(RetentionPolicy.RUNTIME)
9
public @interface Inject;
10
11
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER})
12
@Retention(RetentionPolicy.RUNTIME)
13
public @interface Named {
14
String value() default "";
15
}
16
17
@Target({ElementType.TYPE})
18
@Retention(RetentionPolicy.RUNTIME)
19
public @interface Singleton;
20
```
21
22
## CDI Scopes
23
24
```java { .api }
25
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
26
@Retention(RetentionPolicy.RUNTIME)
27
@Scope
28
public @interface RequestScoped;
29
30
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
31
@Retention(RetentionPolicy.RUNTIME)
32
@Scope
33
public @interface SessionScoped;
34
35
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
36
@Retention(RetentionPolicy.RUNTIME)
37
@Scope
38
public @interface ApplicationScoped;
39
40
@Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD})
41
@Retention(RetentionPolicy.RUNTIME)
42
@Scope
43
public @interface ConversationScoped;
44
```
45
46
## Bean Manager
47
48
```java { .api }
49
public interface BeanManager {
50
Object getReference(Bean<?> bean, Type beanType, CreationalContext<?> ctx);
51
Object getInjectableReference(InjectionPoint ij, CreationalContext<?> ctx);
52
<T> CreationalContext<T> createCreationalContext(Contextual<T> contextual);
53
Set<Bean<?>> getBeans(Type beanType, Annotation... qualifiers);
54
Set<Bean<?>> getBeans(String name);
55
Bean<?> getPassivationCapableBean(String id);
56
<X> Bean<? extends X> resolve(Set<Bean<? extends X>> beans);
57
void validate(InjectionPoint injectionPoint);
58
void fireEvent(Object event, Annotation... qualifiers);
59
<T> Set<ObserverMethod<? super T>> resolveObserverMethods(T event, Annotation... qualifiers);
60
List<Decorator<?>> resolveDecorators(Set<Type> types, Annotation... qualifiers);
61
List<Interceptor<?>> resolveInterceptors(InterceptionType type, Annotation... interceptorBindings);
62
boolean isScope(Class<? extends Annotation> annotationType);
63
boolean isNormalScope(Class<? extends Annotation> annotationType);
64
boolean isPassivatingScope(Class<? extends Annotation> annotationType);
65
boolean isQualifier(Class<? extends Annotation> annotationType);
66
boolean isInterceptorBinding(Class<? extends Annotation> annotationType);
67
boolean isStereotype(Class<? extends Annotation> annotationType);
68
Set<Annotation> getInterceptorBindingDefinition(Class<? extends Annotation> bindingType);
69
Set<Annotation> getStereotypeDefinition(Class<? extends Annotation> stereotype);
70
boolean areQualifiersEquivalent(Annotation qualifier1, Annotation qualifier2);
71
boolean areInterceptorBindingsEquivalent(Annotation interceptorBinding1, Annotation interceptorBinding2);
72
int getQualifierHashCode(Annotation qualifier);
73
int getInterceptorBindingHashCode(Annotation interceptorBinding);
74
Context getContext(Class<? extends Annotation> scopeType);
75
ELResolver getELResolver();
76
ExpressionFactory wrapExpressionFactory(ExpressionFactory expressionFactory);
77
<T> AnnotatedType<T> createAnnotatedType(Class<T> type);
78
<T> InjectionTarget<T> createInjectionTarget(AnnotatedType<T> type);
79
<T> InjectionTargetFactory<T> getInjectionTargetFactory(AnnotatedType<T> annotatedType);
80
<X> ProducerFactory<X> getProducerFactory(AnnotatedField<? super X> field, Bean<X> declaringBean);
81
<X> ProducerFactory<X> getProducerFactory(AnnotatedMethod<? super X> method, Bean<X> declaringBean);
82
<T> BeanAttributes<T> createBeanAttributes(AnnotatedType<T> type);
83
BeanAttributes<?> createBeanAttributes(AnnotatedMember<?> type);
84
<T> Bean<T> createBean(BeanAttributes<T> attributes, Class<T> beanClass, InjectionTargetFactory<T> injectionTargetFactory);
85
<T, X> Bean<T> createBean(BeanAttributes<T> attributes, Class<X> beanClass, ProducerFactory<X> producerFactory);
86
InjectionPoint createInjectionPoint(AnnotatedField<?> field);
87
InjectionPoint createInjectionPoint(AnnotatedParameter<?> parameter);
88
<T extends Extension> T getExtension(Class<T> extensionClass);
89
<T> InterceptionFactory<T> createInterceptionFactory(CreationalContext<T> ctx, Class<T> clazz);
90
}
91
```
92
93
## Usage Examples
94
95
```java
96
@ApplicationScoped
97
public class UserService {
98
99
@Inject
100
private EntityManager em;
101
102
@Inject
103
private Logger logger;
104
105
public List<User> findAll() {
106
logger.info("Finding all users");
107
return em.createQuery("SELECT u FROM User u", User.class)
108
.getResultList();
109
}
110
}
111
112
@Named("userBean")
113
@RequestScoped
114
public class UserBean {
115
116
@Inject
117
private UserService userService;
118
119
private List<User> users;
120
121
@PostConstruct
122
public void init() {
123
users = userService.findAll();
124
}
125
126
public List<User> getUsers() {
127
return users;
128
}
129
}
130
```