Java 如何在Spring中自动连接依赖项/外部jar中的组件?
我有一个Spring Boot项目,无法从外部jar自动连接组件。当我尝试这样做时,我得到了一个Java 如何在Spring中自动连接依赖项/外部jar中的组件?,java,spring,maven,spring-boot,Java,Spring,Maven,Spring Boot,我有一个Spring Boot项目,无法从外部jar自动连接组件。当我尝试这样做时,我得到了一个org.springframework.beans.factory.NoSuchBeanDefinitionException,表示找不到具有该名称的bean 我尝试了一些类似问题的解决方案,比如: …但还是没能成功 以下是我试图实现的一个示例: 这里是Spring启动项目示例中的启动类 package com.springdi.example; import org.springframe
org.springframework.beans.factory.NoSuchBeanDefinitionException
,表示找不到具有该名称的bean
我尝试了一些类似问题的解决方案,比如:
…但还是没能成功
以下是我试图实现的一个示例:
这里是Spring启动项目示例中的启动类
package com.springdi.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import com.dependency.example.DependencyBasePackageClass;
import com.dependency.example.somepackage.SomeBean;
@SpringBootApplication
@ComponentScan(basePackages = {"com.springdi.example"}, basePackageClasses = DependencyBasePackageClass.class)
public class SpringProjectExampleApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(SpringProjectExampleApplication.class, args);
String beanName = SomeBean.class.getName();
System.out.printf("%s can be autowired: %s\n", beanName, String.valueOf(context.containsBean(beanName)).toUpperCase());
}
}
这只是一个简单的Spring引导项目,检查是否可以自动连接依赖项jar中的组件
下面是jar中的组件(dependency-example-1.0.0.jar)
这是同一个jar的基本包类
package com.dependency.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
/**
* Just a class to serve as the root for component
* scanning in "com.dependency.example" and its sub-packages
*/
@Configuration
@ComponentScan
public class DependencyBasePackageClass {
}
我已经在SpringProjectExampleApplication和@ComponentScan
中尝试了@Import(DependencyBasePackageClass.class)
,但没有成功
我还尝试了使用@SpringBootApplication(scanBasePackageClasses={SpringProjectExampleApplication.class,DependencyBasePackageClass.class})
以及类型不安全的@springboot应用程序(scanBasePackages={“com.springdi.example”,“com.dependency.example”})
@Configuration@ComponentScan({“com.dependency.example})
也会失败,context.containsBean(“com.dependency.example.somepackage.SomeBean”)
仍然返回false
com.springdi.example // class with @SpringBootApplication annotation
|
|
|
com.springdi.example.* // Will find all @Service, @Component, @Configuration
// in subpackages below the @SpringBootApplication
// annotation
这个jar作为依赖项包含在classpath和pom.xml中
<dependencies>
<!-- other dependencies -->
<dependency>
<groupId>com.rbaggio</groupId>
<artifactId>dependency-example</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${basedir}/lib/dependency-example-1.0.0.jar</systemPath>
</dependency>
</dependencies>
com.rbaggio
依赖项示例
1.0.0
系统
${basedir}/lib/dependency-example-1.0.0.jar
可能是jar的位置、包含方式还是需要一些额外的配置
我将感谢任何帮助!提前谢谢。好了,一些基本的东西,你的包裹有点乱了
@SpringBootApplication
将扫描该类注释下的包中的所有类。此注释是@EnableAutoConfiguration
,@Configuration
和@ComponentScan
的别名,表示@ComponentScan(basePackages={“com.springdi.example”},basePackageClasses=DependencyBasePackageClass.class)
不需要
com.springdi.example // class with @SpringBootApplication annotation
|
|
|
com.springdi.example.* // Will find all @Service, @Component, @Configuration
// in subpackages below the @SpringBootApplication
// annotation
您可以在此处阅读有关注释的更多信息
由于其他带注释的类与@SpringBootApplication
不在相同的包结构中,因此需要定义所有要扫描注释的位置
@SpringBootApplication(scanBasePackages = {"com.springdi.example", "com.dependency.example"})
可能会包含您要扫描的所有包。
@ComponentScan
妨碍了查找依赖bean。执行@Import
或创建一个返回依赖项目的bean。我也尝试过,但没有成功。。。applicationContext.containsBean(“com.dependency.example.somepackage.SomeBean”)仍然返回false。我认为问题可能是jar包含在类路径中的方式或者缺少注释。无论如何,谢谢。您需要删除DependencyBasePackageClass中的第二个@ComponentScan
,但它仍然不起作用。。但如果我给@Component(“SomeBean”)这样的bean命名,它会以某种方式工作。似乎我必须命名包含的jar中的每个bean,以便在@autowiredy中使用它们。您可以使用它列出所有加载的bean,它也可以工作。列出所有加载的bean将很有帮助。谢谢你,托马斯!(我会接受你上面的回答)