Reputation: 1667
I would like to autowire an object from an external JAR that I use it in my application:
@Autowired
PythonInterpreter interp;
I get this Exception :
Field interp in com.package.services.ServicesImpl required a bean of type 'org.python.util.PythonInterpreter' that could not be found.
Action:
Consider defining a bean of type 'org.python.util.PythonInterpreter' in your configuration.
I know that @ComponentScan
will only work if the class is annotated with @Component
.
Upvotes: 2
Views: 2477
Reputation: 6216
Spring handles dependency injection through the @Autowired
annotation.When a spring application is initially started it scan packages to discover beans. So all, classes that are annotated or meta-annotated with @Component
will get picked up during component scan.
@Autowired
annotationIn your case,you are auto wiring the bean but spring cannot find a bean of type PythonInterpreter.class
in it's context.That is why it is throwing that error.
The solution to your issue is to register the bean in Spring's application context in a configuration class.We usually register beans by annotation a class with the @Configuration
(so that spring picks it up for component scan) annotation and the @Bean
annotation as follows:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
or using xml based configuration as :
<beans>
<bean id="myService" class="com.acme.services.MyServiceImpl"/>
</beans>
Upvotes: 1
Reputation: 140457
The point is: you have to tell Spring how to create an instance of that class.
See their example in their documentation:
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyServiceImpl();
}
}
So, as the first comment correctly tells you: you need to define a method that somehow creates that object. Then you annotate that method as @Bean, and make sure that Spring finds it as @Configuration.
Upvotes: 6