2011-12-04 2 views
3

간단한 콩을 @WebService에 삽입 할 수 없습니다. FlatFileNoteDAO 다음과 같이 정의된다 :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 단지 구현이 : 정의 된 클래스 경로 및 javax.inject 종속 봄으로, 나는 몇 가지 기본 인터페이스 기반의 DAO 등 간단한 JAX-WS의 웹 서비스를 만들어

@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> 

나는 그것을 가리켜 글래스 피시에 응용 프로그램을 배포 을 target/note-ws/디렉토리에 복사하고 ?Tester 페이지를 통해 간단한 takeNote 메소드를 실행하십시오.

테스터 양식을 제출하면 noteDAO.store(n);NullPointerException이 표시됩니다. 이는 아마도 noteDAO가 주입되지 않았기 때문일 것입니다.

나는 봄이 컨텍스트 초기화 (자바 EE 컨텍스트)에 글래스 피쉬의 로그에 의해 호출 된 것을 확인할 수 있습니다

내 콩이 정의라고
[#|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|#] 

: noteStorageWS, NoteDAO을 등 -에.

아이디어가 있으십니까?

수정하려면 을 수정하십시오. JSR 330 - 종속성 주입 기능을 제공하기 위해 Spring을 사용하고 있습니다.

답변

1

나는이 코드를 결코 얻지 못했기 때문에 코드 기반이 작고 수동 의존성 해결에 의존하는 DI 기능을 제거했다. 보통은 이전 new IFaceImpl();이다.

0

JAX-WS와 Guice는 @GuiceManaged 주석을 통해 특정 통합이 필요합니다. 더 많은 정보 here.

+0

- 당신은 내가이거나이 추가로 필요하다고한다 말? –

+0

어떤 JSR-330 구현을 사용하고 있습니까? –

+0

원래 게시물에 나온 것처럼 Spring : "클래스 패스의 봄" –

0

비즈니스 로직을 별도의 빈으로 옮기고 @Configurable로 주석을 달아 Spring이 빈의 라이프 사이클을 처리 할 수있게한다. 이제

@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) 
     } 
    } 

NoteStorageWS

에 그 콩을 사용하거나 웹 서비스 엔드 포인트가 스프링 관리 빈은 또한 있도록 엔드 포인트 구성이 적절한 있는지 확인하시기 바랍니다. 예 : -

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

체크 내가 Guice를 사용하지 않는 the link

관련 문제