为什么我不能从Spring Boot库中导入服务或组件?

huangapple go评论61阅读模式
英文:

Why I can't import a service or a component from a library in Springboot?

问题

I created a "starter project" that I use for storing common dtos, custom exceptions, and classes and use it as a dependency for all my project so that when I update a class I don't have to update every project.

So I just build the jar and use it in the project that I need those classes like this:

implementation files('../my-library/jar/my-library-1.0.jar')

The issue is thought that when I try to use a service or a component from this library I get an error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nob234.mylibrary.services.MyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801) ~[spring-beans-5.3.25.jar:5.3.25]

Here is my service in the library:

@Service
@AllArgsConstructor
public class MyService {
    public String addSomething(String string){
        return string + " something";
    }
}

And here I tried to inject it in another service:

@Service
@AllArgsConstructor
public class AnotherService {

    private final MyService myService;

    public String printHelloSomething() {
      return myService.addSomething("Hello");
    }
}

I thought maybe there is a missing constructor or something, but I think there is an issue with the context of the Spring @Service / @Component annotation. How can I fix this?

英文:

I created a "starter project" that I use for storing common dtos, custom exceptions, and classes and use it as a dependency for all my project so that when I update a class I don't have to update every project.

So I just build the jar and use it in the project that I need those classes like this:

implementation files('../my-library/jar/my-library-1.0.jar')

The issue is thought that when I try to use a service or a component from this library I get an error:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nob234.mylibrary.services.MyService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1801) ~[spring-beans-5.3.25.jar:5.3.25]

Here is my service in the library:

@Service
@AllArgsConstructor
public class MyService {
    public String addSomething(String string){
        return string + " something";
    }
}

And here I tried to inject it in another service:

@Service
@AllArgsConstructor
public class AnotherService {

    private final MyService myService;

    public String printHelloSomething() {
      return myService.addSomething("Hello");
    }
}

I thought maybe there is a missing constructor or something, but I think there is an issue with the context of the Spring @Service / @Component annotation. How can I fix this?

答案1

得分: 0

以下是翻译好的部分:

已修复:

当我将jar文件包含在我的项目中时 implementation files('../my-library/jar/my-library-1.0.jar'), 我的项目中的Spring上下文不知道jar文件中定义的bean,因此无法找到并注入它们。

为了解决这个问题,我确保Spring扫描我的库中的包以创建组件的bean,并使用@ComponentScan

@Configuration
@ComponentScan(basePackages = "com.nob234.mylibrary")
public class MyLibraryConfiguration {
}

@ComponentScan注释告诉Spring扫描"com.nob234.mylibrary"包及其子包中的组件以创建bean。

然后,在主项目中,我导入了这个配置类:

@Configuration
@Import(MyLibraryConfiguration.class)
public class MyAppConfiguration {
}

这将确保Spring扫描库中的包并为其中定义的组件创建bean。

注意: 如果您已经在库中使用配置类,您可以将@ComponentScan注释添加到该类而不是创建新的配置类。

英文:

Fixed it:

When I included the jar file in my project implementation files('../my-library/jar/my-library-1.0.jar'), the Spring context in my project was not aware of the beans defined in the jar file, so it was not able to find and inject them.

To fix this issue, I made sure that Spring scans the packages in my library for components and creates beans for them with @ComponentScan.

@Configuration
@ComponentScan(basePackages = "com.nob234.mylibrary")
public class MyLibraryConfiguration {
}

@ComponentScan annotation tells Spring to scan the "com.nob234.mylibrary" package and all its sub-packages for components to create beans for.

Then, in the main project, I imported this configuration class:

@Configuration
@Import(MyLibraryConfiguration.class)
public class MyAppConfiguration {
}

This will ensure that Spring scans the packages in the library and creates beans for the components defined in it.

Note: If you're already using a configuration class in your library, you can add the @ComponentScan annotation to that class instead of creating a new configuration class.

huangapple
  • 本文由 发表于 2023年4月13日 16:24:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003247.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定