springmvc中webservice如何调用业务层。

eternal9015 2017-10-23 04:19:25
使用springmvc,然后创建了webservice,但是在webservice中无法调用springmvc中的service。每次调用都是null,如果分开调用,各自都没有问题。接口也能发布出去。但是用客户端调用一下就出问题了。请问各位大大,如何解决呢?

这个是springmvc的模块,没有问题


这个是webservice的发布的,也没有问题


但是,如果用客户端调用,就出问题了,主要是webservice接口里面调用springmvc的接口为null,就是想知道如何把springmvc的service注入到webservice中。



下面是配置文件。
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">



<!-- 字符串 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>



<!-- 配置springmvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>




<!-- 装饰器 -->
<filter>
<filter-name>sitemesh</filter-name>
<filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>sitemesh</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>




<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-webservice.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置CXF框架的核心Servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>




<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>


springmvc的配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">

<!-- 配置扫描controller的所有注解 -->
<context:component-scan base-package="com.me.**.controller">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>


<!-- 使用自动注册方法 -->
<mvc:annotation-driven>
<mvc:message-converters>
<ref bean="mappingJacksonHttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>


<!--静态资源映射 -->
<mvc:resources location="/statics/" mapping="/statics/**"></mvc:resources>

<!-- 配置jsp显示的ViewResolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<!-- 前缀 -->
<property name="prefix" value="/view/page/" />
<!-- 后缀 -->
<property name="suffix" value=".jsp" />
</bean>

<!-- springmvc文件上传 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="102400000" />
<property name="defaultEncoding" value="UTF-8" />
<property name="resolveLazily" value="true" />
<property name="uploadTempDir" value="fileUpload/temp" />
</bean>

<!-- 针对ie的json问题配置 -->
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>

<import resource="spring-service.xml"/>
<import resource="spring-dao.xml"/>
</beans>

spring-webservice.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"
default-lazy-init="true">


<!--发布webservice -->
<!-- WebService的实现Bean定义 -->
<!--web.xml配置了webservice的访问路径/server/*,那么/server/web-publish?wsdl就是该webservice的访问路径 -->


<bean id="fileBase_WS" class="com.me.FileSys.ws.impl.FileBaseServerImpl" />
<!-- jax-ws endpoint定义 -->
<jaxws:endpoint id="fileserver" implementor="#fileBase_WS" address="/filebase_publish" />

<!--发布webservice -->
<context:component-scan base-package="com.me.**.ws" />

<import resource="spring-service.xml"/>
<import resource="spring-dao.xml"/>

</beans>


spring-service.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
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-4.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">


<context:component-scan base-package="com.me.**.service" />
<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>

<!-- 基于注解的申明方式 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>



spring-dao.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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
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-4.3.xsd">


<!-- 配置数据源文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />

<!-- c3p0连接池的私有属性 -->
<property name="maxPoolSize" value="10" />
<property name="minPoolSize" value="5" />
<!-- 关闭连接后不自动commit -->
<property name="autoCommitOnClose" value="false" />
<!-- 获取连接超时 -->
<property name="checkoutTimeout" value="1000" />
<!-- 连接失败重试次数 -->
<property name="acquireRetryAttempts" value="2" />
</bean>

<!-- 配饰sqlsessionfactory -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
<!-- 注入数据库连接池 ,注意!!!这里是注入,不是value -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置mybatis 全局配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 扫描pojo包 -->
<property name="typeAliasesPackage" value="com.me.**.entity"/>
<!-- 扫描sql配置文件:mapping需要的xml文件 -->
<property name="mapperLocations" value="classpath:mapper/**/*.xml"/>
</bean>


<!-- 配置扫描dao接口的包,并注入到spring中 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlsessionfactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean" />
<!-- 扫描dao包 -->
<property name="basePackage" value="com.**.dao" />
</bean>


</beans>


小弟先在这里谢过了。如果各位谁有源码,能给我借鉴看看也很好,谢谢!
...全文
1464 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
eternal9015 2017-10-23
  • 打赏
  • 举报
回复
哈哈,不好意思,上面这样的配置已经没有问题。 说一下主要的思路吧。springmvc和webservice使用的加载容器不一样。 springmvc是用org.springframework.web.servlet.DispatcherServlet进行加载。那么正常使用springmvc的配置就行。 webservice是用org.apache.cxf.transport.servlet.CXFServlet进行加载的,但是还是要用到org.springframework.web.context.ContextLoaderListener。 所以重点就是,如果要在webservice中进行调用其他的service。那么你要调用的那个service就应该被注入到webservice中。 所以最好把springmvc的配置分成spring-web.xml(主要配置web层)和spring-service-dao.xml(这里配置service层和dao层)。 在webservice.xml(我的名字是spring-webservice.xml)和spring-web.xml中都注入spring-service-dao.xml(service和dao的xml两个配置,你也可以写成一个配置文件,因为service中肯定很多操作会用到dao,所以dao肯定需要)。 然后使用配置就可以了,启动么有问题。

5,655

社区成员

发帖
与我相关
我的任务
社区描述
Web开发应用服务器相关讨论专区
社区管理员
  • 应用服务器社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