Configuring Spring to ignore dependencies annotated with @Inject

I have an EJB class in which I need to inject two beans - one must be entered into the EJB container, and the other by the Spring container.

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

    @Inject
    private EJBClass a;

    @Autowired
    private SpringComponent b;

}

Here, the Spring interceptor tries to intercept the bean 'a' injection, and it becomes unsuccessful. I want the EJB container to enter the bean 'a' container and Spring to enter the bean 'b'.

Please show me the way out.

+4
source share
2 answers

When you configure a SpringBeanAutowiringInterceptordependency class , annotated with @Inject, can be excluded from automatic posting.

, , 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 standard {@link Autowired} annotation.
 * <p>Also supports JSR-330 {@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 , .

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, , .

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

@Inject
private EJBClass a;

@Autowired
private SpringComponent b;

}

, - . /.

+2

@EJB EJB

-1

All Articles