spring cloud ConsulRibbonClientConfiguration exception - spring

When trying to use spring cloud consul I am receiving this error when I try and autowire the Ribbon client during a rest call:
"Error creating bean with name 'consulRibbonClientConfiguration': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: com/google/common/reflect/TypeToken"
Other times I will get this error instead:
Error creating bean with name 'consulRibbonClientConfiguration': Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: Could not initialize class com.netflix.client.config.CommonClientConfigKey
I had this working before I started trying to use my own configuration classes. Now depending on what system I run it on I get slightly different errors like the one above where the consul ribbon client configuration is not able to instantiate itself. Any incite on this problem would be helpful
The configuration class looks like:
#Profile("!unit-test")
#EnableDiscoveryClient
#ImportResource("classpath:/hadoopContext.xml")
#Configuration
#EnableAutoConfiguration
public class XXXConfiguration..
The main is simply:
#SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
The pom is using:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-consul-all</artifactId>
<version>1.0.0.M3</version>
</dependency>

This problem appears to have been caused by redundant and mismatched dependencies in the maven file. These don't appear to be related to Ribbon, but I guess somehow this cascaded the issue when Ribbon tried to initialize.
For future readers redundant libraries I removed included: spring-tx, hadoop-common, and javax.validation which all needed to be removed to get past this error.

Related

I have upgraded Spring boot version from 1.5 to 2.1. But facing error org.springframework.beans.factory.UnsatisfiedDependencyException

I found another post on Stackoverflow that was similar to my question. But It was saying "don't use Camel case in #ConfigurationProperties".
But, I'm not using Camel case. So that's why asking a new question.
I'm using below code in Spring Boot 2.1:
#Configuration
#EnableConfigurationProperties(UMAAppProperties.class)
public class UMAAppConfig {
}
and
#ConfigurationProperties("app") //all properties are within the 'app' hierarchy
public class UMAAppProperties {
}
In application.yml, I'm writing:
app: // This single line only. Nothing after that for app:
But, I'm getting below exception:
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'UMAAppConfig': Unsatisfied dependency expressed through field
'umaAppProperties';
nested exception is org.springframework.boot.context.properties.ConfigurationPropertiesBindException:
Error creating bean with name 'app-com.common.finding.abc.configuration.UMAAppProperties': Could not
bind properties to 'UMAAppProperties' : prefix=app, ignoreInvalidFields=false,
ignoreUnknownFields=true;
Any solution for that??
I found the answer:
As there is nothing under app: in application.yml , then means we have to comment this line.

Spring Boot connect to Postgres database on Heroku

I've been playing around with a Spring Boot app deployed on Heroku but I've stumbled upon an error that I can't seem to find a solution.
I'm trying to connect to a Postgres database following the Heroku tutorial (link) but I get this error over and over again:
Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [javax.sql.DataSource]:
Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: No supported DataSource type found
Here's the config file I'm using:
spring.datasource.url=${JDBC_DATABASE_URL}
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.removeAbandoned=true
And the DatabaseConfig class:
#Configuration
public class DatabaseConfig {
#Bean #Primary
#ConfigurationProperties(prefix = "spring.datasource")
public DataSource dataSource() {
return DataSourceBuilder.create()
.build();
}
}
Can anyone point me in the right direction. What am I doing wrong?
I encountered this same exact issue and managed to solve it. The issue is not specific to Heroku, because it can be reproduced by running the app locally as well using the same configuration.
According to the stacktrace it is clear that a DataSource has not been found in the class path. According to Spring Boot documentation, found here, you can either use spring-boot-starter-jdbc or spring-boot-starter-data-jpa to automatically get tomcat-jdbc, which appears to be the preferred one in Spring Boot.
I added the following dependency to pom.xml, which solved the problem:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

Disabling Transaction Management in Spring JMS listener

I have a spring boot application as a Spring JMS listener. i have configured multiple datasource manager one for Oracle and another one for DB2 .
whenever i am starting app ,jms listener container is looking for a transaction manager bean and giving below error as it find two bean.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jms.JmsAnnotationDrivenConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.springframework.transaction.PlatformTransactionManager org.springframework.boot.autoconfigure.jms.JmsAnnotationDrivenConfiguration.transactionManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.transaction.PlatformTransactionManager] is defined: expected single matching bean but found 2: db2TransactionManager,oracleTransactionManager
i dont want to maintain JMS transaction. how could i achieve it or how can we disable jms transaction feature?
below are the annotation i have added on my main spring boot class. also i am using Spring Data repository
#SpringBootApplication(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class})
#ComponentScan(basePackages = "com.deere.oracledataupdate.*")
//#EnableJpaRepositories(basePackages ="com.deere.oracledataupdate.dao.springdata")
#EntityScan(basePackages = "com.deere.oracledataupdate.*")
#PropertySource({ "classpath:application-${IafConfigSuffix}.properties" })
public class Application extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Looking to the current Spring Boot code we have (JmsAnnotationDrivenConfiguration):
#Autowired(required = false)
private JtaTransactionManager transactionManager;
So, right now it requires only the bean which is exactly JtaTransactionManager by type. I guess both yours are DataSourceTransactionManager.
I'm sure that was correct fix to worry only about the XA tx-manager for auto-config.
Seems for me you can fix your issue with something like #Primary on one of your tx-manager beans.
But... Do you need a JMS Annotation support in your application at all?
Maybe it would be just enough to exclude JmsAnnotationDrivenConfiguration as well?
If need it anyway, I see only one way to fix it: disable JmsAnnotationDrivenConfiguration and configure #EnableJms manually, bypassing the tx-manager issue and just don't configure it for the DefaultJmsListenerContainerFactory as you request.
See JmsAnnotationDrivenConfiguration source code for more information.

