我創建了一個新的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'中。 –