Suppose I have several config classes as the ones below. All of them autowire a customObject which is configured based on a different filePath, and then its used to create another bean.
My concern with this approach is related to the fact that the customObject might need in the future another attribute (dependant on the config class) to work.
@Configuration
public ClassA{
@Value("${A.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
customObject.set(filePath);
return CustomObjectWrapperFactory.set(customObject).build();
}
}
@Configuration
public ClassB{
@Value("${B.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
customObject.set(filePath);
return CustomObjectWrapperFactory.set(customObject).build();
}
}
@Configuration
public ClassC{
@Value("${C.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
customObject.set(filePath);
return CustomObjectWrapperFactory.set(customObject).build();
}
}
Reading a little bit about Annotations and AOP I was wondering if it's possible to create an annotation that ,after the config class has been instantiated, gets the instance of the config class and using reflection invokes every setter of every autowired attribute defined in it and passes to them the needed attributes based on their names.
The end result would be something like this:
@Configuration
@InstantiateAutowiredObjectsAfterCreation
public ClassA{
@Value("${A.filePath}")
private String filePath;
@Autowired
private CustomObject customObject;
@Bean
public CustomObjectWrapper something(){
return CustomObjectWrapperFactory.set(customObject).build();
}
}
Aucun commentaire:
Enregistrer un commentaire