Spring event listeners are called twice

I am a problem with Spring Service Listeners In my web application, any immediate help would be appreciated.

Event listeners are registered and called twice if I have a circular dependency.

I have a service class, this is an @transaction annotation on other methods

@Service(PBSTaskService.BEAN_NAME)
public class PBSTaskServiceImpl extends StandardServiceImpl<ITask> implements          PBSTaskService,ApplicationListener<SurveyDefinitionPublishedEvent>
{
    @Autowired
    private AutoSelectTaskSliceRouteSyncService autoSelectTaskSliceRouteSyncService; //  CYCLIC Dependency
    @Override
    public void onApplicationEvent(SurveyDefinitionPublishedEvent event)
     { 
      System.out.println("PBSTSImpl"); // THIS IS CALLED TWICE
     }
... Other method with @Transaction Annotation
}


@Service(AutoSelectTaskSliceRouteSyncService.BEAN_NAME)
public class AutoSelectTaskSliceRouteSyncServiceImpl implements AutoSelectTaskSliceRouteSyncService
{ 
      @Autowired private PBSTaskService pbsTaskService; // CYCLIC dependency
}

Now, if I remove the dependency of AutoSelectTaskSliceRouteSyncService on the first class, OnApplicationEvent is called once and the other twice.

, SimpleApplicationEventMulticaster.getApplicationListeners(myEvent): -, Cglib . , . , -, CGLIB. Tx:   proxy-target- class= "true false", .

,

https://jira.springsource.org/browse/SPR-7940?focusedCommentId=98988&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-98988

+4
4

ApplicationEvent -. , .

: . . -: 1. - Jdk 2. - Cglib, @transactions.

, :

  • ApplicationListener 2. 3. , @Transaction.

, spring . 2 3 , .

              spring , . , , -, , @transaction, .

+2

Spring , @Service @Component, ApplicationListener, . , , @Service @Compontent.

+1

Spring 4.2 ApplicationListener @EventListener . .

https://spring.io/blog/2015/02/11/better-application-events-in-spring-framework-4-2

@Component
public class MyListener {

    @EventListener
    public void handleContextRefresh(ContextRefreshedEvent event) {
        ...
    }
}
0

bean- Spring Spring Beans ( ) bean-, bean- Advised ApplicationListener ApplicationEventMulticaster. , , ApplicationEventMulticaster ( ).

Spring SimpleApplicationEventMulticaster, non- Advised Advised ApplicationListener ( , Advised onApplicationEvent , - @Transactional AOP-, , )

@Component
public class AdviceAwareApplicationEventMulticaster extends SimpleApplicationEventMulticaster {

    @Override
    protected Collection<ApplicationListener<?>> getApplicationListeners(ApplicationEvent event, ResolvableType eventType) {
        Map<ApplicationListener<?>, ApplicationListener<?>> listenersByNakedInstances = new LinkedHashMap<>();// because superclass returns sorted listeners
        Collection<ApplicationListener<?>> applicationListeners = super.getApplicationListeners(event, eventType);
        for (ApplicationListener<?> listener : applicationListeners) {
            boolean advised = false;
            ApplicationListener<?> nakedListener = null;
            if (listener instanceof Advised) {
                try {
                    nakedListener = (ApplicationListener<?>) ((Advised)listener).getTargetSource().getTarget();
                } catch (Exception e) {
                    // TODO 
                }
                advised = true;
            } else
                nakedListener = listener;
            if (advised || !listenersByNakedInstances.containsKey(nakedListener))
                listenersByNakedInstances.put(nakedListener, listener);
        }
        return listenersByNakedInstances.values();
    }
}

Spring , , bean- Spring, Spring Application Context .

, , Spring, , .

0

Source: https://habr.com/ru/post/1527494/


All Articles