Getting cyclic dependencies error in Spring boot - java

I am trying to migrate our spring application to Spring boot.
I am getting an cyclic dependencies error.
Bean A -> Bean B -> Bean C
In Bean C I am autowiring a Map which is defined in a XML
<util:map id="beanMap" map-class="java.util.HashMap">
<entry key="beanA" > <bean class="com.org.BeanA" /> </entry>
</util:map>
In Bean C I am autowiring
#Autowired
#Resource(name = "beanMap")
private Map<String, serviceInterface> beanMap;
This setup is working properly in Spring without boot.
But when I try to run this configuration in Spring Boot. I am facing cyclic dependencies error.
Any help is appreciated.

Try removing #Autowired in Bean C.

Related

after upgrading the Spring boot version to 2.6, getting circular dependency error

Spring boot application failed to start after upgrading to 2.6.4 due to circular dependency.
I have two class like this:
class ABC{
#Autowired
PQR pqr;
}
Class PQR{
#Autowired
ABC abc;
}
previously working fine, now getting circular dependency error after upgrading spring boot to 2.6.4
How to resolve this?
You can write #Lazy annotation at top of one of the autowire . This will let your spring application compile without any error
Like below:
Class PQR{
#Lazy
#Autowired
ABC abc;
}

How to use class which is not a dependency in my spring boot project

In my spring project I can defined bean in XML like this:
<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
<property name="messageFactory">
<bean class="weblogic.xml.saaj.MessageFactoryImpl" />
</property>
</bean>
even my project does not contain weblogic.xml.saaj.MessageFactoryImpl class. When I deploy my application to wls server everything works fine just STS is complaining that it doesn't know this class. How I can convert this code to spring-boot application and define this factory in some java class ?
In a configuration class (has #Configuration above class definition) create a method as a factory for your MessageFactoryImpl and use #Bean above it. Defining a bean in spring boot is like the old java configuration.
See This question , It will help
weblogic.xml.saaj.MessageFactoryImpl is an implementation class which exists already in WebLogic server library jars.
To do that using #Bean, use #ConditionOnClass
#Bean
#Primary
#ConditionalOnClass(weblogic.xml.saaj.MessageFactoryImpl.class)
public SoapMessageFactory messageFactory() {
return new weblogic.xml.saaj.MessageFactoryImpl();
}

Spring boot CRUD application - Bean not found error

I am developing a simple CRUD application with spring-boot.
I have most of the project completed, although I get this error when I try to run the project.
Description:
Field userDBOP in com.application.crud.GreetingController required a
bean of type 'com.application.crud.myoperation.JdbcUserDAO' that could
not be found.
Action:
Consider defining a bean of type
'com.application.crud.myoperation.JdbcUserDAO' in your configuration.
In IntelliJ when I hover over the line that causes the error, the following message shows up
"Could not autowire. No beans of 'JdbcUserDAO' type found.
Even though I have in my 'Beans.xml' file (located below the 'src' directory:
<bean id="customerDAO" class="com.application.crud.myoperation.JdbcUserDAO">
<property name="dataSource" ref="dataSource" />
</bean>
Can anyone tell me how to fix this error?
This seems to be a problem with configuring a Spring Boot Application with an existing Spring Context. There is a section in the Spring documentation about this.
By default, you need to specify the location of your application context using the #ImportResource annotation. An example would be:
#SpringBootApplication
#ImportResource("applicationContext.xml")
public class ExampleApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(ExampleApplication.class, args);
}
}
Note that if the file is located elsewhere in the classpath, then you need to reference it properly for spring to pick it up (e.g #ImportResource({"classpath*:applicationContext.xml"}) )

Spring Java Config with XML config

I am trying to use Spring java config with XML based config but beans using spring profile feature are never getting created. Can you suggest what could be root cause. Spring 3.2 is used by my application.
My Java Config Class -
#Configuration
#ComponentScan({ "com.aexp.mars" })
#Profile("dev")
#PropertySource("classpath:messages.properties" )
#ImportResource("classpath:/spring/spring-profiles.xml")
public class SpringConfigDev{
private DriverManagerDataSource dataSource;
Spring XML config-
<beans profile="dev">
<bean id="marsURLConfig" class="com.aexp.mars.common.utils.MarsURLConfig">
<property name="solrBaseURL" value="http://localhost:8080/solr"/>
</bean>
</beans>
The Dev profile is activated using the code below inside the main method
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
String env = "dev";
context.getEnvironment().setActiveProfiles(env);
context.register(SpringConfigDev.class);
context.refresh();
One of my Spring Components uses Autowiring with MarsURLConfig type and I get the exception below.
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException:
No matching bean of type [com.aexp.mars.common.utils.MarsURLConfig] found
for dependency: expected at least 1 bean which qualifies as autowire
candidate for this dependency. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true)}
If I remove profile="dev" from the xml, the application works correctly
this seems to be known limitation with Spring 3.2 Java based config approach. Spring 3.2 do not support bean level profile creation when we use Java config and since I was trying to load XML from Java based configuration class , it didn't worked for me .

MappingJacksonHttpMessageConverter not found with Spring4

I migrated my Spring framework from 3.x to 4.2.RELEASE but, when I start the jUnit I'm getting this error:
Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from URL [file:src/test/resources/applicationContext.xml]; nested exception is java.lang.NoClassDefFoundError: org/springframework/http/converter/json/MappingJacksonHttpMessageConverter
I read on internet from Version 4.1. it's not supported anymore; I checked and the new version is available on application class path (I imported spring-web dependency). ( Spring 4 and Rest WS integration )
It seems spring is still looking for the old version of Converter. It probably depends on something I've on my application context, but, the question is, how can I "tell to Spring" to use new version?
-- UPDATE
I commented
<mvc:annotation-driven />
and it seems to works fine but... why?
There's a newer version of that class in Spring 4: use org.springframework.http.converter.json.MappingJackson2HttpMessageConverter (note the '2').
In my REST servlet's Spring config file I have it configured as follows (I added my custom objectMapper to it, but you can just omit that):
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="s3.util.json.CustomJsonMapper" />
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>

Categories