gpt4 book ai didi

java - 如何将Java项目从jar导入到eclipse中?

转载 作者:行者123 更新时间:2023-12-01 10:29:05 25 4
gpt4 key购买 nike

所以我得到了这样结构的客户端代码。 client-context.xml 是 spring bean 配置。

client
|
+-- config
| |
| +--client.properties
| +--client-context.xml
| +--keystore.jks
| +--request.xml
|
+-- lib
| |
| +--client.jar
| +--app-model.jar
| +--spring-aop.jar
| +--spring-beans.jar
| +--spring-context.jar
| -- other dependencies--
+--run.bat

我已经解压了 client.jar,因为我需要修改它。

它的结构是这样的

client-1.0
|
+--package
| |
| +--client.class
| +--client.java
| +--clientImpl.class
| +--clientImpl.java
|
+--META-INF
| |
| +--MANIFEST.MF

我尝试在 Eclipse 中创建一个具有以下结构的项目。我还包含其他 jar 作为依赖项

client
|
+--src
| |
| +--package
| | |
| | +--client.java
| | +--clientImpl.java
| |
| +--main
| | |
| | +--resources
| | | |
| | | +--client-context.xml
| | | +--client.properties
| | | +--keystore.jks
| | | +--request.xml
| |
| +--META-INF
| | |
| | +--MANIFEST.MF

我遇到异常

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [package.Client] is defined at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985) at package.ClientImpl.main(ClientImpl.java:164)

这是我的上下文的相关部分

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:client.properties" />
</bean>

<util:properties id="ePSProperties" location="classpath:client.properties" />

<bean id="epsClient" class="package.ClientImpl" />

以及代码的相关部分,异常在 ctx.getBean 处抛出

public static void main(String[] args) {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
"classpath*:client-context.xml");
Client epsClient = ctx.getBean(Client.class);
epsClient.sendReceiveXmls();
epsClient.sendReceiveJavaObjects();
ctx.close();
}

更新:我想通了,我将 context.xml 移至 src 的根目录。但我不确定最佳实践是什么。

编辑:我尝试将此项目用作动态 Web 项目(maven 项目)中的库。我的 client-1.0 项目安装在本地存储库中。以下是我的客户端代码。

