Search code examples
javaspringmavenjakarta-eeejb

SpringBeanAutowiringInterceptor throwing NoSuchBeanDefinitionException


I'm trying to autowire beans to EJBs with Spring using the @Interceptors(SpringBeanAutowiringInterceptor.class) annotation. The problem is that I keep on getting the NoSuchBeanDefinitionException for the autowired classes when the EJBs are instantiated. (The Filter class in my example).

I'm using Java EE 6 application (EJB 3.0), Spring 4.2.2 managed with Maven and running in a WebSphere 7 AS.

I have the following Spring dependencies:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.2.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.ldap</groupId>
    <artifactId>spring-ldap-core</artifactId>
    <version>2.0.4.RELEASE</version>
    <exclusions>
        <exclusion>
            <artifactId>spring-tx</artifactId>
            <groupId>org.springframework</groupId>
        </exclusion>
    </exclusions>
</dependency>

Implementation:

@Stateless
@Remote(ServiceRemote.class)
@Interceptors(SpringBeanAutowiringInterceptor.class)
public class Service implements ServiceRemote {

@Autowired
private Filter filter;

...

}

Class to be autowired

@Component
public class FilterImpl implements Filter { ... }

The beanRefContext.xml which SpringBeanAutowiringInterceptor will look for:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

    <bean id="businessBeanFactory"
        class="org.springframework.context.support.ClassPathXmlApplicationContext">
        <constructor-arg value="classpath*:applicaion-context.xml" />
    </bean>
</beans>

The application-context.xml with the bean definitions:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">


    <bean class="com.FilterImpl" />

</beans>

Both XMLs are in the classpath, as they are in src/main/resources/.

I've tried some debugging with the following code, but couldn't find something useful.

BeanFactoryLocator factoryLocator = ContextSingletonBeanFactoryLocator.getInstance("classpath:beanRefContext.xml");

BeanFactoryReference ref = factoryLocator.useBeanFactory("businessBeanFactory");
BeanFactory factory = ref.getFactory();

FilterImpl instance = factory.getBean(FilterImpl.class);

Both contexts appear to be loaded, as I get logs like the following, when the EJB is instantiated for the first time.

org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@7730773; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions Loading XML bean definitions from URL [file:/C:/project/service-module/target/classes/beanRefContext.xml]
org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5580558: root of context hierarchy

But after that Spring can't find the bean I defined in the application-context.xml.

In application-context.xml I've tried using <context:component-scan base-package="com"/> instead of the <bean class="com.FilterImpl" />, but I got the same result.

Am I missing some configuration?

Edits: Added suggestion by Steve C and logs of context loading.


Solution

  • I've just found the answer. In the <constructor-arg value="classpath*:applicaion-context.xml" /> there is an applicaion-context.xml when it should be application-context.xml. When I edited the question and posted the logs about the loading of the contexts, I noticed that only beanRefContext.xml had its definition loaded. There was no logs about some parsing error and I just ignored this possibility. Everything is working now.