gpt4 book ai didi

java - 在 Jax-Ws 中使用 @Autowire 和 Spring 容器

转载 作者:行者123 更新时间:2023-12-01 06:09:06 24 4
gpt4 key购买 nike

我有一个带有 Spring 容器的 Jax-Ws Metro 项目。我无法 Autowiring SoapHandler 内的字段。我尝试了互联网资源中的所有内容,但没有成功。该字段始终为空。字段名称为“paymentPortalService”。

我的处理程序类:

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

@Autowire
private PaymentPortalService paymentPortalService;
}

我有一个调用远程服务的代理服务。在远程服务调用方法中将此处理程序添加到处理程序链中:

@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以及互联网上的其他一些解决方案,但没有成功。我使用的是 Apache Tomcat 8,没有 EE 容器。

提前致谢!

最佳答案

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>

关于java - 在 Jax-Ws 中使用 @Autowire 和 Spring 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38767811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com