springshell在jar中运行时失败

mwecs4sa  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(437)

我正在使用springshell构建一个shell应用程序。在这一点上,intellij上的一切都运行良好,这意味着我可以毫无问题地运行我的命令。
我正在使用jpa,hibernate是实现提供者,我能够连接到我的数据库并能够查询。
当我创建应用程序的工件并在没有intellij的情况下运行它时,问题就开始了。
这是我的演示命令:

  1. @ShellComponent
  2. public class MyCommands {
  3. private final EntityManager entityManager;
  4. @Autowired
  5. public MyCommands(EntityManager manager) {
  6. this.entityManager = manager;
  7. }
  8. @ShellMethod(value = "Add two integers together", key = "find")
  9. public String find(int id) {
  10. Customer customer = entityManager.find(Customer.class, id);
  11. String res = "";
  12. if(customer != null) {
  13. // logic
  14. } else {
  15. // logic
  16. }
  17. return res;
  18. }
  19. }

项目结构:

我的工件设置:

配置:

  1. @Configuration
  2. public class Config {
  3. @Bean
  4. public EntityManagerFactory entityManagerFactory() {
  5. return Persistence.createEntityManagerFactory("pu");
  6. }
  7. @Bean
  8. public EntityManager entityManager(EntityManagerFactory emf) {
  9. return emf.createEntityManager();
  10. }
  11. }

应用程序:

  1. @SpringBootApplication
  2. public class DemoApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(DemoApplication.class, args);
  5. }
  6. }

日志:

  1. 16:47:46.312 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
  2. org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'standardParameterResolver' defined in class path resource [org/springframework/shell/standard/StandardAPIAutoConfiguration.class]: Unsatisfied dependency expressed through method 'standardParameterResolver' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="spring-shell")}
  3. at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
  4. at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541)
  5. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1334)
  6. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)
  7. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564)
  8. at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
  9. at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
  10. at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
  11. at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
  12. at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
  13. at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944)
  14. at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
  15. at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
  16. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769)
  17. at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
  18. at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
  19. at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
  20. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1313)
  21. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1302)
  22. at com.example.demo.DemoApplication.main(DemoApplication.java:14)
  23. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.core.convert.ConversionService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Qualifier(value="spring-shell")}
  24. at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790)
  25. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346)
  26. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300)
  27. at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
  28. at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
  29. ... 19 common frames omitted

谢谢您!

a14dhokn

a14dhokn1#

您需要使用一些东西将依赖项与代码打包到jar中,或者需要在执行时手动将依赖项添加到类路径中。这通常包括maven的shade插件或gradle的shade插件。
看看如何使用maven创建具有依赖关系的可执行jar?
这也可以在intellij中完成,但将ide作为构建工具通常是个坏主意。
https://blog.karthicr.com/posts/2016/07/10/creating-an-executable-jar-in-intellij-idea/

相关问题