Spring Boot 上的@ComponentScan

发布于 2025-01-10 06:17:46 字数 1291 浏览 0 评论 0原文

我无法让 Spring Boot 查找和注入对象。主要方法在Application.java类中,我需要注入另一个分支和子分支中的对象。

如果你看一下图片,我需要注入 pablosz.bot.frameworkpablosz.bot.framewokr.persistentobjectsz.domain 中的对象z.screens 包

我尝试过 @ComponentScan@CompoonentsScan @EntityScan 等。有人可以帮助我吗?

我的应用程序包(参见图片)

这是 Application.java 代码。

package z.futbol;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@ComponentScan({"pablosz.bot.framework"
               ,"pablosz.bot.framework.persistentobjects"
               ,"z.futbol"
               ,"z.futbol.domain"})
@Configuration
public class Application  
{
    public static void main(String[] args)
    {
        try
        {
            SpringApplication.run(Application.class,args);          
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

I can't get Spring Boot to find and inject objects. The main method is in the Application.java class, and I need to inject objects that are in another branch and sub-branches.

If you look at the image, I need to inject objects that are in the pablosz.bot.framework, pablosz.bot.framewokr.persistentobjects, z.domain and z.screens packages.

I have tried @ComponentScan, @CompoonentsScan @EntityScan, etc. Could anyone help me please?

Packages of my app (see the image)

Here is the Application.java code.

package z.futbol;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@ComponentScan({"pablosz.bot.framework"
               ,"pablosz.bot.framework.persistentobjects"
               ,"z.futbol"
               ,"z.futbol.domain"})
@Configuration
public class Application  
{
    public static void main(String[] args)
    {
        try
        {
            SpringApplication.run(Application.class,args);          
        }
        catch(Exception e)
        {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等风来 2025-01-17 06:17:46

Spring Boot 将从您声明主类(声明 @SpringBootApplication 的类)的包(和子包)中执行组件扫描。

您不需要在组件扫描中声明包 z.futbol。您只需添加主包 (pablosz.**) 之外的包。

不要忘记使用 spring 的注释来声明要放入 spring 上下文中的 bean。

您还应该删除无用的@Configuration。

Spring boot will perform a component scan from the packages (and subpackages) where you are declaring your main class (the class where you declare @SpringBootApplication).

You don't need to declare the packages z.futbol in component scan. You only have to add the packages that are outside of your main package (pablosz.**).

Don't forget to declare the beans you want to put in the spring context with the spring's annotations.

You should remove @Configuration also which is useless.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文