gpt4 book ai didi

java - 将 bean 包含到应用程序上下文中

转载 作者:行者123 更新时间:2023-12-02 12:53:16 26 4
gpt4 key购买 nike

我创建了一个新的 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 或@服务。

enter image description here

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.tropicalenterprisesltd"}) 来扫描 jar 文件中的所有内容。这将包括我希望的任何子包。所以,这里最根本的问题是,“为什么 Spring 不读取 jar 文件中的类以及如何让它这样做?”请指教。

最佳答案

您应该使用includeSystemScope参数将您的jar包含到SpringBoot应用程序的类路径中

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<includeSystemScope>true</includeSystemScope>
</configuration>
</plugin>

或者作为另一个选项,您可以将 jar 安装到本地 Maven 存储库

mvn install:install-file -Dfile=path/to/jed-1.6.jar -DgroupId=com.tacticalenterprisesltd -DartifactId=jed -Dversion=1.6 -Dpackaging=jar

然后将其用作标准的maven依赖

   <dependency>
<groupId>com.tacticalenterprisesltd</groupId>
<artifactId>jed</artifactId>
<version>1.6</version>
</dependency>

关于java - 将 bean 包含到应用程序上下文中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44526024/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com