如何在多servlet webservice中配置Spring bean

时间:2013-08-27 00:39:01

标签: spring web-services servlets jersey javabeans

我有一个带有两个webservices的java项目,第一个工作正常,spring bean是@Autowired并按预期工作。我试图在泽西服务中使用相同的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>

应用context.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>

WebService.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....      
}

1 个答案:

答案 0 :(得分:0)

固定。解决方案是:

  1. 从web.xml中删除
  2. &LT; bean id =“mywebservice”class =“com.mypackage.webservice.Webservice”/&gt;

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

    1. 使用@Controller注释Webservice - 这表明它位于表示层
    2. 使用@Repository注释LocationDao - 表示这是在持久层
    3. 添加了另一个类服务并使用@Service注释 - 表示这是一个服务层。
    4. 请务必在bean的setter上注释@Autowired ...这也会导致空指针错误。
    5. 这改善了我的应用程序的整体流程,并将逻辑分成了清晰的层。