0
# Enterprise JavaBeans
1
2
EJB API for developing enterprise business components with transaction management, security, and lifecycle support.
3
4
## Session Bean Annotations
5
6
```java { .api }
7
@Target({ElementType.TYPE})
8
@Retention(RetentionPolicy.RUNTIME)
9
public @interface Stateless {
10
String name() default "";
11
String mappedName() default "";
12
String description() default "";
13
}
14
15
@Target({ElementType.TYPE})
16
@Retention(RetentionPolicy.RUNTIME)
17
public @interface Stateful {
18
String name() default "";
19
String mappedName() default "";
20
String description() default "";
21
String passivationCapable() default "";
22
}
23
24
@Target({ElementType.TYPE})
25
@Retention(RetentionPolicy.RUNTIME)
26
public @interface Singleton {
27
String name() default "";
28
String mappedName() default "";
29
String description() default "";
30
}
31
```
32
33
## EJB Context
34
35
```java { .api }
36
public interface EJBContext {
37
EJBHome getEJBHome() throws IllegalStateException;
38
EJBLocalHome getEJBLocalHome() throws IllegalStateException;
39
Properties getEnvironment();
40
Identity getCallerIdentity();
41
Principal getCallerPrincipal();
42
boolean isCallerInRole(Identity role);
43
boolean isCallerInRole(String roleName);
44
UserTransaction getUserTransaction() throws IllegalStateException;
45
void setRollbackOnly() throws IllegalStateException;
46
boolean getRollbackOnly() throws IllegalStateException;
47
TimerService getTimerService() throws IllegalStateException;
48
Object lookup(String name) throws IllegalArgumentException;
49
Map<String, Object> getContextData();
50
}
51
```
52
53
## Lifecycle Callbacks
54
55
```java { .api }
56
@Target({ElementType.METHOD})
57
@Retention(RetentionPolicy.RUNTIME)
58
public @interface PostConstruct;
59
60
@Target({ElementType.METHOD})
61
@Retention(RetentionPolicy.RUNTIME)
62
public @interface PreDestroy;
63
64
@Target({ElementType.METHOD})
65
@Retention(RetentionPolicy.RUNTIME)
66
public @interface PostActivate;
67
68
@Target({ElementType.METHOD})
69
@Retention(RetentionPolicy.RUNTIME)
70
public @interface PrePassivate;
71
```
72
73
## Usage Example
74
75
```java
76
@Stateless
77
public class UserService {
78
79
@PersistenceContext
80
private EntityManager em;
81
82
@PostConstruct
83
public void init() {
84
// Initialization logic
85
}
86
87
public User findUser(Long id) {
88
return em.find(User.class, id);
89
}
90
91
@PreDestroy
92
public void cleanup() {
93
// Cleanup logic
94
}
95
}
96
```