如何把一个RestTemplate放到一个单独的class中?
How to put a RestTemplate into a separate class?
我有一个 Spring 应用程序,其 RestTemplate
的定义如下:
@SpringBootApplication(scanBasePackages = {"com.mycompany"})
public class MyApplication extends SpringBootServletInitializer {
[...]
@Bean
public RestTemplate getRestTemplate1() {
return new RestTemplate();
}
@Bean("restTemplate2")
public RestTemplate getRestTemplate2() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
[...]
}
[...]
}
我想把getRestTemplate2
放到一个单独的class中。所以我创建了以下 class:
@Configuration
@ComponentScan
public class GetRestTemplate2 {
@Bean("restTemplate2")
public RestTemplate getRestTemplate2() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
[...]
}
}
当我启动应用程序时收到错误消息
org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'restTemplate2'
defined in class path resource [XXXXXXXX.class]: Cannot register bean definition
[Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false;
factoryBeanName=restTemplate2; factoryMethodName=getRestTemplate2; initMethodName=null; destroyMethodName=(inferred); defined
in class path resource [XXXXXXXXX.class]] for bean 'restTemplate2':
There is already [Generic bean: class [XXXXXXXXX]; scope=singleton; abstract=false;
lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null;
initMethodName=null; destroyMethodName=null; defined in file
[XXXXXXXX.class]] bound.
如何确保创建 restTemplate2
的代码位于单独的 class 中并被 Spring 正确检测到?
更新 1: 将 GetRestTemplate2
class 重命名为 GetRestTemplate2Config
没有帮助。
这似乎有效:
@Configuration
@ComponentScan
public class GetRestTemplate2Config {
@Bean(name = "restTemplate2")
public RestTemplate getRestTemplate2() throws KeyStoreException,
NoSuchAlgorithmException, KeyManagementException {
[...]
}
}
我有一个 Spring 应用程序,其 RestTemplate
的定义如下:
@SpringBootApplication(scanBasePackages = {"com.mycompany"})
public class MyApplication extends SpringBootServletInitializer {
[...]
@Bean
public RestTemplate getRestTemplate1() {
return new RestTemplate();
}
@Bean("restTemplate2")
public RestTemplate getRestTemplate2() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
[...]
}
[...]
}
我想把getRestTemplate2
放到一个单独的class中。所以我创建了以下 class:
@Configuration
@ComponentScan
public class GetRestTemplate2 {
@Bean("restTemplate2")
public RestTemplate getRestTemplate2() throws KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
[...]
}
}
当我启动应用程序时收到错误消息
org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'restTemplate2'
defined in class path resource [XXXXXXXX.class]: Cannot register bean definition
[Root bean: class [null]; scope=; abstract=false; lazyInit=null; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false;
factoryBeanName=restTemplate2; factoryMethodName=getRestTemplate2; initMethodName=null; destroyMethodName=(inferred); defined
in class path resource [XXXXXXXXX.class]] for bean 'restTemplate2':
There is already [Generic bean: class [XXXXXXXXX]; scope=singleton; abstract=false;
lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null;
initMethodName=null; destroyMethodName=null; defined in file
[XXXXXXXX.class]] bound.
如何确保创建 restTemplate2
的代码位于单独的 class 中并被 Spring 正确检测到?
更新 1: 将 GetRestTemplate2
class 重命名为 GetRestTemplate2Config
没有帮助。
这似乎有效:
@Configuration
@ComponentScan
public class GetRestTemplate2Config {
@Bean(name = "restTemplate2")
public RestTemplate getRestTemplate2() throws KeyStoreException,
NoSuchAlgorithmException, KeyManagementException {
[...]
}
}