【问题标题】:Spring java config EJB proxy not workingSpring java config EJB代理不起作用
【发布时间】:2014-03-03 08:41:30
【问题描述】:

在使用 Spring 的 java 配置类时,我无法让 EJB bean 正常工作。 具体来说,我有以下工作:

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

我的 spring-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,local-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 版。

【问题讨论】:

  • 解释doesn't work
  • 我已经这样做了?部署失败,因为它无法实例化我的控制器。异常说没有找到 FooService 的自动装配候选者。
  • 您使用的是哪个 Spring 版本?
  • 我更新了问题。我使用 Spring 4.0.2。

标签: java spring spring-mvc ejb-3.0


【解决方案1】:

问题似乎与读取配置的顺序有关,并且可能是双重配置加载。

具体来说,引入一个单独的配置类并在 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-02
    • 2017-01-09
    • 2015-06-17
    • 1970-01-01
    • 2012-09-07
    • 2018-01-27
    • 2015-09-20
    • 2016-05-12
    相关资源
    最近更新 更多