Spring MVC 框架开发的webService 无法注入mapper,江湖救急

KingSen_Yale 2018-03-19 03:13:58
在原有的Srping mvc 框架中开发了个webService 接口,配置上遇到点问题,一直解决不了,是关于service、mapper的注入问题。
在开发webservice接口之前项目是正常的,加入webservice后,使用webservice的时候就无法找到mapper。

废话不多说,贴代码:

<servlet>
<servlet-name>spring4mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring4mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<!-- spring配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-*.xml</param-value>
</context-param>



<!-- ==============webService 配置 begin============== -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 这里是我的webservice配置文件路径+名称-->
<param-value>/WEB-INF/webservice.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<display-name>CXF Servlet</display-name>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<!-- ==============webService 配置 end============== -->



接下来是webservice配置的代码:

<?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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:cxf="http://cxf.apache.org/core"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<import resource="classpath*:META-INF/cxf.xml" />
<import resource="classpath*:META-INF/cxf-extension-soap.xml" />
<import resource="classpath*:META-INF/cxf-servlet.xml" />
<import resource="config/spring-dao.xml" />




<!-- 业务service -->
<bean id="eccService" class="com.scm.sap.service.impl.EccTableInitServiceImpl" >

</bean>


<jaxws:endpoint id="helloWorld" address="/Hello">
<jaxws:implementor>
<bean class="com.scm.sap.service.impl.WebServiceToSapImpl">
<property name="eccService" ref="eccService"></property>
</bean>
</jaxws:implementor>
</jaxws:endpoint>
</beans>



剩下有个spring-dao.xml 和 spring4mvc-servlet.xml 配置文件都是正常配置,配置数据库已经包的扫描。


接下来看java代码:


