PublisherBeanScanRegression.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334
  1. import com.fasterxml.jackson.databind.ObjectMapper;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  4. import org.springframework.data.redis.core.StringRedisTemplate;
  5. /** Real component scan, with no Boot auto-config or external Redis connection. */
  6. public class PublisherBeanScanRegression {
  7. static class Consumers {
  8. @Autowired com.zksy.base.event.DeviceStatusChangePublisher management;
  9. @Autowired com.zksy.api.event.DeviceStatusChangePublisher collection;
  10. }
  11. public static void main(String[] args) {
  12. try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
  13. context.getDefaultListableBeanFactory().setAllowBeanDefinitionOverriding(false);
  14. context.registerBean(ObjectMapper.class, () -> new ObjectMapper());
  15. context.registerBean(StringRedisTemplate.class, () -> new StringRedisTemplate() {
  16. @Override public void afterPropertiesSet() { /* no external connection */ }
  17. });
  18. context.registerBean(Consumers.class);
  19. context.scan("com.zksy.base.event", "com.zksy.api.event");
  20. context.refresh();
  21. Consumers consumers = context.getBean(Consumers.class);
  22. if (consumers.management == null || consumers.collection == null) {
  23. throw new AssertionError("Both publisher types must remain injectable");
  24. }
  25. if (context.getBeanNamesForType(com.zksy.base.event.DeviceStatusChangePublisher.class).length != 1
  26. || context.getBeanNamesForType(com.zksy.api.event.DeviceStatusChangePublisher.class).length != 1) {
  27. throw new AssertionError("Each publisher type must have exactly one bean");
  28. }
  29. System.out.println("PASS: both publishers scan and inject without bean overriding or external services");
  30. }
  31. }
  32. }