加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

spring – 注释扫描不扫描类路径中的外部jar

发布时间:2020-12-15 01:48:40 所属栏目:大数据 来源:网络整理
导读:问题:Spring Component Annotation扫描没有拾取外部jar中注释的类,该类未包含在pom.xml中.但是我需要从外部jar中扫描具有特定注释的类.这些外部jar将放在类路径中,但在编译期间我的应用程序不会知道. 1)我们有一个maven模块(artifactId =“metric_processor

问题:Spring Component Annotation扫描没有拾取外部jar中注释的类,该类未包含在pom.xml中.但是我需要从外部jar中扫描具有特定注释的类.这些外部jar将放在类路径中,但在编译期间我的应用程序不会知道.

1)我们有一个maven模块(artifactId =“metric_processor”),它生成一个jar文件(metric_processor.jar)并有以下类

package com.metric;
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface ProcessMetric {
    String name();
}

package com.metric;
public interface MetricProcessor {
   int computeMetric();
}

package com.metric;
@ProcessMetric(name="LATENCY")
@Component
public class LatencyMetricProcessor implements MetricProcessor {
    .....
}

2)我们有另一个maven模块(“artifactId =”metric_processor_external“),它生成一个jar(metric_processor_external.jar)并包含”metric_processor“模块作为编译时范围.

package com.metric;
@ProcessMetric(name="TEST_METRIC_EXTERNAL")
@Component
public class TestMetricProcessor implements MetricProcessor {
    ....
}

3)我们有一个第三个(主要)maven模块(artifactId =“main_application”),它是一个独立的应用程序(使用spring),它在编译范围内包含模块“metric_processor”. (但不包括“metric_processor_external”).第三个模块的构建插件是


此模块的应用程序上下文xml是

    

我有以下类,这是应用程序的起点

package com.main;

import ...

public class TriggerMetricProcessor {
    public static void main(String[] args) throws  Exception {
        ApplicationContext context =
                new ClassPathXmlApplicationContext("application-context.xml");

        TriggerMetricProcessor triggerMetricProcessor = (TriggerMetricProcessor) context.getBean("triggerMetricProcessor");
        triggerMetricProcessor.initMetricProcessor(context);
    }

    private void initMetricProcessor(ApplicationContext context) {
        GenericBeanFactoryAccessor beanFactoryAccessor = new GenericBeanFactoryAccessor(context);
        final Map

我们将第三个模块编译为

maven clean install assembly:single 

这会生成jar文件“main_application-with-dependencies.jar”

然后我们运行它

java -cp "metric_process_external.jar" -jar main_application-with-dependencies.jar

现在应用程序只找到“LatencyMetricProcessor”并且找不到“TestMetricProcessor”.

有人可以帮忙吗?

最佳答案
使用-jar选项执行jar文件时,将忽略-cp选项.

-jar选项的Oracle Java docs说:

-jar

Execute a program encapsulated in a JAR file. The first argument is
the name of a JAR file instead of a startup class name. In order for
this option to work,the manifest of the JAR file must contain a line
of the form Main-Class: classname. Here,classname identifies the
class having the public static void main(String[] args) method that
serves as your application’s starting point. See the Jar tool
reference page and the Jar trail of the Java Tutorial for information
about working with Jar files and Jar-file manifests.

When you use this option,the JAR file is the source of all user
classes,and other user class path settings are ignored.

另请查看这篇文章:stackoverflow.com/questions/5879925/in-linux-how-to-execute-java-jar-file-with-external-jar-files

因此,您需要使用Class-Path:标头在清单文件中指定metric_process_external.jar.您应该可以使用Maven程序集插件来执行此操作.

如果这不实用,则需要在没有-jar标志的情况下运行应用程序:

java -cp "metric_process_external.jar:main_application-with-dependencies.jar" com.main.TriggerMetricProcessor

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读