无法从子包中扫描组件 - Spring Jar

时间:2015-02-24 07:00:28

标签: eclipse spring executable-jar

我正在开发一个使用外部jar依赖的spring命令行应用程序。当我从eclipse运行应用程序时它工作得很好,但是当我将它导出为runnable jar时,它无法从子包中自动装配dependecies。但是,如果我在spring-context中编写每个bean定义,那么它可以工作。它有什么问题? @Component无效或component-scan base package还是其他?

这是Spring-context.xml:

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


    <context:property-placeholder location="classpath*:/connection.properties, classpath*:/log4j.properties" />
    <import resource="classpath*:/sm-service-context.xml" />

    <context:annotation-config />
    <context:component-scan base-package="com.hca.sm.migration" />

    <bean class="com.hca.sm.migration.MigrationController" />

</beans>

更新:ERROR Stacktrace:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.hca.sm.migration.MigrationController#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.hca.sm.migration.soap.util.SOAPMessageUtil com.hca.sm.migration.MigrationController.soapMessageUtil; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hca.sm.migration.soap.util.SOAPMessageUtil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.hca.sm.migration.SMMigrationApp.main(SMMigrationApp.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.hca.sm.migration.soap.util.SOAPMessageUtil com.hca.sm.migration.MigrationController.soapMessageUtil; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hca.sm.migration.soap.util.SOAPMessageUtil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289)
    ... 18 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hca.sm.migration.soap.util.SOAPMessageUtil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1103)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:963)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480)
    ... 20 more
ERROR [main] (SMMigrationApp.java:17) - org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.hca.sm.migration.MigrationController#0': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.hca.sm.migration.soap.util.SOAPMessageUtil com.hca.sm.migration.MigrationController.soapMessageUtil; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hca.sm.migration.soap.util.SOAPMessageUtil] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

MigrationController.java:

package com.hca.sm.migration;

import java.util.List;
import javax.annotation.PostConstruct;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/* other imports */

@Component
public class MigrationController extends AbstractEntityUtil {

    private static final Logger logger = Logger.getLogger(MigrationController.class.getName());

    @Autowired
    protected SOAPMessageUtil soapMessageUtil;

    @Autowired
    protected SOAPGroupUtil soapGroupUtil;

    @Autowired
    protected SOAPMessageThreadUtil soapMessageThreadUtil;

    @Autowired
    protected RestServiceUtil restServiceUtil;

    private SecureMail connectionPort = null;
    private GroupList groupList = null;

    @PostConstruct
    public void init() {
        connectionPort = soapConnectionUtil.getSOAPConnection();
    }

    public void migrateOldSmToNew(String clientId, String dryRun) {
        groupList = soapGroupUtil.getListOfGroupsByClientId(clientId, connectionPort) ;

        /* other logic*/

    }
}

我的主要课程叫做控制器:

package com.hca.sm.migration;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;

@Component
public final class SMMigrationApp {

    public static void main(String[] args) {
        String clientId = "dc=ihcs,dc=com";
        String dryRun = "true";

        try {
            ApplicationContext smApplicationContext = new ClassPathXmlApplicationContext("classpath*:/SM-MigrationApplicationContext.xml");
            // migration
            MigrationController migrationController = (MigrationController) smApplicationContext.getBean(MigrationController.class.getName());  //here is error
            migrationController.migrateOldSmToNew(clientId, dryRun);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

1 个答案:

答案 0 :(得分:0)

OKAY!因此,经过大量的挖掘后,我选择了另一种方法将我的应用程序打包为可运行的jar并且它接缝为我工作。但是我仍然不知道component-scan-base package在这种情况下是不起作用的原因。我假设它必须对maven组装jar和.classpathmenifest.mf的方式做些什么。所以我建议如果有人使用以下插件有这样的问题:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <version>1.2.2.RELEASE</version>
    <executions>
        <execution>
           <goals>
               <goal>repackage</goal>
            </goals>
        </execution>
     </executions>
 </plugin>

这是创建包含所有依赖项的独立可运行jar的最简单方法。

在控制台编译和打包运行maven命令:

mvn clean package

在控制台上运行jar:

java -jar jarName.jar

希望这可以帮助像我这样的人!!!!