2011-12-04 49 views
3

我似乎無法將簡單的bean注入到我的@WebService中。隨着上定義的類路徑和javax.inject依賴春天,我創建了一個簡單的JAX-WS Web服務與一些底層接口驅動的DAO等:JAX-WS&JSR 330(Spring) - 無法注入依賴關係

@Named 
@WebService(name = "NoteStorage", serviceName = "NoteStorageWS") 
public class NoteStorageWS implements NoteStore { 

    private static final Log l = LogFactory.getLog(NoteStorageWS.class); 

    @Named("NoteDAO") 
    @Inject 
    private NoteDAO noteDAO; 

    public NoteStorageWS() { 
     super(); 
    } 

    @Override 
    @WebMethod 
    public StorageState takeNote(String note) { 
     try { 
      l.info(format("Service received message: '%s'", note)); 

      Note n = new Note(); 
      n.setContent(note); 
      noteDAO.store(n); 

     } catch (Exception e) { 
      l.error(e); 
      return StorageState.FAILURE; 
     } 
     return StorageState.SUCCESS; 
    } 

    @WebMethod(exclude = true) 
    public void setNoteDAO(NoteDAO noteDAO) { 
     this.noteDAO = noteDAO; 
    } 
} 

NoteDAO剛剛實現:FlatFileNoteDAO其定義如下:

@Named("NoteDAO") 
public class FlatFileNoteDAO implements NoteDAO { 

    private static final Log l = LogFactory.getLog(FlatFileNoteDAO.class); 

    @Override 
    public void store(Note n) { 
     if (n == null) { 
      throw new IllegalArgumentException("Note was null"); 
     } 

     try { 
      l.info(format("Storing note '%s'", n)); 
      FileWriter fileWriter = new FileWriter(new File("Note")); 
      fileWriter.write(format("%s\n", n.getContent())); 
      fileWriter.close(); 
     } catch (IOException e) { 
      throw new DataAccessException(e); 
     } 

    } 

} 

我的web.xml中說:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" 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_3_0.xsd"> 
    <welcome-file-list> 
     <welcome-file>index.html</welcome-file> 
    </welcome-file-list> 

    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/context.xml</param-value> 
    </context-param> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <resource-env-ref> 
     <description>Object factory for the CDI Bean Manager</description> 
     <resource-env-ref-name>BeanManager</resource-env-ref-name> 
     <resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type> 
    </resource-env-ref> 
</web-app> 

我通過指向其將應用程序部署到GlassFish到目標/ note-ws /目錄並通過?Tester頁面執行簡單的takeNote方法。

一旦提交測試者表格,我得到在noteDAO.store(n);行,大概是因爲noteDAO沒有被注入。

我可以證實,春天已經上下文初始化(Java EE的上下文中)援引日誌從GlassFish的:

[#|2011-12-04T16:57:24.970+0000|INFO|glassfish3.1.1|org.springframework.context.annotation.ClassPathBeanDefinitionScanner|_ThreadID=256;_ThreadName=Thread-2;|JSR-330 'javax.inject.Named' annotation found and supported for component scanning|#] 

    [#|2011-12-04T16:57:25.653+0000|INFO|glassfish3.1.1|org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor|_ThreadID=256;_ThreadName=Thread-2;|JSR-330 'javax.inject.Inject' annotation found and supported for autowiring|#] 

    [#|2011-12-04T16:57:25.757+0000|INFO|glassfish3.1.1|org.springframework.beans.factory.support.DefaultListableBeanFactory|_ThreadID=256;_ThreadName=Thread-2;|Pre-instantiating singletons in org.s[email protected]9e39146: defining beans [noteStorageWS,NoteDAO,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy|#] 

它說我的豆子被定義爲:noteStorageWSNoteDAO等上。

任何想法?

編輯 爲了澄清,我使用Spring來提供JSR 330 - 依賴注入 - 功能。

回答

1

我從來沒有得到過這個解決方案,所以我最終刪除了DI功能,因爲代碼庫很小,並且依賴於手動依賴關係解析 - 現在只需要簡單的舊版本new IFaceImpl();

0

JAX-WS和Guice需要通過@GuiceManaged註釋進行特定集成。更多信息here

+0

- 你說我應該或者它另外還需要? –

+0

你正在使用哪個JSR-330實現? –

+0

春天,如原來的帖子所述:「春天在類路徑」 –

0

將業務邏輯移至單獨的bean並使用@Configurable對其進行註釋,以便Spring可以處理bean的生命週期。現在使用的bean在NoteStorageWS

@Configurable 
    public class NoteStorageUtil{ 
     @Named("NoteDAO") 
     @Inject 
     private NoteDAO noteDAO; 

     public StorageState takeNote(String note) { 
     try { 
       l.info(format("Service received message: '%s'", note)); 
       Note n = new Note(); 
       n.setContent(note); 
       noteDAO.store(n); 

      } catch (Exception e) { 
       l.error(e); 
       return StorageState.FAILURE; 
      } 
      return StorageState.SUCCESS; 
     } 
    } 

    @WebService(name = "NoteStorage", serviceName = "NoteStorageWS") 
    public class NoteStorageWS implements NoteStore { 
     public StorageState takeNote(String note) { 
      return new NoteStorageUtil().takeNote(note) 
     } 
    } 

或請檢查您的端點配置是正確的,從而使Web服務端點是Spring管理豆也。 如: -

<bean id="hello" class="demo.spring.service.HelloWorldImpl" /> 
    <jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld" /> 

檢查我沒有使用吉斯the link