0

我有一个带有两个 web 服务的 java 项目,第一个工作正常,spring bean 是 @Autowired 并且按预期工作。我尝试在 jersey 服务中使用相同的 bean,并收到来自 DAO 的空指针异常。代码如下,请帮忙!

Web.xml

<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>spring</servlet-name>
     <url-pattern>/sprservice/*</url-pattern>
</servlet-mapping>

<servlet>
  <servlet-name>jersey-services</servlet-name>
  <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
  <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
  <init-param>
      <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
      <param-value>true</param-value>
  </init-param>
  <init-param>
     <param-name>com.sun.jersey.config.property.packages</param-name>
     <param-value>com.mypackage</param-value>
  </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>
<servlet-mapping>
 <servlet-name>jersey-services</servlet-name>
 <url-pattern>/services/*</url-pattern>
</servlet-mapping>

应用程序上下文.xml

<context:annotation-config />
<context:component-scan base-package="com.mypackage"/>
<mvc:annotation-driven/>

<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WEB-INF/jsp/" />
    <property name="suffix" value=".jsp" />
</bean>

<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
    <property name="dataSource" ref="dataSource"/>
</bean>
<bean id="mywebservice" class="com.mypackage.webservice.Webservice">
</bean>

<beans profile="default">
    <jdbc:embedded-database id="dataSource"/>        
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>
</beans>

<beans profile="prod">
    <bean class="java.net.URI" id="dbUrl">
        <constructor-arg value="****"/>
    </bean>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="*****"/>
        <property name="username" value="******"/>
        <property name="password" value="******"/>
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/>
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <!-- change this to 'verify' before running as a production app -->
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.connection.autocommit">false</prop>
            </props>
        </property>
    </bean>
</beans>

服务-beans.xml

<beans>
<import resource="classpath:applicationContext.xml"/>

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

<jaxrs:server id="myContainer" address="/">
    <jaxrs:serviceBeans>
        <ref bean="mywebservice"/>
    </jaxrs:serviceBeans>
</jaxrs:server>

</beans>

网络服务.java

Path("ws")
public class WebService {

private static final Logger logger = LogManager.getLogger("WebService");

@Resource
private LocationDAO locationdao;

   @GET
   @Path("getLocations")
   @Produces({MediaType.APPLICATION_JSON})
   public List<Location> getLocation(){
   logger.info("VA-- GETTING LOCATION");
   if(locationdao == null){
       logger.error("VA-- DAO IS NULL");
   }
   return locationdao.listLocations();  
   }

LocationDAO.java

@Service
public class LocationDAOImpl implements LocationDAO{

private static final Logger logger = LogManager.getLogger("LocationDAOImpl");

@PersistenceContext
EntityManager em;

@Transactional
public void addLocation(Location location) {
    //Do stuff....      
}
4

1 回答 1

0

固定的。解决方案是:

  1. 从 web.xml 中删除

<bean id="mywebservice" class="com.mypackage.webservice.Webservice"/>

此行导致空指针错误,因为我将 XML 与注释混合在一起。我在这里得到了解决方案的想法。

  1. 使用 @Controller 注释 Webservice - 这表明这是在表示层中
  2. 使用 @Repository 注释 LocationDao - 表示这是在持久层中
  3. 添加了另一个类 Service 并用 @Service 注释 - 表示这是一个服务层。
  4. 请务必在 bean 的 setter 上注释 @Autowired ...这也会导致空指针错误。

这改善了我的应用程序的整体流程,并将逻辑分成清晰的层。

于 2013-08-30T04:37:23.170 回答