将服务层暴露为 Web 服务后,服务层注入不起作用

发布于 2024-12-26 02:59:32 字数 3933 浏览 5 评论 0原文

我有一个服务类通过 DAO 类执行数据库操作,我使用 spring 注入 DAO 对象。它运行良好。

之后,我使用 JAX-WS 注释公开了与 Web 服务方法相同的方法。

之后它就不起作用了,它说我的 dao 对象没有被注入,所以我得到了 nullpointerexception。我这样做的方式正确与否?

我的服务类是

package com.tecnotree.upc.services.impl;

import com.tecnotree.upc.services.*;
import com.tecnotree.upc.entities.*;
import com.tecnotree.upc.dao.UpcLineOfBusinessDao;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.WebParam;
import javax.jws.soap.SOAPBinding;

import org.springframework.transaction.annotation.Transactional;
@WebService
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL,
            parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class UpcLineOfBusinessServiceImpl implements UpcLineOfBusinessService {
    private UpcLineOfBusinessDao upcLineOfBusinessDao;

    public void setUpcLineOfBusinessDao(
            UpcLineOfBusinessDao upcLineOfBusinessDao) {
        this.upcLineOfBusinessDao = upcLineOfBusinessDao;
    }

    @Override
    @Transactional
    @WebMethod
    public UpcLineOfBusinessEntity createUpcLineOfBusinessEntity(
            UpcLineOfBusinessEntity upcLineOfBusinessEntity) {
        try{
        if(upcLineOfBusinessDao!=null)
            return upcLineOfBusinessDao.create(upcLineOfBusinessEntity);
        else
            return null;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }


    }

}

我的 spring 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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="upcDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@192.168.4.33:1521:lt33" />
    <property name="username" value="UPC_PROD" />
    <property name="password" value="UPC_PROD" />
</bean>
<bean id="upcSessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="upcDataSource" />
    <property name="mappingResources">
        <list>
            <value>/com/tecnotree/upc/hbmfiles/UpcLineOfBusinessEntity.hbm.xml
            </value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="upcSessionFactory" />
    </property>
</bean>

<tx:annotation-driven />

<bean id="upcLineOfBusinessDao" class="com.tecnotree.upc.dao.impl.UpcLineOfBusinessDaoImpl">
    <property name="sessionFactory">
        <ref bean="upcSessionFactory" />
    </property>
</bean>
<bean id="upcLineOfBusinessService" class="com.tecnotree.upc.services.impl.UpcLineOfBusinessServiceImpl">
    <property name="upcLineOfBusinessDao">
        <ref bean="upcLineOfBusinessDao" />
    </property>
</bean>

I have one service class to do the DB operations through the DAO classes , i inject the DAO object using spring. It is working fine.

After that i exposed the same method as a webservice method using JAX-WS annotation.

After that it is not working it saying my dao object not get injected , so i am getting nullpointerexception. the way i am doing is correct or not?

My service class is

package com.tecnotree.upc.services.impl;

import com.tecnotree.upc.services.*;
import com.tecnotree.upc.entities.*;
import com.tecnotree.upc.dao.UpcLineOfBusinessDao;
import javax.jws.WebMethod;
import javax.jws.WebService;
import javax.jws.WebParam;
import javax.jws.soap.SOAPBinding;

import org.springframework.transaction.annotation.Transactional;
@WebService
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT,use=SOAPBinding.Use.LITERAL,
            parameterStyle=SOAPBinding.ParameterStyle.WRAPPED)
public class UpcLineOfBusinessServiceImpl implements UpcLineOfBusinessService {
    private UpcLineOfBusinessDao upcLineOfBusinessDao;

    public void setUpcLineOfBusinessDao(
            UpcLineOfBusinessDao upcLineOfBusinessDao) {
        this.upcLineOfBusinessDao = upcLineOfBusinessDao;
    }

    @Override
    @Transactional
    @WebMethod
    public UpcLineOfBusinessEntity createUpcLineOfBusinessEntity(
            UpcLineOfBusinessEntity upcLineOfBusinessEntity) {
        try{
        if(upcLineOfBusinessDao!=null)
            return upcLineOfBusinessDao.create(upcLineOfBusinessEntity);
        else
            return null;
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
            return null;
        }


    }

}

My spring xml file is

<?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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="upcDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="oracle.jdbc.OracleDriver" />
    <property name="url" value="jdbc:oracle:thin:@192.168.4.33:1521:lt33" />
    <property name="username" value="UPC_PROD" />
    <property name="password" value="UPC_PROD" />
</bean>
<bean id="upcSessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource" ref="upcDataSource" />
    <property name="mappingResources">
        <list>
            <value>/com/tecnotree/upc/hbmfiles/UpcLineOfBusinessEntity.hbm.xml
            </value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.hbm2ddl.auto">update</prop>
        </props>
    </property>
</bean>
<bean id="transactionManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory">
        <ref bean="upcSessionFactory" />
    </property>
</bean>

<tx:annotation-driven />

<bean id="upcLineOfBusinessDao" class="com.tecnotree.upc.dao.impl.UpcLineOfBusinessDaoImpl">
    <property name="sessionFactory">
        <ref bean="upcSessionFactory" />
    </property>
</bean>
<bean id="upcLineOfBusinessService" class="com.tecnotree.upc.services.impl.UpcLineOfBusinessServiceImpl">
    <property name="upcLineOfBusinessDao">
        <ref bean="upcLineOfBusinessDao" />
    </property>
</bean>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葵雨 2025-01-02 02:59:32

我通过扩展服务类中的 SpringBeanAutowiringSupport 解决了这个问题。现在工作正常了。感谢您的所有评论

I solved this issue by extending the SpringBeanAutowiringSupport in my service class . Now it is working fine . Thank you for all comments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文