Benutzerdefinierte Annotationen funktionieren bei Spring Beans nicht
Posted: 05 Jan 2025, 16:21
Ich habe meine neue benutzerdefinierte Anmerkung @MyCustomAnnotation erstellt:
Ich habe diese Annotation auf Komponente und Bean angewendet. Hier ist der Code,
Und
Jetzt möchte ich, dass alle Beans mit @MyCustomAnnotation annotiert werden. Hier ist der Code:
Meine Frage ist, warum ich kein Bean mit dem Namen „INCOMING_PROCESS_CHANNEL“ erhalte, da es @MyCustomAnnotation hat? Welche Codeänderungen sollte ich vornehmen, wenn ich eine Bean mit dem Namen „INCOMING_PROCESS_CHANNEL“ erhalten möchte?
Code: Select all
@Target({ElementType.METHOD, ElementType.TYPE, ElementType.FIELD})
@Retention(RUNTIME)
public @interface MyCustomAnnotation {
}
Code: Select all
@MyCustomAnnotation
@Component
public class CoreBusinessLogicHandler implements GenericHandler {
// some business logic
}
Code: Select all
@Configuration
public class BusinessConfig {
@Autowired
private CoreIntegrationComponent coreIntegrationComponent;
@MyCustomAnnotation
@Bean(name = INCOMING_PROCESS_CHANNEL)
public MessageChannel incomingProcessChannel() {
return coreIntegrationComponent.amqpChannel(INCOMING_PROCESS_CHANNEL);
}
//some other configurations
}
Code: Select all
import org.springframework.context.ApplicationContext;
@Configuration
public class ChannelConfig {
@Autowired
private ApplicationContext applicationContext;
public List getRecipientChannel(CoreIntegrationComponent coreIntegrationComponent) {
String[] beanNamesForAnnotation = applicationContext.getBeanNamesForAnnotation(MyCustomAnnotation.class);
//Here in output I am getting bean name for CoreBusinessLogicHandler Only.
}
}