Repository navigation
Expand file tree
/
Copy pathExpBeanPostProcessor.java
More file actions
130 lines (108 loc) · 5.73 KB
/
Copy pathExpBeanPostProcessor.java
File metadata and controls
130 lines (108 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
// Simula BeanPostProcessor: intercepta cada bean antes y después de su inicialización.
// Spring llama a todos los BeanPostProcessor registrados en orden para cada bean.
public class ExpBeanPostProcessor {
// ── Marcador de validación ────────────────────────────────────────────────
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface NotNull {}
// ── Beans de ejemplo ─────────────────────────────────────────────────────
static class ServicioEmail {
@NotNull String smtpHost;
@NotNull String fromAddress;
ServicioEmail(String smtpHost, String fromAddress) {
this.smtpHost = smtpHost;
this.fromAddress = fromAddress;
}
public void enviar(String dest) {
System.out.println(" Email enviado a " + dest + " via " + smtpHost);
}
}
static class ServicioInvalido {
@NotNull String apiKey; // null a propósito → ValidationPostProcessor lo detecta
ServicioInvalido() { /* apiKey queda null */ }
}
// ── Interfaz BeanPostProcessor ────────────────────────────────────────────
interface BeanPostProcessor {
// Se ejecuta antes de init() del bean (p.ej., antes de @PostConstruct)
default Object postProcessBeforeInit(Object bean, String name) { return bean; }
// Se ejecuta después de init() del bean (p.ej., después de @PostConstruct)
default Object postProcessAfterInit(Object bean, String name) { return bean; }
}
// ── Procesadores ──────────────────────────────────────────────────────────
// Registra el ciclo de vida en consola — util para diagnosticar el orden de inicialización
static class LoggingPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInit(Object bean, String name) {
System.out.println(" [LOG] BEFORE INIT → " + name + " (" + bean.getClass().getSimpleName() + ")");
return bean;
}
@Override
public Object postProcessAfterInit(Object bean, String name) {
System.out.println(" [LOG] AFTER INIT → " + name + " listo");
return bean;
}
}
// Valida que los campos @NotNull no sean null al finalizar la inicialización
static class ValidationPostProcessor implements BeanPostProcessor {
@Override
public Object postProcessAfterInit(Object bean, String name) {
for (Field field : bean.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(NotNull.class)) {
field.setAccessible(true);
try {
if (field.get(bean) == null) {
throw new IllegalStateException(
"Bean '" + name + "': campo @NotNull '" + field.getName() + "' es null");
}
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
System.out.println(" [VALID] " + name + " pasó validación @NotNull");
return bean;
}
}
// ── Cadena de procesadores ────────────────────────────────────────────────
// Spring aplica los BeanPostProcessor en el orden en que están registrados
static class PostProcessorChain {
private final List<BeanPostProcessor> processors = new ArrayList<>();
void add(BeanPostProcessor processor) { processors.add(processor); }
// Simula el ciclo completo: before → init() → after
Object process(Object bean, String name) {
for (BeanPostProcessor pp : processors) {
bean = pp.postProcessBeforeInit(bean, name);
}
// Aquí Spring llama a @PostConstruct / InitializingBean.afterPropertiesSet()
System.out.println(" [INIT] " + name + ".init()");
for (BeanPostProcessor pp : processors) {
bean = pp.postProcessAfterInit(bean, name);
}
return bean;
}
}
public static void main(String[] args) {
PostProcessorChain chain = new PostProcessorChain();
// El orden de registro determina el orden de ejecución
chain.add(new LoggingPostProcessor());
chain.add(new ValidationPostProcessor());
System.out.println("=== Bean válido ===");
chain.process(new ServicioEmail("smtp.gmail.com", "noreply@example.com"), "servicioEmail");
System.out.println("\n=== Bean con campo @NotNull null ===");
try {
chain.process(new ServicioInvalido(), "servicioInvalido");
} catch (IllegalStateException e) {
System.out.println(" Error detectado: " + e.getMessage());
}
System.out.println("\n=== Nota ===");
System.out.println("El orden de los BeanPostProcessor importa:");
System.out.println("LoggingPostProcessor primero → registra todo, incluso los fallos de validación.");
}
}