我创建了一个新的Spring Boot项目,以了解如何在外部jar文件中声明bean。我创建了一个名为ContextApplication的文件,我所要做的就是显示Spring已扫描的所有bean。请注意,此过程中没有涉及单个xml文件。我喜欢这样,宁愿保持这种方式。但是,如果我们必须这样做,我会尝试几乎所有的事情。将bean包含到应用程序上下文中
ContextApplication:
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或@Service。
的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文件中的标记的类不被扫描并没有显示在控制台的列表了。即使我明确提供了这个注释:@ComponentScan(basePackages = {“com.tacticalenterprisesltd”})通过ContextApplication类来扫描jar文件中的所有内容。这将包括我希望的任何子包。因此,这里最基本的问题是,“为什么Spring不是在jar文件的类中读取的,我该如何让它读取?”请指教。
你的'jed.jar'应该是一个mave不依赖任何添加到类路径。这个jar也是一个普通的jar或用Spring Boot创建的另一个jar。 –
jed-1.6.jar是我创建的库。我试图在“Maven Dependancies”下添加jar文件,如上图所示,但不能。每次我选择Add Jars或Add External Jars时,jar文件都会进入列表顶部,而不是在Maven Dependancies下。 – Alan
当然不会。该jar必须存在于你的maven仓库中,并作为''添加到'pom.xml'中。 –