Java ..... Spring Boot 中构造函数的参数 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50412208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 23:37:13  来源:igfitidea点击:

Parameter 0 of constructor in ..... Spring Boot

javaspring-boot

提问by Aymen Kanzari

I have a problem when launch my app. Could somebody help me to solve this issue?

启动我的应用程序时遇到问题。有人可以帮我解决这个问题吗?

Parameter 0 of constructor in com.journaldev.elasticsearch.service.BookServiceImpl required a bean of type 'com.journaldev.elasticsearch.dao.search.BookRepositorySearch' that could not be found.
Action:
Consider defining a bean of type 'com.journaldev.elasticsearch.dao.search.BookRepositorySearch' in your configuration.

GenericRepository

通用存储库

public interface GenericRepository<T, K> {
  Map<String, Object> get(final K id);
}

GenericRepositoryImpl

GenericRepositoryImpl

public class GenericRepositoryImpl<T, K extends Serializable> implements GenericRepository<T, K> { 

  private RestHighLevelClient restHighLevelClient;

  private ObjectMapper objectMapper;

  public GenericRepositoryImpl(ObjectMapper objectMapper, RestHighLevelClient restHighLevelClient) {
    this.objectMapper = objectMapper;
    this.restHighLevelClient = restHighLevelClient;
  }

  @Override
  public Map<String, Object> get(K id) {
    return null;
  }
}

BookRepositorySearch

书库搜索

@Component
public interface BookRepositorySearch extends GenericRepository<Book, Long> {}

BookService

图书服务

public interface BookService {
  Map<String, Object> get(final Long id);    
}

BookServiceImpl

图书服务实现

@Service
public class BookServiceImpl implements BookService {

  private final BookRepositorySearch bookRepositorySearch;

  public BookServiceImpl(BookRepositorySearch bookRepositorySearch) {
    this.bookRepositorySearch = bookRepositorySearch;
  }      
  @Override
  public Map<String, Object> get(Long id) {
    return null;
  }
}

采纳答案by Always Learning

From your previous comments, looks like you want to keep BookRepositorySearchas an interface. If that's the case, you need to create a concrete instance of that interface and put @Componenton that.

从您之前的评论来看,您似乎想保留BookRepositorySearch一个界面。如果是这种情况,您需要创建该接口的具体实例并穿上@Component它。

You don't need @Componenton your interface declaration and you can't extend a class in an interface.

您不需要@Component接口声明,也不能在接口中扩展类。

public interface BookRepositorySearch {}

Create a concrete type that implements the interface and extends extends GenericRepository<Book, Long>you want to autowire and put @Componenton it:

创建一个具体类型来实现接口并扩展GenericRepository<Book, Long>您想要自动装配的扩展并将其放在@Component上面:

@Component
public class BookRepositorySearchImpl 
    implements BookRepositorySearch 
    extends GenericRepository<Book, Long>  
{}

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-class-ctor

https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-class-ctor

Instantiation with a constructor

When you create a bean by the constructor approach, all normal classes are usable by and compatible with Spring. That is, the class being developed does not need to implement any specific interfaces or to be coded in a specific fashion. Simply specifying the bean class should suffice. However, depending on what type of IoC you use for that specific bean, you may need a default (empty) constructor.

The Spring IoC container can manage virtually any class you want it to manage; it is not limited to managing true JavaBeans. Most Spring users prefer actual JavaBeans with only a default (no-argument) constructor and appropriate setters and getters modeled after the properties in the container. You can also have more exotic non-bean-style classes in your container. If, for example, you need to use a legacy connection pool that absolutely does not adhere to the JavaBean specification, Spring can manage it as well.

使用构造函数实例化

当您通过构造函数方法创建 bean 时,所有普通类都可以被 Spring 使用并与 Spring 兼容。也就是说,正在开发的类不需要实现任何特定的接口或以特定的方式进行编码。只需指定 bean 类就足够了。但是,根据您对该特定 bean 使用的 IoC 类型,您可能需要一个默认(空)构造函数。

Spring IoC 容器几乎可以管理您希望它管理的任何类;它不仅限于管理真正的 JavaBean。大多数 Spring 用户更喜欢实际的 JavaBeans,它只有一个默认的(无参数)构造函数和适当的 setter 和 getter,它们以容器中的属性为模型。您还可以在您的容器中拥有更多异国情调的非 bean 风格的类。例如,如果您需要使用绝对不符合 JavaBean 规范的遗留连接池,Spring 也可以管理它。

回答by Jan Ossowski

First of, You need to "tell" spring what to pass as a parameter. The simplest option is the one mentioned by @Berger in a comment. If for some reason that is not a good approach for you (eg. BookRepositorySearch is not a spring managed bean), you can make a java config file with some more logic:

首先,您需要“告诉”spring 作为参数传递的内容。最简单的选项是@Berger 在评论中提到的选项。如果由于某种原因这对您来说不是一个好方法(例如 BookRepositorySearch 不是 Spring 管理的 bean),您可以使用更多逻辑创建一个 java 配置文件:

@Configuration
public class Config {

   // you can use @Autowired here


   @Bean
   public BookService bookService() {
      return new BookServiceImpl(--pass your parameter here, get it however you want--)
   }

}

edit:

编辑:

Apparently Spring doesn't require @Autowired anymore (thanks @Mark Rotteveel). So the problem is that spring doesn't have an instance of your class. The reason for that is (I think) that you use a class parameter instead of an interface. If You just create a marker interface that BookRepositorySearch implements and use that as an argument instead of the actual inplementation, I would expect it to work. Another solution is what I wrote above already, but for the BookRepositorySearch class.

显然 Spring 不再需要@Autowired(感谢@Mark Rotteveel)。所以问题是 spring 没有你的类的实例。原因是(我认为)您使用类参数而不是接口。如果您只是创建 BookRepositorySearch 实现的标记接口并将其用作参数而不是实际的实现,我希望它能够工作。另一个解决方案是我上面已经写的,但用于 BookRepositorySearch 类。

@Configuration
public class Config {

   // you can use @Autowired here


   @Bean
   public BookRepositorySearch bookRepositorySearch () {
      return new BookRepositorySearch();
   }

}

This way Spring will have it's beloved instance ;)

这样,Spring 就会拥有它心爱的实例;)

回答by devquy

How do you inherit a class as an interface?

你如何继承一个类作为接口?

  @Component 
  public interface BookRepositorySearch extends GenericRepository<Book, Long> {}

Change this interface with a class then try again.

使用类更改此接口,然后重试。

  @Component
  public class BookRepositorySearch extends GenericRepository<Book, Long> {}

回答by Maung Kyaing

I solved it with this configuration. Look for @EnableAutoConfigurationin your Configuration file.

我用这个配置解决了它。@EnableAutoConfiguration在您的配置文件中查找。

@Configuration

@EnableJpaRepositories(basePackages = "com.akog02.repostories")

@EntityScan(basePackages = "com.akog02.domain")

@EnableTransactionManagement

@EnableAutoConfiguration

public class WebConfiguration {

}