gpt4 book ai didi

java - Spring java配置EJB代理不工作

转载 作者:行者123 更新时间:2023-11-30 04:00:17 24 4
gpt4 key购买 nike

在使用 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 方法,则部署会失败,因为无法实例化 Controller (没有找到 FooService 的 Autowiring 候选项):

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

最佳答案

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

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

关于java - Spring java配置EJB代理不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22142107/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com