@WebService(endpointInterface = "com.scm.sap.service.impl.IWebServiceToSap")
public class WebServiceToSapImpl extends SpringBeanAutowiringSupport implements IWebServiceToSap {

private IEccTableInitService eccService;//这个service在配置文件中注入了,使用接口的时候也不为null

public void setEccService(IEccTableInitService eccService) {
this.eccService = eccService;
}

public IEccTableInitService getEccService() {
return eccService;
}


sevice接口实现类

@Service
public class EccTableInitServiceImpl implements IEccTableInitService {
// @Resource
//就是这里出问题了,这里注入不了,加了标签启动报错,没加resource能正常启动,但是为null
private EccTableInitMapper mapper;


public EccTableInitMapper getMapper() {
return mapper;
}
public void setMapper(EccTableInitMapper mapper) {
this.mapper = mapper;
}


这个mapper一直注入不了,按道理我配置了自动扫描mapper啊,为什么注入不了呢,
网上查资料都很少这方面的问题出现,很无奈啊,有了解的请回答下
...全文
1150 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
flamen087 2018-03-20
  • 打赏
  • 举报
回复
引用 15 楼 KingSen_Yale 的回复:
[quote=引用 14 楼 flamen087 的回复:] [quote=引用 12 楼 KingSen_Yale 的回复:] [quote=引用 9 楼 flamen087 的回复:]

<bean class="com.scm.sap.service.impl.WebServiceToSapImpl">
                <property name="eccService" ref="eccService"></property>
            </bean>

这段丢到spring的applicationcontext里面去,<jaxws:implementor>里面引用spring的bean应该就好了
<jaxws:implementor>里面引用spring的bean什么意思?不要做改动吧? 想把你说的那段代码放入了你说的配置文件里去,爆错: Error creating bean with name 'helloWorld': Cannot create inner bean 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' of type [com.scm.sap.service.impl.WebServiceToSapImpl] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' defined in ServletContext resource [/WEB-INF/webservice.xml]: Cannot resolve reference to bean 'eccService' while setting bean property 'eccService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'eccService' is defined 其中webservice.xml是我webservice的配置文件[/quote] 你把2个xml解析到了2个context里面了。。spring的和webservice相互之间不知道,肯定注不进去。我之前遇到的情况和你这个还不太一样,是由于加载顺序的问题导致的。现在这种情况直接从spring的context里面直接取出来吧。。[/quote] 对对对,我的好像也是加载顺序的问题,所以,哥,具体怎么改,可以留下个email吗,困扰好几天了[/quote] 其实就是在webservice的里面把eccService 的定义去掉,放在spring里面。 然后webservice里面写成<property name="eccService" ref="eccService"></property> 由于我这之前用的接口和你这不太一样,不确定你这个能不能加载到另外一个context里面的内容。我qq发你私信了,要是不行了可以一起研究下
KingSen_Yale 2018-03-20
  • 打赏
  • 举报
回复
KingSen_Yale 2018-03-20
  • 打赏
  • 举报
回复
引用 14 楼 flamen087 的回复:
[quote=引用 12 楼 KingSen_Yale 的回复:] [quote=引用 9 楼 flamen087 的回复:]

<bean class="com.scm.sap.service.impl.WebServiceToSapImpl">
                <property name="eccService" ref="eccService"></property>
            </bean>

这段丢到spring的applicationcontext里面去,<jaxws:implementor>里面引用spring的bean应该就好了
<jaxws:implementor>里面引用spring的bean什么意思?不要做改动吧? 想把你说的那段代码放入了你说的配置文件里去,爆错: Error creating bean with name 'helloWorld': Cannot create inner bean 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' of type [com.scm.sap.service.impl.WebServiceToSapImpl] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' defined in ServletContext resource [/WEB-INF/webservice.xml]: Cannot resolve reference to bean 'eccService' while setting bean property 'eccService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'eccService' is defined 其中webservice.xml是我webservice的配置文件[/quote] 你把2个xml解析到了2个context里面了。。spring的和webservice相互之间不知道,肯定注不进去。我之前遇到的情况和你这个还不太一样,是由于加载顺序的问题导致的。现在这种情况直接从spring的context里面直接取出来吧。。[/quote] 对对对,我的好像也是加载顺序的问题,所以,哥,具体怎么改,可以留下个email吗,困扰好几天了
flamen087 2018-03-20
  • 打赏
  • 举报
回复
引用 12 楼 KingSen_Yale 的回复:
[quote=引用 9 楼 flamen087 的回复:]

<bean class="com.scm.sap.service.impl.WebServiceToSapImpl">
                <property name="eccService" ref="eccService"></property>
            </bean>

这段丢到spring的applicationcontext里面去,<jaxws:implementor>里面引用spring的bean应该就好了
<jaxws:implementor>里面引用spring的bean什么意思?不要做改动吧? 想把你说的那段代码放入了你说的配置文件里去,爆错: Error creating bean with name 'helloWorld': Cannot create inner bean 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' of type [com.scm.sap.service.impl.WebServiceToSapImpl] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' defined in ServletContext resource [/WEB-INF/webservice.xml]: Cannot resolve reference to bean 'eccService' while setting bean property 'eccService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'eccService' is defined 其中webservice.xml是我webservice的配置文件[/quote] 你把2个xml解析到了2个context里面了。。spring的和webservice相互之间不知道,肯定注不进去。我之前遇到的情况和你这个还不太一样,是由于加载顺序的问题导致的。现在这种情况直接从spring的context里面直接取出来吧。。
KingSen_Yale 2018-03-20
  • 打赏
  • 举报
回复
KingSen_Yale 2018-03-20
  • 打赏
  • 举报
回复
我怀疑是jar包问题
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
引用 9 楼 flamen087 的回复:

<bean class="com.scm.sap.service.impl.WebServiceToSapImpl">
                <property name="eccService" ref="eccService"></property>
            </bean>

这段丢到spring的applicationcontext里面去,<jaxws:implementor>里面引用spring的bean应该就好了
<jaxws:implementor>里面引用spring的bean什么意思?不要做改动吧? 想把你说的那段代码放入了你说的配置文件里去,爆错: Error creating bean with name 'helloWorld': Cannot create inner bean 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' of type [com.scm.sap.service.impl.WebServiceToSapImpl] while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.scm.sap.service.impl.WebServiceToSapImpl#4bae62' defined in ServletContext resource [/WEB-INF/webservice.xml]: Cannot resolve reference to bean 'eccService' while setting bean property 'eccService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'eccService' is defined 其中webservice.xml是我webservice的配置文件
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
引用 10 楼 u014205849 的回复:
[quote=引用 8 楼 u014205849 的回复:] <context-param> <!-- spring配置文件路径 --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring-*.xml</param-value> </context-param> <!-- ==============webService 配置 begin============== --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 这里是我的webservice配置文件路径+名称--> <param-value>/WEB-INF/webservice.xml</param-value> </context-param> 两个一样的contextConfigLocation ?
不要跟我说贴上来的忘了注释[/quote] 一个是spring的配置文件啊,另外一个是webservice的配置文件,我帖子的第二段代码就是webservice.xml
IKNOW_jly 2018-03-19
  • 打赏
  • 举报
回复
引用 8 楼 u014205849 的回复:
<context-param> <!-- spring配置文件路径 --> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/spring-*.xml</param-value> </context-param> <!-- ==============webService 配置 begin============== --> <context-param> <param-name>contextConfigLocation</param-name> <!-- 这里是我的webservice配置文件路径+名称--> <param-value>/WEB-INF/webservice.xml</param-value> </context-param> 两个一样的contextConfigLocation ?
不要跟我说贴上来的忘了注释
flamen087 2018-03-19
  • 打赏
  • 举报
回复

<bean class="com.scm.sap.service.impl.WebServiceToSapImpl">
                <property name="eccService" ref="eccService"></property>
            </bean>

这段丢到spring的applicationcontext里面去,<jaxws:implementor>里面引用spring的bean应该就好了
IKNOW_jly 2018-03-19
  • 打赏
  • 举报
回复
<context-param>
<!-- spring配置文件路径 -->
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring-*.xml</param-value>
</context-param>



<!-- ==============webService 配置 begin============== -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 这里是我的webservice配置文件路径+名称-->
<param-value>/WEB-INF/webservice.xml</param-value>
</context-param>
两个一样的contextConfigLocation ?
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
引用 5 楼 u014205849 的回复:
xml中配置了bean,又使用注解实例化Bean?EccTableInitServiceImpl
那个,贴错了代码,我只是2个都试了下,贴代码的时候没注释掉而已
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
引用 4 楼 maijia0754 的回复:
hi,首先,我是个自学不到一年的Java菜鸟,我只能说说我的想法,对于mapper注入不了,我觉得可能的问题有: 1.容器中没有 2.容器中有但是注入时名字不对 3.注入发生在bean初始化并放入容器之前 4.不是同一个容器内,父子容器,父容器获取不到子容器的bean(可能性很小) 如果其他地方可以注入mapper的话,那么容器中是有mapper的,排除掉1, 那么可能是名字不对的问题,你有试过使用@Autowired或者@Resource(name="eccTableInitMapper")吗? 或者直接用代码 ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 2.ac.getBean("beanId"); 如果依旧注入不了,更改load-on-startup参数,设置大一点或者设置成负数试试 我的能力有限,能想到的也就这么多,希望可以帮到你。
哈哈,谢谢,这个是关于webservice配置问题,其实开始项目是好的,只是开发个webservice接口后就不知道怎么配置了, 你说的这些我试过,无果
IKNOW_jly 2018-03-19
  • 打赏
  • 举报
回复
xml中配置了bean,又使用注解实例化Bean?EccTableInitServiceImpl
maijia0754 2018-03-19
  • 打赏
  • 举报
回复
hi,首先,我是个自学不到一年的Java菜鸟,我只能说说我的想法,对于mapper注入不了,我觉得可能的问题有: 1.容器中没有 2.容器中有但是注入时名字不对 3.注入发生在bean初始化并放入容器之前 4.不是同一个容器内,父子容器,父容器获取不到子容器的bean(可能性很小) 如果其他地方可以注入mapper的话,那么容器中是有mapper的,排除掉1, 那么可能是名字不对的问题,你有试过使用@Autowired或者@Resource(name="eccTableInitMapper")吗? 或者直接用代码 ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml"); 2.ac.getBean("beanId"); 如果依旧注入不了,更改load-on-startup参数,设置大一点或者设置成负数试试 我的能力有限,能想到的也就这么多,希望可以帮到你。
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
KingSen_Yale 2018-03-19
  • 打赏
  • 举报
回复
mapper一直注入不了!!!!

81,090

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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