Issue with #EnableCircuitBreaker annotation when running springboot service in local environment - spring

when using #EnableCircuitBreaker annotation with spring-cloud-services-dependencies 1.3.0 we are getting
"Caused by: java.lang.NoSuchMethodError:
org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper.outboundMapper()Lorg/springframework/integration/amqp/support/DefaultAmqpHeaderMapper;"
exception. We are not using any Messaging functionality in our
service.
We are using jetty embedded environment.
Can anyone suggest me how can I solve my issue?

The exception is thrown, cause your application tries to use some classes of spring integration for message queue, which cannot be found.
If you go further up the stacktrace you may find some classes with 'AutoConfiguration' that may show, which spring auto configuration has triggered the search for these missing classes.
You can also add the command line parameter --debug to see what auto config is actually doing.
Once you identified the culprit you can exclude it from the auto configuration like this :
#EnableAutoConfiguration(exclude = {...})

Related

EnableLoadTimeWeaving annotation causes application context to fail to load

I am trying to enable AspectJ load-time weaving (not Spring AOP) in a Spring Boot application. My goal is to weave advice into annotated fields and java.lang.reflect.Field.set(Object, Object) at load-time.
Per the Spring docs, I tried:
#Configuration
#EnableLoadTimeWeaving
public class Config {}
Running the Spring Boot application with this configuration resulted in the application context failing to load with this message:
Caused by: java.lang.IllegalStateException:
ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader]
does NOT provide an 'addTransformer(ClassFileTransformer)' method.
Specify a custom LoadTimeWeaver or start your Java virtual machine
with Spring's agent: -javaagent:spring-instrument-{version}.jar
The latter suggestion in that message is not a good option as I am trying to avoid necessitating launch script modifications. The aspect I need to weave actually resides in a library, so all implementing Spring Boot projects will have to make whatever changes required to get LTW to work.
I also tried this configuration:
#Configuration
#EnableLoadTimeWeaving
public class Config implements LoadTimeWeavingConfigurer {
#Override
public LoadTimeWeaver getLoadTimeWeaver() {
return new ReflectiveLoadTimeWeaver();
}
}
Running the Spring Boot application with this configuration resulted in the application context failing to load with this message:
Caused by: java.lang.IllegalStateException:
ClassLoader [jdk.internal.loader.ClassLoaders$AppClassLoader]
does NOT provide an 'addTransformer(ClassFileTransformer)' method.
It seems I need to make the JVM use a class loader that has an addTransformer(ClassFileTransformer) method. I don't know how to do that, particularly for this situation. Any suggestions?
I am not an active Spring user, but I know that Spring supports annotation- or XML-configured agent hot-attachment and has some container-specific classes for that according to its documentation. It does not seem to work reliably in all situations, though, especially when running a Spring Boot application from an IDE or so.
Anyway, the AspectJ weaver 1.8.7 and more recent can be hot-attached. I explained how to do that in a Spring setup here. If you want a simpler solution with less boilerplate but one more dependency to a tiny helper library called byte-buddy-agent, you can use this solution as a shortcut. I have not tried it, but I know the helper library and am using it myself in other contexts when hot-attaching bytecode instrumentation agents, avoiding the fuss to cater to different JVM versions and configuration situations. But in order for that to work on JVM 9+, you might need to manually activate auto-attachment for the JVM, which would be another modification for your start-up script, and you would be back to square 1.

Upgrade to Spring Boot 2.1.2 from 2.0.6 causes repository errors

