Spring未为ServiceListFactoryBean对象创建代理

Spring未为ServiceListFactoryBean对象创建代理,spring,aspect,serviceloader,Spring,Aspect,Serviceloader,我正在使用ServiceFactoryBean和ServiceListFactoryBean收集一些SPI实现实例,并自动连接到我的服务Bean。现在,我创建了一些方面来拦截这些类,以测量性能和日志调用 我注意到spring正在为ServiceFactoryBean捕获并注入到服务Bean中的实例创建代理。但它不会为ServiceListFactoryBean捕获的实例列表创建任何代理 我如何告诉spring为这些bean创建代理,以使我的方面能够工作 下面是我的代码片段- 收集SPI实现并将其

我正在使用ServiceFactoryBean和ServiceListFactoryBean收集一些SPI实现实例,并自动连接到我的服务Bean。现在,我创建了一些方面来拦截这些类,以测量性能和日志调用

我注意到spring正在为ServiceFactoryBean捕获并注入到服务Bean中的实例创建代理。但它不会为ServiceListFactoryBean捕获的实例列表创建任何代理

我如何告诉spring为这些bean创建代理,以使我的方面能够工作

下面是我的代码片段-

收集SPI实现并将其公开用于自动布线的配置

@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;
}
方面 (这个有效)

(这个不行)


仅供参考,我通过以下方式通过编程创建代理解决了这个问题-

@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();
}
@Autowired
私人通知执行人通知执行人;
@豆子
公共列表通知发件人(){
LOGGER.info(“NotificationSender实现的装箱代理”);
列表发件人=getAvailableNotificationSenders();
debug(“找到[{}]NotificationSender实现”,CollectionUtils.size(senders));
返回createProxiedNotificationSendersAsSpringWillNotCreateProxyForThese(发件人);
}
私有列表getAvailableNotificationSenders(){
列表发送者=新的ArrayList();
试一试{
ServiceListFactoryBean ServiceListFactoryBean=新ServiceListFactoryBean();
serviceListFactoryBean.setServiceType(NotificationSender.class);
serviceListFactoryBean.AfterPropertieSet();
发件人=(列表)serviceListFactoryBean.getObject();
}捕获(例外情况除外){
LOGGER.error(“无法检索通知发送方实现”,例如);
}
回信人;
}
私有列表CreateProxizedNotificationSendersAssSpringWillNotCreateProxy(列表notificationSenders){
List proxyNotificationSenders=new ArrayList();
for(NotificationSender:notificationSenders){
proxyNotificationSenders.add(createAspectJProxy(sender));
}
返回代理通知发送者;
}
私有NotificationSender CreateApectJProxy(NotificationSender NotificationSender){
AspectJProxyFactory AspectJProxyFactory=新AspectJProxyFactory(notificationSender);
aspectJProxyFactory.addAspect(notificationPerformanceLogger);
返回aspectJProxyFactory.getProxy();
}
@Pointcut("execution(* com.xxx.spi.notification.NotificationSender.*(..))")
private void anyNotificationSenderAPI() {}
@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();
}