我正在尝试将现有的SOAP服务bean配置从XML转换为JAVA。我得到这个奇怪的问题,其中一些bean配置代码被多次执行。它不会影响功能,但在使用xml配置时不会发生这种情况。我在SpringConfig
类中使用的Java配置是:
@Configuration
@ImportResource("classpath:webServices.xml")
public class mySpringConfig {
public static int i = 0;
@Inject
ExceptionNormalizationAdvice exceptionNormalizationAdvice;
@Inject
private RequestHeaderInjectorAdvice requestHeaderInjectorAdvice;
@Inject
private CustomHandlerResolver customHandlerResolver;
@Bean(name = "service1")
public JaxWsPortProxyFactoryBean service1() {
i++;
System.out.println("inside s1 " + i);
JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean();
List<Advice> adviceList = new ArrayList<Advice>();
adviceList.add(exceptionNormalizationAdvice);
adviceList.add(requestHeaderInjectorAdvice);
bean.setAdviceList(adviceList);
bean.setHandlerResolver(customHandlerResolver);
bean.setServiceInterface(service1Soap.class); //
bean.setNamespaceUri("http://example.com/service1");
bean.setWsdlDocumentUrl(this.getClass().getClassLoader().getResource("service1.wsdl"));
bean.setServiceName("service1");
bean.setPortName("service1Soap");
return bean;
}
@Bean(name = "service2")
public JaxWsPortProxyFactoryBean service2() {
i++;
System.out.println("inside s2 " + i);
JaxWsPortProxyFactoryBean bean = new JaxWsPortProxyFactoryBean();
List<Advice> adviceList = new ArrayList<Advice>();
adviceList.add(exceptionNormalizationAdvice);
adviceList.add(requestHeaderInjectorAdvice);
bean.setHandlerResolver(customHandlerResolver);
bean.setAdviceList(adviceList);
bean.setServiceInterface(service2Soap.class); //
bean.setNamespaceUri("http://example.com/servic2");
bean.setWsdlDocumentUrl(this.getClass().getClassLoader()
.getResource("service2.wsdl"));
bean.setServiceName("service2");
bean.setPortName("service2Soap");
return bean;
}
}
当我尝试打印文本并计算时,我得到了#s;&#34; s1&#34;在计数4和8和&#34; s2&#34;内打印2次打印6次,计数范围从1到7,没有4。该服务的相应XML配置是这样的。仅列出了服务1用于示例,服务2使用了类似的配置。
<bean id="service1"
class="JaxWsPortProxyFactoryBean">
<property name="handlerResolver" ref="customHandleResolver" />
<property name="serviceInterface" value="service1Soap" />
<property name="wsdlDocumentUrl" value="classpath:META-INF/wsdls/service1.wsdl" />
<property name="namespaceUri" value="http://example.com/service1" />
<property name="serviceName" value="service1" />
<property name="portName" value="service1Soap" />
<property name="adviceList">
<list>
<ref bean="exceptionNormalizationAdvice"/>
<ref bean="requestHeaderInjectorAdvice"/>
</list>
</property>
</bean>
在服务类中,我使用以下方法注入工厂bean并使用代理与SOAP服务器通信。
@Resource(name="service1")
private service1Soap service1;
service1.callSomeMethod();
web.xml
包含:
<display-name>my-web</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextClass</param-name>
<param-value>
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>com.example.myown.core.CoreSpringConfig</param-value>
</context-param>
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>my-content</param-value>
</context-param>
<servlet>
<servlet-name>spring</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.example.myown.web.WebMvcSpringConfig</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/myweb/*</url-pattern>
</servlet-mapping>
</web-app>
WebMvcSpringConfig
和CoreSpringConfig
都有@ComponentScan
注释,并扫描相同的基础包(com.example.myown
),CoreSpringConfig
使用{WebMvcSpringConfig
排除FilterType.Assignable_Type
{1}}。
我面临的问题是,当我计算s1&#34;内部的次数时,#34;并且&#34;在s2&#34;内它不止一次。我尝试在excludeFilter
内为mySpringConfig
类添加WebMvcSpringConfig
,现在一些冗余事件已经消失但并非全部。只需将mySpringConfig
文件(如上所示)的输出设为:
inside s2 1
inside s2 2
inside s2 3
inside s1 4
如果我将service1()
函数移到下面并将service2()
函数移到顶部,则输出也会与此互换:
inside s1 1
inside s1 2
inside s1 3
inside s2 4
如何更改类中函数的位置会改变输出?理想情况下,两者都应该只打印一次。我尝试在所有地方搜索@ComponentScan
,但它仅用于核心弹簧配置和WebMVCSpringConfig
文件,而不是其他任何地方。我确信bean已初始化并正确配置,因为上述问题不会对功能造成任何问题。
注意:导入webservices.xml
以配置其他休息服务,并在xml文件中注释掉soap服务。