Java 尽管进行了所有扫描,Springboot仍无法找到我的自动连线bean

Java 尽管进行了所有扫描,Springboot仍无法找到我的自动连线bean,java,spring-boot,autowired,Java,Spring Boot,Autowired,因此,我有一个名为ClientFactory的类,我使用jar将它导入到我的项目中。看起来是这样的: @Component public class ClientFactory { @Autowired private Client client; public ClientFactory() { } public Client get() { return this.client; } } 我的类使用这个函数,看起来像:

因此,我有一个名为
ClientFactory
的类,我使用jar将它导入到我的项目中。看起来是这样的:

@Component
public class ClientFactory {

    @Autowired
    private Client client;

    public ClientFactory() {
    }

    public Client get() {
        return this.client;
    }
}
我的类使用这个函数,看起来像:

@SpringBootApplication(scanBasePackages = {"path.to.client.*"})
@EnableAutoConfiguration
public class ProjectClient {

    @Autowired
    public ClientFactory clientFactory;

    public ProjectClient() {}

    public String getSomething(String something){

        Client client = (Client) clientFactory.get();
        return "x";

    }
}

我从测试类中调用这段代码:

@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Disabled
@TestPropertySource(properties = { //
        "some.property=value", //
})
public class SomeTests {

     @Autowired
     ProjectClient p;

     @Test
     public void sampleTest() throws Exception {
            p.getSomething("sample");

     }
}

我得到以下错误:

Field clientFactory in ProjectClient required a bean of type 'ClientFactory' that could not be found.

Action:

Consider defining a bean of type 'ClientFactory' in your configuration.


我尝试了实体、组件和包扫描的所有组合,但似乎没有任何效果。应用程序根本找不到这个bean,我在谷歌上到处搜索,似乎找不到让它工作的方法-我是Spring boot的新手-请帮助:(

看起来你忘了在测试类上添加
@RunWith(SpringRunner.class)

我认为这个类编译不正确

@Component
public class ClientFactory {

    @Autowired
    private Client client;

    public Factory() {

    }

    public Client get() {
        return this.client;
    }
}
当类名为ClientFactory时,构造函数被定义为Factory,编译器无法编译它


请将工厂更改为ClientFactory(如果没有特定用途,请删除默认构造函数),然后重试使用bean注释创建
ClientFactory
,因为您是通过从jar导入来引用
ClientFactory

@Bean
    public Clientfactory clientfactory () {
        return new Clientfactory ();
    }

这花了我太长时间,但我找到了答案。我对
ProjectClient
做了如下更改:

@Service
@ComponentScan(basePackages = {"path.to.*"})
public class ProjectClient {

我猜我到客户端的路径不正确或不可检测(我使用相同的路径导入该类)。

能否显示pom.xml文件1。已使用
@EnableAutoConfiguration
.2进行元注释。
@SpringBootApplication
注释的类理想情况下应该是程序的开始,因此是
main()
方法应该在那里。3.
@ComponentScan
依赖于包结构。请共享所涉及的类的确切包结构和再现问题的最小工作示例,好吗?请在代码段中包含包声明。此外,您的代码没有意义。您的
@SpringBootA应用程序
注释应该在应用程序启动类上,换句话说,是一个带有
main
方法的类。而不是在这样的仲裁类上。
@springbootplication
已经暗示
@EnableAutoConfiguration
@M.Deinum我的主类有该注释,但是如果我不在顶部包含该注释在该类中,它失败了,出现了以下错误:(我删除了
@EnableAutoConfiguration
,只保留了
@ComponentScan
(使用
@SpringBootApplication
并正常工作):
ProjectClient
应该是
@Component
(或类似的东西)您应该更喜欢
scanBasePackages
而不是
@ComponentScan
。很抱歉,我不想公开真正的代码-我会编辑它。据我所知,您在
@SpringBootApplication(scanBasePackages={“path.to.client.*}中不仅将一个包设置为基本包)
。我已尝试使一个具有相同结构和上下文的项目在测试中正确启动。我猜问题中没有足够的信息。由于您无法共享您的项目结构等,我建议您创建一个具有相同结构的小测试项目,并尝试对其运行测试。可能会有帮助。他正在使用JUnit5不需要它,因为JUnit4就是这样。@MaximPopov我不得不对正在扫描的包做一点小小的更改,它成功了!我无法修改
ClientFactory
将其添加到spring boot应用程序的主类中不要修改客户端工厂