我的第一个WebService例子及遇到的问题

 今天在写webservice例子时,遇到一个问题就是在接口实现类中想注入Bean实例,发现不能注入,得到的对象为空。后来经过查找,发现是如果要在webservice接口实现中可以使用注解式注入Bean,与平常写的配置有点不同,代码如下:

@WebService(serviceName="UserService",targetNamespace="http://channel.service.teedry.com",portName="UserServicePort",endpointInterface="com.teedry.webservices.channel.UserService") @Features(features = "org.apache.cxf.feature.LoggingFeature") public class UserServiceImpl implements UserService { @Autowired private UserManager userManager; public List<User> finAllUser() { List<User> list = null; try{ if(userManager == null){ System.out.println("为空对象"); } list = userManager.findAllUser(); }catch (Exception e) { e.printStackTrace(); }finally{ return list; } } @Override public OperatorResult insert(String name, String password) { OperatorResult operatorResult = new OperatorResult(); try{ User user = new User(); user.setName(name); user.setPassword(password); userManager.insertUser(user); operatorResult.setRetCode(1); operatorResult.setRetMsg("操作成功"); }catch (Exception e) { operatorResult.setRetCode(0); operatorResult.setRetMsg("操作失败"); e.printStackTrace(); }finally{ return operatorResult; } } }

刚开始的配置:

<?xml version="1.0" encoding="UTF-8"?> <!-- START SNIPPET: beans --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="userService" address="/UserService" implementor="com.teedry.webservices.channel.UserServiceImpl" /> <!-- END SNIPPET: beans -->

经过运行,发现Bean实体不能注入,后改为如下配置,Bean实例可以注入

<?xml version="1.0" encoding="UTF-8"?> <!-- START SNIPPET: beans --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <jaxws:endpoint id="userService" address="/UserService" > <jaxws:implementor ref="UserService" /> </jaxws:endpoint> <!-- 实现WebService的bean --> <bean id="UserService" class="com.teedry.webservices.channel.UserServiceImpl" /> </beans> <!-- END SNIPPET: beans -->

你可能感兴趣的:(我的第一个WebService例子及遇到的问题)