2016-08-04 122 views
1

我有一個使用Spring容器的Jax-Ws Metro項目。我無法在SoapHandler內自動裝入字段。我嘗試了一切從互聯網資源,但沒有成功。該字段始終爲空。字段名稱是「paymentPortalService」。在Spring容器中使用@Autowire Jax-Ws

我的處理程序類:

public class CustomSoapHeaderHandler extends SpringBeanAutowiringSupport implements SOAPHandler<SOAPMessageContext> { 
    private static final Logger log = Logger.getLogger(CustomSoapHeaderHandler.class); 

    @Autowire 
    private PaymentPortalService paymentPortalService; 
} 

我有調用遠程服務的代理服務。添加了這個處理程序在調用遠程服務方法HandlerChain的:

@Service 
public class CustomServiceProxy { 
    private final Logger log = Logger.getLogger(CustomServiceProxy.class); 
    private RemoteServicePort pspPort; 


    public CustomServiceProxy() { 
     try { 


     pspPort = new RemoteService_Service(new URL("https://x?wsdl")).getRemoteServicePort(); 

     Binding binding = ((BindingProvider) pspPort).getBinding(); 
     List<Handler> handlers = binding.getHandlerChain(); 
     handlers.add(new CustomSoapHeaderHandler()); 
     binding.setHandlerChain(handlers); 
    } 
} 

我試圖改變@Autowire到@Resource和從互聯網上一些其他的解決方案,沒有成功。我使用沒有EE容器的Apache Tomcat 8。

提前致謝!

+0

而爲什麼它不應該是'null'。你正在自己創建'CustomSoapHeaderHandler'的實例。它不是由任何種類的容器管理的,無論是球衣還是春天。所以什麼都不會注入。我建議閱讀球衣文檔,而不是解釋如何使用Spring設置球衣。 –

+0

感謝您的回覆!是的,我知道這是一個愚蠢的錯誤,我將它改爲: Autowired private CustomSoapHeaderHandler customSoapHeaderHandler; 但它仍然無法正常工作。處理程序對象爲null。我還將Component註釋添加到CustomSoapHeaderHandler類。 – 0bj3ct

+0

正如前面提到的,閱讀[文檔](https://jersey.java.net/documentation/latest/spring.html)而不是嘗試錯誤的東西。接下來你的'SoapHandler'需要是@ @ Component',我假設你已經設置了組件掃描。 (請刪除擴展'SpringBeanAutowiringSupport',因爲這不會增加任何內容 –

回答

-1
Add this jar from ivy 

<dependency org="org.springframework" name="spring-aop" rev="4.2.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-beans" rev="4.2.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-context" rev="4.2.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-core" rev="4.2.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-expression" rev="4.1.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-web" rev="4.2.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-webmvc" rev="4.2.1.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-jdbc" rev="4.2.5.RELEASE" transitive="false"/> 
<dependency org="org.springframework" name="spring-tx" rev="4.2.5.RELEASE" transitive="false"/> 
<dependency org="com.fasterxml.jackson.core" name="jackson-annotations" rev="2.7.4" transitive="false"/> 
<dependency org="com.fasterxml.jackson.core" name="jackson-core" rev="2.7.4" transitive="false"/> 
<dependency org="com.fasterxml.jackson.core" name="jackson-databind" rev="2.7.4" transitive="false"/> 
<dependency org="mysql" name="mysql-connector-java" rev="5.1.39" transitive="false"/> 
<dependency org="org.apache.commons" name="commons-dbcp2" rev="2.1.1" transitive="false"/> 
<dependency org="org.apache.commons" name="commons-pool2" rev="2.4.2" transitive="false"/> 

A response object bean looks like 

@Component 
public class ResponseObjectBean { 
    @JsonProperty("code") 
    private int statusCode; 
    @JsonProperty("msg") 
    private String statusMsg; 
    @JsonProperty("data") 
    private Object data; 

    //getter and setter 
} 

A log in controller 

@RestController 
public class LogInController { 

    @Autowired 
    ResponseObjectBean responseData; 

    @Autowired 
    LogInService logInService; 

    @RequestMapping(value = "/login", method = RequestMethod.POST) 

    public ResponseEntity<ResponseObjectBean> userLogIn(@RequestBody UserInfoBean userInfoBean) { 
     Object data = null; 
     String msg = null; 
     int statusCode; 
     try { 
      data = logInService.logIn(userInfoBean); 

      if (data != null) { 
       msg = "success"; 
       statusCode = 200; 
      } else { 
       msg = "fail"; 
       statusCode = 503; 
      } 
     } catch (DataAccessException e) { 

       msg = "fail"; 
       statusCode = 503; 
     } catch (Exception e) { 

       msg = "fail"; 
       statusCode = 503; 
      e.printStackTrace(); 
     } 

     responseData.setStatusCode(statusCode); 
     responseData.setStatusMsg(msg); 
     responseData.setData(data); 
     return new ResponseEntity<ResponseObjectBean>(responseData, HttpStatus.OK); 

    } 

} 


A log in Service class looks like 

@Service 
@Transactional 
public class LogInService { 

    @Autowired 
    LogInDao logInDao; 

    public UserInfoBean logIn(UserInfoBean userInfoBean) throws DataAccessException { 
     return logInDao.upSertUser(userInfoBean); 

    } 

} 


A log in dao class 
@Repository 
public class LogInDao { 

    @Autowired 
    private JdbcTemplate jdbcTemplate; 

    public UserInfoBean upSertUser(UserInfoBean userInfoBean) throws Exception { 
     StringBuilder sql = new StringBuilder(); 
     int count; 
     UserInfoBean infoBean = null; 

     try { 
      // your logic for db operation 
       return infoBean; 
      } 
     } catch (Exception e) { 
      throw new Exception(e); 
     } finally { 
      sql = null; 
      infoBean = null; 
      run = null; 
     } 

    } 

} 

for use jdbc templete add these line in your spring.xml 

    <!-- jdbcTemplate uses dataSource --> 
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 
     <property name="dataSource" ref="dataSource" /> 
    </bean> 

    <!-- dataSource configuration --> 
    <bean id="dataSource" destroy-method="close" 
     class="org.apache.commons.dbcp2.BasicDataSource"> 
     <property name="driverClassName" value="your driverClassName" /> 
     <property name="url" value="your url" /> 
     <property name="username" value="db username" /> 
     <property name="password" value="db password" /> 
    </bean>