I have runnable JAR A which contains interface:
interface FooInterface {
void foo();
...
}
In JAR AI have also class which is trying to autowire FooInterface implmenetation:
class Other{
@Autowired
FooInterface fooInterfaceImplementation;
...
}
In other project BI have jar A as external library and implementation of the FooInterface:
@Component
class BarClass implements FooInterface {
void foo(){...}
...
}
I'm trying to run my runnable A JAR with classes from JAR B by using command:
java -jar A.jar -cp B.jar
But it ends with following exception:
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ocado.cfc.optimisation.AlgorithmInterface' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:348)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:335)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1093)
at com.ocado.cfc.optimisation.Executable.main(Executable.java:81)
Is it possible to autowire required bean in that way?
Any help highly appreciated.
是的,为了让Spring从类路径JAR文件中检测/扫描bean,您需要使用类级别注释@ComponentScan(basePackages="com.ocado")
将包添加到spring boot启动器类中。
It isn't possible if you try to use -jar
and -cp
together. -cp
is ignored when you use -jar
.
If you want to have more than one jar on the classpath you can pass them both in using -cp
. You'll then have to also provide the name of the main class that you want to launch. Given that you appear to be using Spring Boot, that might look like this:
java -cp A.jar:B.jar org.springframework.boot.loader.JarLauncher
You may also be interested in Spring Boot's PropertiesLauncher
that allows you to create an executable jar with a configurable class path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.