[英]How to import Java project into eclipse from jar?
因此,已經給了我這樣結構的客戶端代碼。 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中創建一個項目。 我還把其他罐子作為依賴項
client
|
+--src
| |
| +--package
| | |
| | +--client.java
| | +--clientImpl.java
| |
| +--main
| | |
| | +--resources
| | | |
| | | +--client-context.xml
| | | +--client.properties
| | | +--keystore.jks
| | | +--request.xml
| |
| +--META-INF
| | |
| | +--MANIFEST.MF
我有一個例外
線程“主”中的異常org.springframework.beans.factory.NoSuchBeanDefinitionException:類型為[package.Client]的合格bean沒有在org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:319)上定義org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:985)在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為null。 同樣,clientImpl中的所有字段均為空。 因此,自動接線是錯誤的。 應該怎么做?
該問題是由無法訪問的client-context.xml
引起的。
指示Spring在類路徑中搜索此文件,但該文件所在的目錄尚未包含在類路徑中。
通過將文件移到根目錄(屬於類路徑),我能夠解決該問題。
編輯:對於我的第二個問題,我跟隨這個家伙設計了帶有Spring建議的Java庫,並創建了一個SpringLoader類,該類加載了上下文並且可以正常工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.