Spring Boot 上的@ComponentScan
我无法让 Spring Boot 查找和注入对象。主要方法在Application.java类中,我需要注入另一个分支和子分支中的对象。
如果你看一下图片,我需要注入 pablosz.bot.framework
、pablosz.bot.framewokr.persistentobjects
、z.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.