Repository navigation
Expand file tree
/
Copy pathExpPerfiles.java
More file actions
84 lines (70 loc) · 3.26 KB
/
Copy pathExpPerfiles.java
File metadata and controls
84 lines (70 loc) · 3.26 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
import java.util.HashMap;
import java.util.Map;
// En Spring Boot, @Profile filtra qué beans se registran según el perfil activo.
// SPRING_PROFILES_ACTIVE=prod o --spring.profiles.active=prod activan el perfil.
// Aquí se replica la misma lógica con una variable String activeProfile.
public class ExpPerfiles {
interface ServicioEmail {
void enviar(String destinatario, String asunto);
}
// @Profile("dev") — Solo activo cuando el perfil activo es "dev"
static class EmailFalso implements ServicioEmail {
@Override
public void enviar(String destinatario, String asunto) {
System.out.println("[DEV] Email simulado → " + destinatario + ": " + asunto);
}
}
// @Profile("prod") — Solo activo cuando el perfil activo es "prod"
static class EmailSMTP implements ServicioEmail {
@Override
public void enviar(String destinatario, String asunto) {
System.out.println("[PROD] SMTP → " + destinatario + ": " + asunto);
}
}
// Registro de beans que respeta perfiles — equivalente al contenedor de Spring Boot
static class ProfileRegistry {
private final String activeProfile; // equivale a SPRING_PROFILES_ACTIVE
private final Map<Class<?>, Object> beans = new HashMap<>();
ProfileRegistry(String activeProfile) {
this.activeProfile = activeProfile;
}
// Registra el bean solo si el perfil declarado coincide con el activo.
// Equivale a anotar un @Bean con @Profile("dev") / @Profile("prod").
<T> void registerForProfile(Class<T> type, String profile, java.util.function.Supplier<T> factory) {
if (profile.equals(activeProfile)) { // @Profile("dev") / @Profile("prod")
beans.put(type, factory.get());
}
}
@SuppressWarnings("unchecked")
<T> T getBean(Class<T> type) {
Object bean = beans.get(type);
if (bean == null) throw new IllegalStateException(
"Bean no encontrado para el tipo " + type.getSimpleName() +
" con perfil activo: " + activeProfile);
return (T) bean;
}
}
// Equivalente a @Configuration — declara qué beans existen y bajo qué perfil
static class AppConfig {
static void configure(ProfileRegistry registry) {
// @Bean @Profile("dev")
registry.registerForProfile(ServicioEmail.class, "dev", EmailFalso::new);
// @Bean @Profile("prod")
registry.registerForProfile(ServicioEmail.class, "prod", EmailSMTP::new);
}
}
static void ejecutarConPerfil(String perfil) {
// En Spring Boot: SPRING_PROFILES_ACTIVE=prod o --spring.profiles.active=prod
// Aquí lo establecemos directamente en el ProfileRegistry antes del refresh
ProfileRegistry registry = new ProfileRegistry(perfil);
AppConfig.configure(registry);
System.out.println("Perfil activo: " + perfil);
ServicioEmail email = registry.getBean(ServicioEmail.class);
email.enviar("jorex@example.com", "Bienvenido a java-prep");
}
public static void main(String[] args) {
ejecutarConPerfil("dev");
System.out.println();
ejecutarConPerfil("prod");
}
}