public class ClientImpl implements Client {


private static final String WS_ADDRESSING_ACTION = "http://www.example.org";

@Value("#{ePVSProperties['saml.issuer']}")
private String samlIssuer;

@Value("#{ePVSProperties['saml.name.id']}")
private String samlNameId;

@Value("#{ePVSProperties['facility.keystore.location']}")
private String facilityKeystoreLocation;

@Value("#{ePVSProperties['facility.keystore.password']}")
private String facilityKeystorePassword;

@Value("#{ePVSProperties['facility.keystore.type']}")
private String facilityKeystoreType;

@Value("#{ePVSProperties['ssl.debugging']}")
private String sslDebugging;

@Value("#{ePVSProperties['ssl.allow.unsafe.renegotiation']}")
private String allowUnsafeRenegotiation;

@Value("#{ePVSProperties['epvs.endpoint']}")
private String epvsEndpoint;

@Autowired
private WebServiceTemplate webServiceTemplate;

private ClassPathXmlApplicationContext ctx;

public EPVSClientImpl() throws ConfigurationException {
DefaultBootstrap.bootstrap();
if(ctx== null)
{ ctx = new ClassPathXmlApplicationContext("classpath*:client-context.xml");
}
}



@PostConstruct
public void initKeyStore() throws Exception {
System.setProperty("javax.net.ssl.keyStore", facilityKeystoreLocation);
System.setProperty("javax.net.ssl.keyStorePassword", facilityKeystorePassword);
System.setProperty("javax.net.ssl.keyStoreType", facilityKeystoreType);
System.setProperty("javax.net.ssl.trustStore", facilityKeystoreLocation);
System.setProperty("javax.net.ssl.trustStorePassword", facilityKeystorePassword);
System.setProperty("javax.net.ssl.trustStoreType", facilityKeystoreType);
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation", allowUnsafeRenegotiation);
if (sslDebugging != null && !sslDebugging.trim().isEmpty()) {
System.setProperty("javax.net.debug", sslDebugging);
}
}

public EnquiryResponse sendReceiveObjects(EnquiryRequest request) {
try {
System.out.println("Sending Java Object Request...");
EnquiryResponse response = (EnquiryResponse) webServiceTemplate.marshalSendAndReceive(epvsEndpoint, request,
getCallback());
return response;
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}

private WebServiceMessageCallback getCallback() throws URISyntaxException {
return new ActionCallback(new URI(WS_ADDRESSING_ACTION), new Addressing10()) {
public void doWithMessage(WebServiceMessage message) throws IOException, TransformerException {
super.doWithMessage(message);
try {
addSamlToken(message);
} catch (Exception e) {
e.printStackTrace();
}
}

public MessageIdStrategy getMessageIdStrategy() {
return new UuidMessageIdStrategy();
}
};
}

private void addSamlToken(WebServiceMessage message) throws IOException, MarshallingException, TransformerException,
GeneralSecurityException, XMLSignatureException, MarshalException, ParserConfigurationException,
TransformerFactoryConfigurationError, SAXException {
Assertion samlTokenAssertion = createSamlAssertion();
Document samlTokenDocument = convertXMLObjectToDocument(samlTokenAssertion);
Element samlTokenRootElement = samlTokenDocument.getDocumentElement();
createSamlTokenSoapHeader(message, samlTokenRootElement);
}

private Assertion createSamlAssertion() throws NoSuchAlgorithmException {
DateTime now = new DateTime();

Issuer issuer = create(Issuer.class, Issuer.DEFAULT_ELEMENT_NAME);
issuer.setValue(samlIssuer);

NameID nameID = create(NameID.class, NameID.DEFAULT_ELEMENT_NAME);
nameID.setFormat(NameID.EMAIL);
nameID.setValue(samlNameId);

Subject subject = create(Subject.class, Subject.DEFAULT_ELEMENT_NAME);
subject.setNameID(nameID);

Assertion assertion = create(Assertion.class, Assertion.DEFAULT_ELEMENT_NAME);
assertion.setID(new SecureRandomIdentifierGenerator().generateIdentifier());
assertion.setIssueInstant(now);
assertion.setIssuer(issuer);
assertion.setSubject(subject);

return assertion;
}

@SuppressWarnings("unchecked")
private <T> T create(Class<T> cls, QName qname) {
return (T) ((XMLObjectBuilder<?>) Configuration.getBuilderFactory().getBuilder(qname)).buildObject(qname);
}

private void createSamlTokenSoapHeader(WebServiceMessage message, Element samlToken)
throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
SaajSoapMessage axiomMessage = (SaajSoapMessage) message;
Source source = new DOMSource(samlToken);
SoapHeader soapHeader = ((SoapMessage) axiomMessage).getSoapHeader();
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(source, soapHeader.getResult());
}

private Document convertXMLObjectToDocument(XMLObject object)
throws IOException, MarshallingException, TransformerException, ParserConfigurationException {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
documentBuilderFactory.setNamespaceAware(true);

DocumentBuilder documentBuilder = null;
documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document document = documentBuilder.newDocument();
Marshaller marshaller = Configuration.getMarshallerFactory().getMarshaller(object);
marshaller.marshall(object, document);

return document;
}

这是我的上下文文件

<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<context:component-scan base-package="package" />

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="file*:/src/epvs-client.properties" />
</bean>

<util:properties id="ePVSProperties" location="file*:/src/client.properties" />

<bean id="epvsClient" class="package.ClientImpl" />

<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate">
<constructor-arg ref="messageFactory" />
<property name="marshaller" ref="jaxb2Marshaller" />
<property name="unmarshaller" ref="jaxb2Marshaller" />
<property name="messageSender">
<bean class="org.springframework.ws.transport.http.CommonsHttpMessageSender" />
</property>
<property name="interceptors">
<list>
<ref bean="securityInterceptor" />
</list>
</property>
</bean>

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="messageFactory">
<bean class="com.sun.xml.messaging.saaj.soap.ver1_1.SOAPMessageFactory1_1Impl" />
</property>
</bean>

<bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>package.webservice.dom.external.v1</value>
</list>
</property>
</bean>

<bean id="securityInterceptor" class="org.springframework.ws.soap.security.wss4j.Wss4jSecurityInterceptor">
<property name="securementActions" value="Signature Encrypt" />
<property name="securementEncryptionCrypto" ref="keyStore" />
<property name="securementEncryptionUser" value="${server.certificate.alias}" />
<property name="securementEncryptionSymAlgorithm">
<util:constant static-field="org.apache.ws.security.WSConstants.TRIPLE_DES" />
</property>
<property name="securementSignatureKeyIdentifier" value="DirectReference" />
<property name="securementSignatureCrypto" ref="keyStore" />
<property name="securementSignatureParts" value="{}{http://schemas.xmlsoap.org/soap/envelope/}Body;{}{urn:oasis:names:tc:SAML:2.0:assertion}Assertion" />
<property name="securementUsername" value="${facility.key.pair.alias}" />
<property name="securementPassword" value="${facility.private.key.password}" />
<property name="validationActions" value="Encrypt Signature" />
<property name="validationSignatureCrypto" ref="keyStore" />
<property name="validationDecryptionCrypto" ref="keyStore" />
<property name="validationCallbackHandler">
<bean class="org.springframework.ws.soap.security.wss4j.callback.KeyStoreCallbackHandler">
<property name="privateKeyPassword" value="${facility.private.key.password}" />
</bean>
</property>
</bean>

<bean id="keyStore" class="org.springframework.ws.soap.security.wss4j.support.CryptoFactoryBean">
<property name="keyStoreLocation" value="file:${facility.keystore.location}" />
<property name="keyStorePassword" value="${facility.keystore.password}" />
<property name="keyStoreType" value="${facility.keystore.type}" />
</bean>

我通过创建客户端实例来调用该库。

ClientImpl clientImpl = new ClientImpl();

但是当我在

调用 sendReceiveObject 时遇到空指针异常
EnquiryResponse response = (EnquiryResponse) webServiceTemplate.marshalSendAndReceive(epvsEndpoint, request,
getCallback());

webServiceTemplate 为空。 clientImpl 中的所有字段也均为空。所以自动接线是错误的。应该采取什么方式?

最佳答案

该问题是由无法访问的 client-context.xml 引起的。

指示 Spring 在类路径中搜索该文件,但该文件所在的目录尚未包含在类路径中。

通过将文件移动到根目录(这是类路径的一部分),我能够解决这个问题。

编辑:对于我的第二个问题,我关注了这些人 Designing a Java library with Spring 建议并创建了一个 SpringLoader 类来加载上下文并且它可以工作。

关于java - 如何将Java项目从jar导入到eclipse中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196720/

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