配置 Spring 以忽略使用 @Inject 注释的依赖项

Configuring Spring to ignore dependencies annotated with @Inject

我有一个 EJB class,我需要在其中注入两个 bean - 一个应该由 EJB 容器注入,另一个是 Spring 容器。

@Stateless
@Interceptors(SpringBeanAutowiringInterceptor.class)
@LocalBean
public class SomeClass {

    @Inject
    private EJBClass a;

    @Autowired
    private SpringComponent b;

}

在这里,Spring 拦截器试图拦截 bean 'a' 的注入,但它失败了。我希望 EJB 容器注入 bean 'a' 和 Spring 容器注入 bean 'b'.

请告诉我一条出去的路。

使用@EJB注解注入EJB

通过自定义 SpringBeanAutowiringInterceptor class,可以从自动连接中排除用 @Inject 注释的依赖项。

要了解幕后发生的事情,请查看源代码 SpringBeanAutowiringInterceptor.java -

/**
 * Actually autowire the target bean after construction/passivation.
 * @param target the target bean to autowire
 */
protected void doAutowireBean(Object target) {
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    configureBeanPostProcessor(bpp, target);
    bpp.setBeanFactory(getBeanFactory(target));
    bpp.processInjection(target);
}

doAutowireBean 的第一行,创建了 AutowiredAnnotationBeanPostProcessor 的新实例。这里配置了一组要扫描的自动连接依赖项的注释。

/**
 * Create a new AutowiredAnnotationBeanPostProcessor
 * for Spring's standard {@link Autowired} annotation.
 * <p>Also supports JSR-330's {@link javax.inject.Inject} annotation, if available.
 */
@SuppressWarnings("unchecked")
public AutowiredAnnotationBeanPostProcessor() {
    this.autowiredAnnotationTypes.add(Autowired.class);
    this.autowiredAnnotationTypes.add(Value.class);
    try {
        this.autowiredAnnotationTypes.add((Class<? extends Annotation>)
                ClassUtils.forName("javax.inject.Inject", AutowiredAnnotationBeanPostProcessor.class.getClassLoader()));
        logger.info("JSR-330 'javax.inject.Inject' annotation found and supported for autowiring");
    }
    catch (ClassNotFoundException ex) {
        // JSR-330 API not available - simply skip.
    }
}

因为默认情况下配置了 @Inject 注解 spring 扫描标有 @Inject 的各个依赖项并尝试自动连接它们。

要排除 @Inject 注释依赖项,请在自定义 class 下方写入。

public class CustomSpringBeanAutowiringInterceptor extends SpringBeanAutowiringInterceptor {

    /**
     * Template method for configuring the
     * {@link AutowiredAnnotationBeanPostProcessor} used for autowiring.
     * @param processor the AutowiredAnnotationBeanPostProcessor to configure
     * @param target the target bean to autowire with this processor
     */
    protected void configureBeanPostProcessor(AutowiredAnnotationBeanPostProcessor processor, Object target) {
        Set<Class> annotationsToScan = new HashSet<Class>();
        annotationsToScan.add(Autowired.class);
        annotationsToScan.add(Value.class);
        processor.setAutowiredAnnotationTypes(annotationsToScan);

    }
}

这里 configureBeanPostProcessor 钩子用于自定义 bean post 处理器,以便只包含我们需要自动连接的注释。

将此自定义 class 应用为代码中的拦截器后,可以实现所需的行为

@Stateless
@Interceptors(CustomSpringBeanAutowiringInterceptor.class)
@LocalBean
public class SomeClass {

@Inject
private EJBClass a;

@Autowired
private SpringComponent b;

}

如果您遇到任何问题,请在评论中告知。也可以根据需要随意优化代码,并原谅任何编译/格式问题。