'Conversion of XML beans to Java config annotation based - handle multiple bean output type
I have to convert two beans in one project and in XML format into another project in @Bean format (Java config) therefore without XML.
<bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="org.apache.logging.log4j.LogManager" />
<property name="targetMethod" value="getContext" />
<property name="arguments">
<list>
<value>false</value>
</list>
</property>
</bean>
<bean id="log4jContext" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject" ref="log4jInitialization"/>
<property name="targetMethod" value="setConfigLocation"/>
<property name="arguments">
<list>
<value>classpath:project.properties</value>
</list>
</property>
</bean>
I tried to convert them in this way:
@Bean(name = "log4jInitialization")
public MethodInvokingFactoryBean log4jInitialization() {
MethodInvokingFactoryBean log4jInitialization = new MethodInvokingFactoryBean();
log4jInitialization.setTargetClass(LogManager.class);
log4jInitialization.setTargetMethod("getContext");
log4jInitialization.setArguments(new Object[]{false});
return log4jInitialization;
}
@Bean(name = "log4jContext")
public MethodInvokingFactoryBean log4jContext(MethodInvokingFactoryBean log4jInitialization) {
MethodInvokingFactoryBean log4jContext = new MethodInvokingFactoryBean();
log4jContext.setTargetObject(log4jInitialization);
log4jContext.setTargetMethod("setConfigLocation");
log4jContext.setArguments(new Object[]{"classpath:project.properties"});
return log4jContext;
}
I get the following error: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'org.springframework.beans.factory.config.MethodInvokingFactoryBean' available: expected single matching bean but found 2: &log4jInitialization,&log4jContext
I think the problem is that the output of the two beans is the same.
So I tried to do this, hoping to solve the problem of ambiguity:
@Bean(name = "log4jContext")
public MethodInvokingFactoryBean log4jContext() {
MethodInvokingFactoryBean log4jContext = new MethodInvokingFactoryBean();
MethodInvokingFactoryBean log4jInitialization = new MethodInvokingFactoryBean();
log4jInitialization.setTargetClass(LogManager.class);
log4jInitialization.setTargetMethod("getContext");
log4jInitialization.setArguments(new Object[]{false});
log4jContext.setTargetObject(log4jInitialization );
log4jContext.setTargetMethod("setConfigLocation");
log4jContext.setArguments(new Object[]{"classpath:application.properties"});
return log4jContext;
}
The error changes and is as follows: java.lang.NoSuchMethodException: org.springframework.beans.factory.config.MethodInvokingFactoryBean.setConfigLocation(java.lang.String)
Can anyone help me, please?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
