如何从jar导入Java项目进入eclipse?

时间:2016-02-04 09:06:52

标签: java eclipse spring

所以我已经获得了像这样结构化的客户端代码。 client-context.xml是spring bean config。

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  

我得到了一个例外

  

线程中的异常" main" 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的根目录下。我不确定最佳做法是什么。

编辑:我&#39;尝试将此项目用作动态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时,我得到了一个N​​ull指针异常
EnquiryResponse response = (EnquiryResponse) webServiceTemplate.marshalSendAndReceive(epvsEndpoint, request,
                getCallback());

webServiceTemplate为null。此外,clientImpl中的所有字段都为null。所以自动接线是错误的。应该采取什么方式?

1 个答案:

答案 0 :(得分:1)

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

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

通过将文件移动到root(它是类路径的一部分),我能够解决问题。

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