I tried to upgrade a working application from Spring Boot 2.0.6 to 2.1.2. I had some troubles with tests after this change, but eventually got around that. I can successfully build the application from NetBeans (mvn clean install). However, when I try to run from a command line using mvn spring-boot:run, here is what I get:
APPLICATION FAILED TO START
Description:
The bean 'xxxRepository', defined in null, could not be
registered. A bean with that name has already been defined in null and
overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting
spring.main.allow-bean-definition-overriding=true
The interesting part is that every time I try to run it, the error is on a different repository, but always with the same message.
It would seem that this has to do with this change:
Bean Overriding
Bean overriding has been disabled by default to prevent a bean being
accidentally overridden. If you are relying on overriding, you will
need to set spring.main.allow-bean-definition-overriding to true.
Given that it is apparently effecting all my repositories, my guess is that there is a configuration problem somewhere. I can follow the recommended action, but it actually made no difference. The problem is that I don't know what to change in the configuration to get this working again. I'm not even sure what to post that is pertinent to the issue. Any ideas on how to figure this out?
We ran into this issue upgrading from Spring Boot 2.0.x to 2.1.x.
I could "solve" this issue by allowing bean definition override with spring.main.allow-bean-definition-overriding: true but it felt like hiding the root cause.
In fact bean definition overriding used to hide poor configuration on our side.
After inspecting our #Configuration classes we were scanning packages containing our repositories twice, using #ComponentScan and #EnableJpaRepository on the same packages from different classes : once with filters #ComponentScan.Filter, once without.
Removing the second component scan fixed the issue.
I have seen this error before and i had a class BOTH annotated with #Component or #Repository or #Service AND also registered as a #Bean in a config class. Is that your case also by any chance?
I got a similar problem, but it was only with #NotNull annotation. When I upgraded the spring it stopped to work. I tried a lot of things that I found here in SO, but the only thing that worked to me was to eliminate the database and run spring again. I know that it sucks, but didn't find another solution.

Error when trying to use both mongo and sql jpa in the same spring app

We have a spring app that works with mongodb.
Now we need it to connect also to mysql.
All the beans are defined in a applicationContext.xml legacy file.
We like that the new mysql configuration will be in java classes.
We created an entity, repository and a configuraion java files.
But it seems that as we try to do so, spring gets confused.
If we try to run the app, it start complaining about the mongo repositories:
Error creating bean with name 'MyMongoRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities
In the intellij we have the "could not autowire" error only on the sql repository (the first 3 are the mongo repositories):
Is it possible that the #configuration class is clashing with the applicationContext?
Should the #configuraion class be in a certain package/folder to work correctly?
Also, in the #configuration file, as there are green beans on the left side, it seems like intellij is able to understand where is the persistence repository.
Thanks for any help.

Spring Boot 1.5 #JdbcTest failing when using Eureka Discovery

I'm trying to use the new #JdbcTest annotation in Spring boot 1.5.0.RC1.
My app uses Eureka discovery ie I have
compile('org.springframework.cloud:spring-cloud-starter-eureka')
in my build.gradle and
#EnableDiscoveryClient
on my main Spring Boot class
When I try to use #JdbcTest to test a JdbcTemplate based DAO I get this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of method eurekaHealthIndicator in org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration$EurekaHealthIndicatorConfiguration required a bean of type 'com.netflix.discovery.EurekaClient' that could not be found.
Action:
Consider defining a bean of type 'com.netflix.discovery.EurekaClient' in your configuration.
It looks like the auto configuration is loading part of the Eureka configuration when it should only load JDBC related beans.
If I add
#TestPropertySource(properties={"eureka.client.enabled=false"})
to the test the problem goes away, but I think #JdbcTest should be making sure already that only relevant beans are loaded.
Am I missing something or is this a problem with the new #JdbcTest, or maybe Spring Cloud Eureka?
Your #SpringBootApplication has # EnableDiscoveryClient on it. When you use a slice annotation (such as #JdbcTest), Spring Boot finds the context to use by looking at the package of your test for a #SpringBootConfiguration. If it does not find one, it looks in the parent package, etc. With a sensible package structure and no further customization, your tests use your #SpringBootApplication as the root context.
So, that configuration class (and only that one) is processed an #EnableDiscoveryClient is also obviously processed. In your case, this has an additional side effect: every single test now requires the Eureka Client.
TL;DR never put such annotation on your application. And only put it if you actually need it. You could define a #Configuration class next to your Spring Boot app for those.
#David
I had similar issue and fixed by adding the following to my application.yml file
eureka:
client:
enabled: false

TeamCity Inspections (IntelliJ IDEA) and Spring Data

TeamCity seems not to recognize Spring Data Repository beans when running the inspections. It works perfectly in IntelliJ IDEA but not in TeamCity.
I receive the following inspection problems:
Warning This custom Spring bean has not yet been parsed And it points to this XML element:
<jpa:repositories base-package="my.base.package" />
And then (obviously) an Error: Could not autowire. No beans of 'MyRepository' type found.
MyRepository extends the Spring Data CrudRepository interface.
As far as I understand, TeamCity launches an IntelliJ instance when running the inspections. Do I have to specify in some way which plugins (Spring Data) IntelliJ has to use when it's running the inspections?
I also tried multiple times to hit the check/reparse button in TeamCity...
Thanks for your help!

Resources