配置Spring以忽略使用@Inject注释的依赖项 [英] Configuring Spring to ignore dependencies annotated with @Inject

查看:310
本文介绍了配置Spring以忽略使用@Inject注释的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an EJB class in which I need to inject two beans - one should be injected by the EJB container and other is a Spring Container.

@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'。

Here, the Spring interceptor trying to intercept the injection of bean 'a' and it's getting failed. I want the EJB container to inject the bean 'a' and Spring container to inject bean 'b'.

请给我一个出路。

推荐答案

通过自定义 SpringBeanAutowiringInterceptor 类,使用 @Inject注释的依赖项可以从汽车布线中排除。

By customizing the SpringBeanAutowiringInterceptor class, dependencies annotated with @Inject can be excluded from auto wiring.

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

To understand what happens behind the scene, have a look at source code of 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 实例。这里配置了要扫描的自动布线依赖关系集。

At first line, of doAutowireBean, a new instance of AutowiredAnnotationBeanPostProcessor is created. Here set of annotations to be scanned for auto wiring dependencies are configured.

/**
 * 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 标记的相应依赖项,并尝试自动连接它们。

Since, by default, the @Inject annotation is configured spring scans respective dependencies marked with @Inject and tries to auto wire them.

要排除 @Inject 带注释的依赖项,请在自定义类下面写。

To exclude @Inject annotated dependencies write below custom 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 hook用于自定义bean post处理器,以便仅包含我们需要自动连接的那些注释。

Here configureBeanPostProcessor hook is utilized to customize the bean post processor so as to include only those annotations which we require to be auto wired.

应用此之后自定义类作为代码中的拦截器可以实现所需的行为

After applying this custom class as interceptor in code the desired behaviour can be achieved

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

@Inject
private EJBClass a;

@Autowired
private SpringComponent b;

}

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

Let know in comments if you face any issues. Also feel free to optimize the code as deemed fit and excuse any compilations / formatting issues.

这篇关于配置Spring以忽略使用@Inject注释的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