Mockito Spring 3.1 Integration

I have been using #Configuration support in Spring to create my Mockito Mocks for use in JUnit tests
#Configuration
public class MockAppContextHelper {
#Bean
public IntegrationServerServiceWrapper integrationServerServiceWrapperTest() {
return mock(IntegrationServerServiceWrapper.class);
}
}
This used to work fine in Spring 3.0.2.
In Spring 3.1 I get the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'integrationServerServiceWrapperTest' defined in class path resource [com/kn/bpa/task/service/impl/MockAppContextHelper.class]: No matching factory method found: factory bean 'mockAppContextHelper'; factory method 'integrationServerServiceWrapperTest()'. Check that a method with the specified name exists and that it is non-static.
Any ideas?
Thanks for your support
Consider adding a reproduction project per the instructions at https://github.com/SpringSource/spring-framework-issues#readme that demonstrates the configuration in question working in 3.0.2 and failing against 3.1.x

Getting Spring Error "Bean named 'x' must be of type [y], but was actually of type [$Proxy]" in Jenkins

I have been debugging this for awhile now, and I'm hoping someone could shed some light here.
I have a Maven project that is added into Jenkins, using JDK 1.6. I'm using AOP in this project to handle the database transaction.
When I run the build in Jenkins, my testcase fails with the following exceptions:-
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'dataHandlerClassificationImpl':
Injection of resource dependencies failed; nested exception is
org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData],
but was actually of type [$Proxy17]
...
...
Caused by: org.springframework.beans.factory.BeanNotOfRequiredTypeException:
Bean named 'writerDataLocationImpl' must be of type [xxx.script.WriterData],
but was actually of type [$Proxy17]
...
...
The DataHandlerClassificationImpl class looks something like this:-
#Service
public class DataHandlerClassificationImpl extends DataHandler {
#Resource(name="writerDataLocationImpl")
private WriterData writerData;
...
}
WriterData is an interface with multiple implementations.
I am able to execute the code without problem from the IDE. To determine whether it is a Maven problem or Jenkins problem, I navigated to the Jenkins' project job folder using command line and I'm able to run mvn test without any errors.
I know the proxy error has something to do with AOP, and that I can only autowire to an interface instead of a concrete class... but that's not the case here since I'm able to run my code fine outside Jenkins.
Any ideas? Thanks.
Excerpt from question comments above:
Are you running Cobertura, Sonar or other code-instrumenting tool on Jenkins? Note that mvn site might also be configured to include Cobertura report in generated site.
The problem with Cobertura is that it performs pretty heavy byte-code instrumentation including the addition of some custom interfaces. When Spring starts up it generates proxies for beans. If bean has at least one interface, it uses standard Java proxy. Otherwise it tries to create class-based proxy.
I guess in your case the CGLIB class proxy was used but after Cobertura instrumentation Spring fall back to java proxies. This caused startup error because dependency injection expected class (or CGLIB subclass).
To cut long story short, force CGLIB class proxies and you'll be fine:
<aop:config proxy-target-class="true"/>
Got the same problem using AspectJ.
There was a bean w
#Configuration public class MyConfig{
#Value("classpath:some.properties")
private Resource theResource;
#Bean
public SomeResource getSomeResource()
{
return SomeResource.getOne(theResource);
}
/******/
#Component
public class SomeResource{
public SomeResource(Resource r) {...}
public static getOne(Resource r} { return new SomeResource(r); }
This works fine until AOP/AspectJ is enabled. The injection validates that the SomeResource bean is from class SomeResource, but since it is a Proxy it crashes.
SOlution: use GLIBC proxy for that Bean instead of AspectJ proxies.
#EnableAspectJAutoProxy(proxyTargetClass=false)
public class SomeResource{...}
Makes no sense, but now got a clearer message
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.springframework.cglib.core.ReflectUtils
(file:/path/spring-core/5.2.10.RELEASE/spring-core-5.2.10.RELEASE.jar) to method
java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain)
WARNING: Please consider reporting this to the maintainers of org.springframework.cglib.core.ReflectUtils
Meaning Java prevent reflection on this method.Either Spring or Java needs to fix that.

Resources