【发布时间】:2017-11-15 11:58:50
【问题描述】:
我创建了一个新的 Spring Boot 项目来学习如何包含我在外部 jar 文件中声明的 bean。我创建了一个名为 ContextApplication 的文件,我要做的就是显示 Spring 扫描过的所有 bean。请注意,此过程中不涉及单个 xml 文件。我喜欢这样,并且更愿意保持这种状态。但是,如果我们必须这样做,我会在这一点上尝试任何事情。
上下文应用:
package com.anypackage.jedcontext;
import java.util.Arrays;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = {"com.tacticalenterprisesltd"})
public class JedcontextApplication {
public static void main(String[] args) {
SpringApplication.run(JedcontextApplication.class, args);
}
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
};
}
}
当我运行它时,它确实可以工作并提供已知 bean 的列表,但是,作为项目的一部分,我引用了一个 jar 文件 jed-1.6.jar,其中有其他类标记为 @Component 或@服务。
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.anypackage</groupId>
<artifactId>jedcontext</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>jedcontext</name>
<description>An application context test</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.tacticalenterprisesltd</groupId>
<artifactId>jed</artifactId>
<version>1.6</version>
<systemPath>C:\Users\Owner\Documents\workspace-sts-3.8.4.RELEASE\JED\jed-1.6.jar</systemPath>
<scope>system</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
现在,当我运行应用程序时,jar 文件中标记的类不会被扫描,也不会显示在控制台的列表中。即使我在 ContextApplication 类上明确提供了这个注释:@ComponentScan(basePackages = {"com.tacticalenterprisesltd"}) 以扫描 jar 文件中的所有内容。这将包括我希望的任何子包。所以,这里的根本问题是,“为什么 Spring 不读取 jar 文件中的类,我该如何让它这样做?”请指教。
【问题讨论】:
-
您的
jed.jar应该是一个 maven 依赖项,而不是随机添加到类路径中。还有那个 jar 是一个普通的 jar 还是另一个用 Spring Boot 创建的 jar。 -
jed-1.6.jar 是我创建的一个库。如上图所示,我尝试在“Maven Dependancies”下添加 jar 文件,但不能。每次我选择 Add Jars 或 Add External Jars 时,jar 文件都会出现在列表的顶部,而不是在 Maven Dependancies 下。
-
当然不会。该 jar 必须在您的 maven 存储库中,并作为
<dependency>添加到您的pom.xml。 -
如何在 Maven 依赖项下获取 jar?
-
就像在
pom.xml中使用<dependency>标记的其他依赖项一样(如我之前的评论中所述)。
标签: java spring-boot