Spring boot Bean创建问题:Bean';xyz';,无法注册类中定义的路径资源
我在创建bean时遇到了一个问题。我试图通过使Spring boot Bean创建问题:Bean';xyz';,无法注册类中定义的路径资源,spring-boot,Spring Boot,我在创建bean时遇到了一个问题。我试图通过使spring.main.allow bean definition overriding=true来解决这个错误,但我想知道为什么会发生这个错误 发生异常。请参阅下面的错误以供参考 Description: The bean 'configProvider', defined in class path resource [com/fidel/fixadaptor/config/BeanConfig.class], could not be re
spring.main.allow bean definition overriding=true
来解决这个错误,但我想知道为什么会发生这个错误
发生异常。请参阅下面的错误以供参考
Description:
The bean 'configProvider', defined in class path resource
[com/fidel/fixadaptor/config/BeanConfig.class], could not be registered. A bean with that name has
already been defined in file [D:\Swapnil\Kovacap\CODE\fix-adaptor-
Ver1.1\target\classes\com\fidel\fixadaptor\config\ConfigProvider.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-
definition-overriding=true
Bean配置类
@Configuration
@ComponentScan("com.fidel.fixadaptor")
public class BeanConfig {
/** The destination. */
private AdaptorDestination destination;
private ConfigProvider configProvider;
/**
* Config provider.
*
* @return the config provider
*/
@Bean
public ConfigProvider configProvider() {
if (configProvider == null) {
configProvider = new ConfigProvider();
}
return configProvider;
}
/**
* Recovery.
*
* @return the recovery
*/
@Bean
public Recovery recovery() {
return new RecoveryManager(configProvider());
}
}
@Component
public class ConfigProvider {
private HashMap<String, String> configMap;
@Autowired
private FixUtils fixUtils;
/**
* @param activeProfile
* @param connectionType
* @return
*/
public HashMap<String, String> getConfig(String activeProfile, String connectionType) {
String file = fixUtils.getConfigFile(connectionType, activeProfile);
File cfgFile = new File(file);
configMap = new HashMap<String, String>();
Scanner sc = null;
try {
sc = new Scanner(cfgFile);
while (sc.hasNextLine()) {
String keyValue = sc.nextLine();
String keys[] = keyValue.split("=");
configMap.put(keys[0], keys[1]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configMap;
}
}
配置提供程序类
@Configuration
@ComponentScan("com.fidel.fixadaptor")
public class BeanConfig {
/** The destination. */
private AdaptorDestination destination;
private ConfigProvider configProvider;
/**
* Config provider.
*
* @return the config provider
*/
@Bean
public ConfigProvider configProvider() {
if (configProvider == null) {
configProvider = new ConfigProvider();
}
return configProvider;
}
/**
* Recovery.
*
* @return the recovery
*/
@Bean
public Recovery recovery() {
return new RecoveryManager(configProvider());
}
}
@Component
public class ConfigProvider {
private HashMap<String, String> configMap;
@Autowired
private FixUtils fixUtils;
/**
* @param activeProfile
* @param connectionType
* @return
*/
public HashMap<String, String> getConfig(String activeProfile, String connectionType) {
String file = fixUtils.getConfigFile(connectionType, activeProfile);
File cfgFile = new File(file);
configMap = new HashMap<String, String>();
Scanner sc = null;
try {
sc = new Scanner(cfgFile);
while (sc.hasNextLine()) {
String keyValue = sc.nextLine();
String keys[] = keyValue.split("=");
configMap.put(keys[0], keys[1]);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return configMap;
}
}
@组件
公共类配置提供程序{
私有HashMap-configMap;
@自动连线
私人固定装置;
/**
*@param-activeProfile
*@param connectionType
*@返回
*/
公共HashMap getConfig(字符串activeProfile,字符串connectionType){
String file=fixUtils.getConfigFile(connectionType,activeProfile);
文件cfgFile=新文件(文件);
configMap=新的HashMap();
扫描仪sc=空;
试一试{
sc=新扫描仪(CFG文件);
while(sc.hasNextLine()){
字符串keyValue=sc.nextLine();
字符串键[]=keyValue.split(“”);
configMap.put(键[0],键[1]);
}
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回配置图;
}
}
请帮助我了解为什么会发生这种情况,任何其他信息请评论我。
提前感谢用Spring的
@组件注释的类
由Spring bean容器自动管理,即:Spring将自己创建一个实例,并在需要时注入它。问题是您正在手动公开另一个bean,因此这会产生歧义,框架无法确定将哪个bean注入到何处
解决方案是删除您对bean的声明,并让spring执行它的功能。我手动执行此操作,因为我希望将该bean作为
@bean public Recovery(){return new RecoveryManager(configProvider());}
recovery Bean我不明白您想要完成什么。为什么要在spring可以为您完成的情况下手动完成?如果我删除configProvider(),请告诉我recoveryManager()将如何获得它