1

I have a problem getting EJB beans working when using Spring's java config classes. Specifically I have the following that works:

@Configuration
@ComponentScan(basePackages = "com.company.web.config")
@ImportResource(value = {"classpath:spring-beans.xml"})
public class AppConfig {
}

@Configuration
@ComponentScan(basePackages = "com.company.web")
public class WebConfig extends WebMvcConfigurationSupport {
   // basic Spring MVC setup omitted
}

My spring-beans.xml looks like this:

<?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:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <jee:local-slsb id="fooService" jndi-name="java:app/model/FooServiceBean!com.company.ejb.FooService"
      business-interface="com.company.ejb.FooService" />
</beans>

With this configuration, everything works, and I can do this:

@Controller
public class HomeController {
    private final FooService fooService;

    @Autowired
    public MyPageController(FooService fooService){
        this.fooService = fooService;
    }

    // request methods
}

Now i try to get rid of the XML file. According to the documentation the local-slsb should be equivalent

<bean id="fooService"
        class="org.springframework.ejb.access.LocalStatelessSessionProxyFactoryBean">
    <property name="jndiName" value="java:app/model/FooServiceBean!com.company.ejb.FooService"/>
    <property name="businessInterface" value="com.company.ejb.FooService"/>
</bean>

However, if I remove the @ImportResource from AppConfig and put this @Bean method instead, deployment fails because the Controller cannot be instantiated (no autowire candidates found for FooService):

@Bean
    public LocalStatelessSessionProxyFactoryBean fooService(){
        LocalStatelessSessionProxyFactoryBean factory = new LocalStatelessSessionProxyFactoryBean();
        factory.setBusinessInterface(FooService.class);
        factory.setJndiName("java:app/model/FooServiceBean!com.company.ejb.FooService");
        return factory;
    }

Any ideas why this doesn't work? I am using Spring version 4.0.2.

4
  • explain doesn't work Commented Mar 3, 2014 at 8:51
  • I already did? Deployment fails because it cannot instantiate my Controller. The exception says no autowire candidates were found for FooService. Commented Mar 3, 2014 at 8:54
  • Which Spring version are you using? Commented Mar 3, 2014 at 10:23
  • I updated the question. I use Spring 4.0.2. Commented Mar 3, 2014 at 10:38

1 Answer 1

2

It seems that the issue was related to the order in which configuration was read, and possibly double configuration loading.

Specifically, introducing a separate configuration class and importing it before WebConfig seems to do the trick, like so:

@Configuration
@Import({EJBConfig.class, WebConfig.class})
public class AppConfig {
}

@Configuration
public class EJBConfig {
    @Bean
    public LocalStatelessSessionProxyFactoryBean fooService(){
        LocalStatelessSessionProxyFactoryBean factory = new LocalStatelessSessionProxyFactoryBean();
        factory.setBusinessInterface(FooService.class);
        factory.setJndiName("java:app/model/FooServiceBean!com.company.ejb.FooService");
        return factory;
    }
}

@Configuration
@ComponentScan(basePackages = "com.company.web")
public class WebConfig extends WebMvcConfigurationSupport {
   // basic Spring MVC setup omitted
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.