2014-03-03 31 views
1

當使用Spring的java配置類時,EJB Bean工作正常。 具體來說,我有工作如下:Spring java配置EJB代理不工作

@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 
} 

我的春天-beans.xml文件看起來是這樣的:

<?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> 

利用這種配置,一切正常了,我可以這樣做:

@Controller 
public class HomeController { 
    private final FooService fooService; 

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

    // request methods 
} 

現在我試圖擺脫XML文件。據the documentation與本地SLSB應相當於

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

但是,如果我刪除AppConfig@ImportResource,並把這個@Bean方法來代替,部署失敗,因爲控制器不能被實例化(沒有發現自動裝配候選人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; 
    } 

任何想法爲什麼這是行不通的?我正在使用Spring版本4.0.2。

+0

解釋'不work' –

+0

我已經做了?部署失敗,因爲它不能實例化我的控制器。例外情況表示沒有爲FooService找到autowire候選人。 –

+0

您使用的是哪個Spring版本? –

回答

2

看來問題與讀取配置的順序有關,並且可能是雙重配置加載。

具體來說,引入獨立的配置類和WebConfig之前導入似乎這樣的伎倆,像這樣:

@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 
}