从Spring4.xxx迁移到Spring5,保留EJB和Web模块 [英] Move from Spring 4.xxx to Spring 5 keeping EJB and web modules
本文介绍了从Spring4.xxx迁移到Spring5,保留EJB和Web模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好
服务器:WebSphere 9。
应用程序在Spring 4上运行良好。我们希望移到Spring 5。
我们遇到了DI(依赖项注入)问题。
我们在Spring 4中有EJB模块(带消息驱动Bean)和Web模块。
我已经知道他们建议完全迁移到Spring或使用一些Spring CDI-Bridge。 https://jira.spring.io/browse/SPR-15154
但我们希望避免更改项目的太多结构(回归测试、少量时间等)。
那么有没有办法保持结构并迁移到Spring 5。
web.xml
<web-app id="WebAppId" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name>AppWebModule</display-name>
<context-param>
<param-name>parentContextKey</param-name>
<param-value>ear.context</param-value>
</context-param>
<context-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>com.sun.faces.config.ConfigureListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Start Spring MVC -->
<servlet>
<servlet-name>Spring MVC Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextClass</param-name>
<param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
</init-param>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.organization.services.config.ServicesWebAppConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
... rest of file
ServicesAppConfigEJB模块。
@Configuration
@EnableAspectJAutoProxy
@EnableTransactionManagement(proxyTargetClass = true)
@ComponentScan({ "com.organization.services.resources", "com.organizatio.services.jsf", "com.organizatio.services.snoop", "com.organizatio.services.version" })
public class ServicesWebAppConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
super.addDefaultHttpMessageConverters(converters); // Have to call this explicitly to register Default Message Converters.
converters.add(new MappingJackson2HttpMessageConverter());
}
}
beanRefContext。XML
"bean id='ear.context'
class='org.spring.....AnnotationConfigApplicationContext
constructor-arg
list
type="java.lang.Class">com.organization.services.config.ServicesAppConfig
list
constructor-arg
错误:
ERROR
[6/18/18 12:30:57:980 CEST] 0000012c SystemOut O [2018-06-18 12:30:57,976]
[WebContainer : 5] [ERROR] []
[org.springframework.web.servlet.DispatcherServlet:initServletBean:503]:
Context initialization failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error
creating bean with name 'adminResource1': Unsatisfied dependency expressed
through field 'adminService'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying
bean of type 'com.organiazation.services.business.admin.xxxx.AdminService'
available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@javax.inject.Inject()}
Spring
以前的解决方案不再适用于SpringBeanAutowiringInterceptor5。推荐答案支持已删除。
下面是我的快速解决方案:
我使用setter在EJB(这里是MDB)中手动注入Spring bean。在Spring的初始化阶段,我的Bean&Injected&是静电Bean(一次性设置)。
这是我的EJB:
import javax.ejb.*;
import javax.jms.*;
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(value = TransactionAttributeType.NOT_SUPPORTED)
@MessageDriven(
name = "messageListenerEJBInstance",
activationConfig = {
// configurations ...
})
public class MessageListenerEJB implements MessageListener {
// ...
private static SpringBean springBean;
@Override
public void onMessage(Message message) {
// ...
springBean.someMethod();
// ...
}
// ...
// Setter -> to initialize my bean from the outside
public void setSpringBean(SpringBean springBean) {
MessageListenerEJB.springBean = springBean;
}
}
这里是初始化方式:
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc" 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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">
// ...
<bean id="bean" class="your.package.SpringBean"/>
<bean id="messageListenerEJBInstance" class="your.package.MessageListenerEJB">
<property name="springBean" ref="bean"/>
</bean>
// ...
</beans>
这篇关于从Spring4.xxx迁移到Spring5,保留EJB和Web模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文