gpt4 book ai didi

java - 项目树更改后的 Spring UnsatisfiedDependencyException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:45 27 4
gpt4 key购买 nike

我是 Spring 的新手,我正在尝试制作一个简单的休息应用程序。当我将所有文件放在一个包中时,应用程序运行良好。由于我改变了我的项目组织,我无法构建我的项目。我收到此错误:

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);
}

}

我的 CustomerRepository.java 文件:

    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);


}

我的应用程序.java

    package socketApp.main;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import socketApp.dal.model.Customer;
import socketApp.dal.repository.CustomerRepository;

@SpringBootApplication

public class Application implements CommandLineRunner{

@Autowired
private CustomerRepository repository;

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Override
public void run(String... args) throws Exception {

repository.deleteAll();

// save a couple of customers
repository.save(new Customer("Alice", "Smith"));
repository.save(new Customer("Bob", "Smith"));

// fetch all customers
System.out.println("Customers found with findAll():");
System.out.println("-------------------------------");
for (Customer customer : repository.findAll()) {
System.out.println(customer);
}
System.out.println();

// fetch an individual customer
System.out.println("Customer found with findByFirstName('Alice'):");
System.out.println("--------------------------------");
System.out.println(repository.findByFirstName("Alice"));

System.out.println("Customers found with findByLastName('Smith'):");
System.out.println("--------------------------------");
for (Customer customer : repository.findByLastName("Smith")) {
System.out.println(customer);
}

}
}

最后是我的 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();
}
}

最佳答案

您要么必须将 Application 类向上移动一个包,以便该类位于其他包之上,要么通过 scanBasePackages 手动指定 basePackagescanBasePackageClasses对于由您的 @SpringBootApplication 启动的组件扫描。

了解更多详情。

关于java - 项目树更改后的 Spring UnsatisfiedDependencyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46506936/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com