2014-09-30 118 views
1

工作我使用RESTful Web服務與春天休眠project.I調用一個方法的web服務,但我不能調用@Service層UserService Bean上爲空。在這裏我的web服務類。@Autowired沒有在RESTful Web服務請求

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
import org.springframework.web.context.support.SpringBeanAutowiringSupport; 
import com.ontag.circa.domain.User; 
import com.ontag.circa.service.UserService;`enter code here` 

@Component 
@Path("/users") 
@WebService 
public class UserServiceWS extends SpringBeanAutowiringSupport { 

    @Autowired 
    UserService userService; 

    User user=new User(); 

    @POST 
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
    @Produces(MediaType.APPLICATION_JSON) 
    @WebMethod 
    public String addUserDeatails(MultivaluedMap<String, String> userParams) { 

      user.setUsername(userParams.getFirst("username"));     
      user.setGcmid(userParams.getFirst("gcmid")); 
      user.setPassword(userParams.getFirst("password")); 

      userService.addUserDeatails(user); 

     return "Sucesss !!!!!!!!"; 

    } 
} 

的@Autowired註解不工作,請您及時提供解決方案

這裏我依賴

<dependency> 
     <groupId>junit</groupId> 
     <artifactId>junit</artifactId> 
     <version>3.8.1</version> 
     <scope>test</scope> 
    </dependency> 

     <dependency> 
      <groupId>com.fasterxml.jackson.jaxrs</groupId> 
      <artifactId>jackson-jaxrs-json-provider</artifactId> 
      <version>2.3.2</version> 
     </dependency> 
     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.11</version> 
     </dependency> 


      <dependency> 
       <groupId>com.sun.jersey</groupId> 
       <artifactId>jersey-server</artifactId> 
       <version>1.8</version> 
      </dependency> 
      <dependency> 
       <groupId>com.sun.jersey</groupId> 
       <artifactId>jersey-core</artifactId> 
       <version>1.8</version> 
      </dependency> 


      <dependency> 
       <groupId>com.sun.jersey</groupId> 
       <artifactId>jersey-json</artifactId> 
       <version>1.8</version> 
      </dependency> 



     <dependency> 
      <groupId>log4j</groupId> 
      <artifactId>log4j</artifactId> 
      <version>${log4j.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-jdbc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-web</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.hibernate</groupId> 
      <artifactId>hibernate-entitymanager</artifactId> 
      <version>${hibernate.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-webmvc</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>jstl</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>mysql</groupId> 
      <artifactId>mysql-connector-java</artifactId> 
      <version>5.1.6</version> 
     </dependency> 
     <dependency> 
      <groupId>com.ganyo</groupId> 
      <artifactId>gcm-server</artifactId> 
      <version>1.0.2</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derby</artifactId> 
      <version>10.11.1.1</version> 
     </dependency> 

     <dependency> 
      <groupId>org.apache.derby</groupId> 
      <artifactId>derbyclient</artifactId> 
      <version>10.11.1.1</version> 
     </dependency> 


      <dependency> 
      <groupId>commons-codec</groupId> 
      <artifactId>commons-codec</artifactId> 
      <version>1.7</version> 
     </dependency> 

這裏我的web.xml

<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    version="2.5"> 

    <display-name>Sample Spring Maven Project</display-name> 

    <servlet> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring-config.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>mvc-dispatcher</servlet-name> 
     <url-pattern>/</url-pattern>   
    </servlet-mapping> 

    <servlet> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>com.sun.jersey.config.property.packages</param-name> 
     <param-value>com.ontag.circa.rest</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

這裏彈簧配置。 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:context="http://www.springframework.org/schema/context" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> 


    <context:annotation-config /> 
    <context:component-scan base-package="com.ontag.circa.controller" /> 
    <context:component-scan base-package="com.ontag.circa.service"> 
<!-- <context:include-filter type="annotation" expression="javax.jws.WebService"/> --> 
    </context:component-scan> 
    <context:component-scan base-package="com.ontag.circa.rest"/> 

    <context:property-placeholder location="classpath:database.properties" /> 

    <mvc:annotation-driven /> 

    <bean id="dataSource" 
     class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${database.driver}" /> 
     <property name="url" value="${database.url}" /> 
     <property name="username" value="${database.user}" /> 
     <property name="password" value="${database.password}" /> 
    </bean> 

    <bean id="sessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>com.ontag.circa.domain.User</value> 
       <value>com.ontag.circa.domain.GCM_Message</value> 
       <value>com.ontag.circa.domain.Ad_geo</value> 
       <value>com.ontag.circa.domain.Advertisement</value> 
       <value>com.ontag.circa.domain.Geofence</value> 

      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
       <prop key="hibernate.show_sql">${hibernate.show_sql}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="txManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="sessionFactory" /> 
    </bean> 

    <bean id="persistenceExceptionTranslationPostProcessor" 
     class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 

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

    <bean id="messageDaoImpl" class="com.ontag.circa.dao.MessageDaoImpl" /> 
    <bean id="userDaoImpl" class="com.ontag.circa.dao.UserDaoImpl" /> 
    <bean id="userServiceImpl_1" class="com.ontag.circa.service.UserServiceImpl_1" /> 
    <bean id="userServiceImpl" class="com.ontag.circa.rest.UserServiceWS" /> 
    <bean id="advertisement" class="com.ontag.circa.service.AdvertisementService" /> 
    <bean id="messageService" class="com.ontag.circa.service.MessageService" /> 
</beans> 

////這是我的接口

public interface UserService { 

     public String addUserDeatails(User user); 
     public String getsomevalue(String mess); 

    } 

    // this is my implementation class 
    import javax.transaction.Transactional; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.Transaction; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.stereotype.Repository; 
    import com.ontag.circa.domain.User; 

    @Repository 
    public class UserDaoImpl implements UserDao { 

     @Autowired 
     SessionFactory sessionFactory; 

     @Override 
     @Transactional 
     public String addUserDeatails(User user) 
     { 
      Session session=null; 

        session = sessionFactory.openSession(); 
        Transaction tx = session.beginTransaction();      
        session.saveOrUpdate(user); 
        tx.commit(); 



      return "Succes fully Added the Web service"; 
     } 

     public String getsomevalue(String mess) 
     { 
      System.out.println(mess + " " + mess); 


      return "message"; 
     } 
    } 
+0

它接縫你使用JAX-RS與Spring。您確定您是否正確地遵循了Spring的JAX-RS實現的集成指令? – geoand 2014-09-30 09:19:06

+0

我對JAX-RS沒有什麼想法,我使用JAX-WS。請提供一些指導方針,幫助我解決這個問題。 – Kajanthan 2014-09-30 09:29:41

+1

你可以顯示你正在使用的依賴關係嗎? – geoand 2014-09-30 09:32:38

回答

0

我有這個問題。在檢查了很多東西之後,我發現一個類定義爲一個bean的問題。一些類有一個特殊的註釋或方法,當創建該類的bean時會導致Spring失敗。