2017-03-11 3 views
0

Je collecte des instances d'implémentation SPI en utilisant ServiceFactoryBean et ServiceListFactoryBean et le câblage automatique à mes beans de service. J'ai maintenant créé certains aspects pour intercepter ces classes afin de mesurer les performances et les invocations de journaux. Je remarque que spring crée un proxy pour les instances capturées par ServiceFactoryBean et injectées dans les beans de service. Mais il ne crée aucun proxy pour la liste des instances capturées par ServiceListFactoryBean.Ressort ne créant pas de proxy pour les objets ServiceListFactoryBean

Comment puis-je dire au printemps de créer proxy pour ces haricots à ce que mes aspects fonctionnent?

Ce qui suit est mon extrait de code -

Configuration qui rassemble les implémentations SPI et les expose pour Autowiring

@Bean 
public ServiceFactoryBean faceImageStorageProvider() { 
    ServiceFactoryBean serviceFactoryBean = new ServiceFactoryBean(); 
    serviceFactoryBean.setServiceType(FaceImageStorageProvider.class); 

    return serviceFactoryBean; 
} 

@Bean 
public ServiceListFactoryBean notificationSenders() { 
    ServiceListFactoryBean serviceListFactoryBean = new ServiceListFactoryBean(); 
    serviceListFactoryBean.setServiceType(NotificationSender.class); 

    return serviceListFactoryBean; 
} 

Aspects (Celui-ci fonctionne)

@Pointcut("execution(* com.xxx.spi.storage.FaceImageStorageProvider.*(..))") 
private void anyFaceImageStorageProviderAPI() {} 

(Celui-ci ne fonctionne pas

@Pointcut("execution(* com.xxx.spi.notification.NotificationSender.*(..))") 
private void anyNotificationSenderAPI() {} 

Répondre

0

Juste Pour votre information, je résolu le problème en créant par programme les procurations de manière suivante -

@Autowired 
private NotificationPerformanceLogger notificationPerformanceLogger; 

@Bean 
public List<NotificationSender> notificationSenders() { 
    LOGGER.info("Crating proxy for NotificationSender implementations"); 

    List<NotificationSender> senders = getAvailableNotificationSenders(); 
    LOGGER.debug("Found [{}] NotificationSender implementations", CollectionUtils.size(senders)); 

    return createProxiedNotificationSendersAsSpringWillNotCreateProxyForThese(senders); 
} 

private List<NotificationSender> getAvailableNotificationSenders() { 
    List<NotificationSender> senders = new ArrayList<>(); 
    try { 
     ServiceListFactoryBean serviceListFactoryBean = new ServiceListFactoryBean(); 
     serviceListFactoryBean.setServiceType(NotificationSender.class); 
     serviceListFactoryBean.afterPropertiesSet(); 

     senders = (List<NotificationSender>) serviceListFactoryBean.getObject(); 
    } catch (Exception ex) { 
     LOGGER.error("Unable to retrieve notification sender implementations", ex); 
    } 

    return senders; 
} 

private List<NotificationSender> createProxiedNotificationSendersAsSpringWillNotCreateProxyForThese(List<NotificationSender> notificationSenders) { 
    List<NotificationSender> proxyNotificationSenders = new ArrayList<>(); 
    for (NotificationSender sender : notificationSenders) { 
     proxyNotificationSenders.add(createAspectJProxy(sender)); 
    } 

    return proxyNotificationSenders; 
} 

private NotificationSender createAspectJProxy(NotificationSender notificationSender) { 
    AspectJProxyFactory aspectJProxyFactory = new AspectJProxyFactory(notificationSender); 
    aspectJProxyFactory.addAspect(notificationPerformanceLogger); 

    return aspectJProxyFactory.getProxy(); 
}