Java 项目树更改后的Spring UnsatifiedPendencyException异常
我是Spring的新手,我正在尝试制作一个简单的rest应用程序。当我将所有文件放在一个包中时,应用程序运行良好。因为我改变了我的项目组织,所以我无法构建我的项目。我得到这个错误:Java 项目树更改后的Spring UnsatifiedPendencyException异常,java,spring,spring-boot,Java,Spring,Spring Boot,我是Spring的新手,我正在尝试制作一个简单的rest应用程序。当我将所有文件放在一个包中时,应用程序运行良好。因为我改变了我的项目组织,所以我无法构建我的项目。我得到这个错误: 2017-09-30 22:32:48.428 WARN 9428 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cance
2017-09-30 22:32:48.428 WARN 9428 --- [ main] o.s.w.c.s.GenericWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'application': Unsatisfied dependency expressed through field 'repository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'socketApp.dal.repository.CustomerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2017-09-30 22:32:48.431 INFO 9428 --- [ main] utoConfigurationReportLoggingInitializer :
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-09-30 22:32:48.495 ERROR 9428 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field repository in socketApp.main.Application required a bean of type 'socketApp.dal.repository.CustomerRepository' that could not be found.
Action:
Consider defining a bean of type 'socketApp.dal.repository.CustomerRepository' in your configuration.
2017-09-30 22:32:48.496 ERROR 9428 --- [ main] o.s.test.context.TestContextManager : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@16267862] to prepare test instance [hello.GreetingControllerTests@47f08b81]
我当前的项目树如下所示
src
main
java
app
dal
model
-Customer.java
repository
-CustomerRepository.java
main
-Aplication.java
webServices
-GreetingController.java
这是我的Customer.java文件:
package socketApp.dal.model;
import org.springframework.data.annotation.Id;
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
package socketApp.dal.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import socketApp.dal.model.Customer;
@Repository
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
My CustomerRepository.java文件:
package socketApp.dal.model;
import org.springframework.data.annotation.Id;
public class Customer {
@Id
public String id;
public String firstName;
public String lastName;
public Customer() {}
public Customer(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
@Override
public String toString() {
return String.format(
"Customer[id=%s, firstName='%s', lastName='%s']",
id, firstName, lastName);
}
}
package socketApp.dal.repository;
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;
import socketApp.dal.model.Customer;
@Repository
public interface CustomerRepository extends MongoRepository<Customer, String> {
public Customer findByFirstName(String firstName);
public List<Customer> findByLastName(String lastName);
}
最后是我的GreetingController.java
package socketApp.webServices;
import socketApp.dal.repository.CustomerRepository;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import socketApp.dal.model.Customer;
import socketApp.dal.model.Greeting;
@RestController
public class GreetingController {
@Autowired
private CustomerRepository repository;
@RequestMapping("/")
public List<Customer> getAllCustomers(){
// fetch all customers
return repository.findAll();
}
}
包socketApp.webServices;
导入socketApp.dal.repository.CustomerRepository;
导入java.util.List;
导入java.util.concurrent.AtomicLong;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.web.bind.annotation.RequestMapping;
导入org.springframework.web.bind.annotation.RequestParam;
导入org.springframework.web.bind.annotation.RestController;
导入socketApp.dal.model.Customer;
导入socketApp.dal.model.Greeting;
@RestController
公共类迎宾控制器{
@自动连线
私有客户存储库;
@请求映射(“/”)
公共列表getAllCustomers(){
//招揽所有顾客
返回repository.findAll();
}
}
您必须将应用程序
类向上移动一个包,以便该类位于其他包之上的包中,或者手动指定基本包
通过或用于@springbootplication
启动的组件扫描
看
- 及
有关更多详细信息。默认情况下,
@SpringBootApplication
将只扫描从带注释类的包“降序”的组件包
所以在您的例子中,因为main类位于socketApp.main
中,而repository位于socketApp.dal.repository
中,所以不会找到它
您有两个选择:
将应用程序
移动到软件包socketApp
package socketApp;
...
@SpringBootApplication
public class Application implements CommandLineRunner {
...
或添加额外的@ComponentScan
注释。在您的情况下,它将是:
@SpringBootApplication
@ComponentScan("socketApp")
public class Application implements CommandLineRunner {
...
我认为第一种方法更好。在第二种方法中,您不应该使用
@componentscan
,而是应该使用@springbootcapplication
的属性,该属性委托给已经存在的@ComponentSan。