@自动连接外部Jar中的对象

Med*_*edo 2 java spring spring-boot

我想从我在应用程序中使用的外部JAR自动连接对象:

@Autowired
PythonInterpreter interp;
Run Code Online (Sandbox Code Playgroud)

我得到这个异常:

com.package.services.ServicesImpl中的字段interp需要一个类型为'org.python.util.PythonInterpreter'的bean,无法找到。

行动:

考虑在配置中定义类型为“ org.python.util.PythonInterpreter”的bean。

我知道,@ComponentScan只有在使用注释班级时,该方法才有效@Component

Gho*_*ica 5

关键是:您必须告诉Spring 如何创建该类的实例。

请参阅其文档中的示例:

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }
}
Run Code Online (Sandbox Code Playgroud)

因此,正如第一个注释正确告诉您的那样:您需要定义一种以某种方式创建该对象的方法。然后,将该方法注释为@Bean,并确保Spring将其找到为@Configuration。