Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Autowire处的弹簧启动不工作。创建bean时出错_Java_Spring Boot_Autowired - Fatal编程技术网

Java Autowire处的弹簧启动不工作。创建bean时出错

Java Autowire处的弹簧启动不工作。创建bean时出错,java,spring-boot,autowired,Java,Spring Boot,Autowired,嗨,我有一个spring boot项目。主类包如下所示: com.som.demo 我在项目中使用了一个外部jar作为maven依赖项。在该jar中有一个用@service注释的服务类,如下所示: @Service public class SomeServiceImpl implements SomeService { @Autowired private TreeCacheWrapper ffCoreCache; @Autowired(required

嗨,我有一个spring boot项目。主类包如下所示:

com.som.demo
我在项目中使用了一个外部jar作为maven依赖项。在该jar中有一个用@service注释的服务类,如下所示:

@Service
public class SomeServiceImpl implements SomeService {
      @Autowired
      private TreeCacheWrapper ffCoreCache;

      @Autowired(required = false)
      private ZookeeperProperties zookeeperProperties;

      public SomeServiceImpl(TreeCacheWrapper ffCoreCache, ZookeeperProperties zookeeperProperties) {
         this.ffCoreCache = ffCoreCache;
         this.zookeeperProperties = zookeeperProperties;
      }
}
com.som.test
这个
SomeServiceImpl
类所在的主包如下所示:

@Service
public class SomeServiceImpl implements SomeService {
      @Autowired
      private TreeCacheWrapper ffCoreCache;

      @Autowired(required = false)
      private ZookeeperProperties zookeeperProperties;

      public SomeServiceImpl(TreeCacheWrapper ffCoreCache, ZookeeperProperties zookeeperProperties) {
         this.ffCoreCache = ffCoreCache;
         this.zookeeperProperties = zookeeperProperties;
      }
}
com.som.test
现在在我的项目中,当我自动连接这个类时,我在运行SpringBoot应用程序时遇到了bean创建错误。我就是这样做的:

@Autowired
private SomeService someService;
错误

 org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'someServiceImpl'
Unsatisfied dependency expressed through field 'ffCoreCache'; 
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ffCoreCache' defined in class path resource [com/som/test/config/ZooKeeperConfig.class]: 
Bean instantiation via factory method failed; 
nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [com.som.test.cache.TreeCacheWrapper]: 
Factory method 'ffCoreCache' threw exception; 
nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry


Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'ffCoreCache' defined in class path resource [com/som/test/config/ZooKeeperConfig.class]: 
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [com.som.test.cache.TreeCacheWrapper]: 
Factory method 'ffCoreCache' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry



Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.som.test.cache.TreeCacheWrapper]: 
Factory method 'ffCoreCache' threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry

Caused by: java.lang.NoClassDefFoundError: org/apache/curator/retry/ExponentialBackoffRetry

Caused by: java.lang.ClassNotFoundException: org.apache.curator.retry.ExponentialBackoffRetry

当SpringBoot应用程序启动时,它会扫描主类所在包中的bean以及它下面的所有包

由于您的主类驻留在包
com.som.demo
中,因此spring boot将在该包中,或在
com.som.demo.sample1
com.som.demo.a.b.c
等包中找到任何bean 但是,它不会扫描
com.som.test
包,因为它是主包的“对等方”

通常,您可以通过接受要扫描的基本包列表的
@ComponentScan
注释,将spring boot配置为扫描您选择的包。您可以将此注释放在主类上(在
@SpringBootApplication
旁边)

然而,这有点违背了spring boot的惯例


阅读更多技术细节和示例。

用@Service而不是实现类注释服务接口,并尝试用@Component注释ServiceImplementation类。

Spring boot默认情况下扫描到主包中的bean。您可以使用

@SpringBootApplication(scanBasePackages={"com.som.demo" , "com.som.test"})

如果您得到ClassNotFoundException,则必须将依赖项添加到项目中。