springmvc,mybatis,cxf整合遇到的bean无法注入的问题

web.xml配置如下:

    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                /WEB-INF/spring-mvc.xml
            </param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

 <!-- 配置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>/webservice/*</url-pattern>
    </servlet-mapping>
    
 <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
               /WEB-INF/spring*.xml
        </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 

spring*.xml加载所有spring开头的配置文件,包括spring-mybatis,spring-cxf

spring-cxf.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"
     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.xsd"
    default-lazy-init="true"> 
     <!-- 引入CXF Bean定义如下,早期的版本中使用 。 -->
     <!-- 3.0以下都需要引入这三个文件,3.0以上只需要cxf.xml这一个xml文件就好 --> 
        <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" />-->
     <!-- WebService的实现Bean定义 -->
     <bean id="trainxcService" class="com.framework.code.webservice.TrainXcServiceImpl" /> 
     <jaxws:endpoint id="myService" implementor="#trainxcService"
         address="/TrainXcService">
     </jaxws:endpoint> 
 </beans>

webservice接口:

@Service
@Transactional
@WebService(endpointInterface="com.framework.code.webservice.TrainXcService")
public class TrainXcServiceImpl extends SpringBeanAutowiringSupport  implements TrainXcService {    
    
    @Autowired
    TrainXiuchengMapper trainXiuchengMapper;
    @Autowired
    TrainMapper trainMapper;
    @Autowired
    SysDictionaryMapper sysDictionaryMapper;
    
    @PostConstruct
    public void init() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    
    @Override
    public JsonResult insert(String traintype, String trainnum,
            String xiucheng,String username, boolean isJiaoche) {    
      

    //未继承SpringBeanAutowiringSupport,执行init()方法前需要用Context去获取bean。无法Autowired获取
    // if(trainXiuchengMapper==null){
    //    WebApplicationContext context = ContextLoader.getCurrentWebApplicationContext();
    //    trainXiuchengMapper=(TrainXiuchengMapper)context.getBean("trainXiuchengMapper");
    //    }
        
        Train train = trainMapper.selectByTypeAndNum(traintype, trainnum);
        TrainXiucheng record = new TrainXiucheng();
        record.setId(IdUtil.getUUID());
        record.setFromdate(new Date());
        record.setTrainid(train.getTrainid());
        record.setTrainnum(trainnum);
        SysDictionary dic = sysDictionaryMapper.selectByText(xiucheng);
        record.setXiuchengid(dic.getId());
        record.setXiuchengname(xiucheng);
        trainXiuchengMapper.insert(record);
        return JsonResult.SUCCESS;
    }

最后测试

public class TestWeb {

    public static void main(String[] args) {
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://192.168.80.1:8080/HHHTComponentTrace/webservice/TrainXcService?wsdl");
        Object[] obj = {"HXD3B","0102","C3","",false};
        try {
            Object[] result = client.invoke("insert", obj);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

大功告成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值