Invalid bean definition with name 'configServicePropertySource' - spring

I have defined ConfigServicePropertySourceLocator bean like following
#Primary
#Bean
public ConfigServicePropertySourceLocator configServicePropertySource(
ConfigClientProperties configClientProperties) {
ConfigServicePropertySourceLocator sourceLocator = new ConfigServicePropertySourceLocator(
configClientProperties);
sourceLocator.setRestTemplate(clientOnlyRestTemplate());
return sourceLocator;
}
but i get the following exception (This is how it is printed in docker) although my bean is marked as #Primary
WARN 1 --- [ main] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.support.BeanDefinitionOverrideException:
Invalid bean definition with name 'configServicePropertySource' defined in de.ig.client.security.configuration.ConfigClientSecurityConfiguration:
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3;
dependencyCheck=0; autowireCandidate=true; primary=true; factoryBeanName=configClientSecurityConfiguration;
factoryMethodName=configServicePropertySource; initMethodName=null; destroyMethodName=(inferred);
defined in de.ig.client.security.configuration.ConfigClientSecurityConfiguration] for bean 'configServicePropertySource':
There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0;
autowireCandidate=true; primary=false; factoryBeanName=configServiceBootstrapConfiguration;
factoryMethodName=configServicePropertySource; initMethodName=null; destroyMethodName=(inferred);
defined in org.springframework.cloud.config.client.ConfigServiceBootstrapConfiguration] bound.

Solved it by setting
spring.main.allow-bean-definition-overriding to true
New from boot version 2.1

Related

Spring Batch, Boot, JNDI and still conflicts with the transactionManager bean name

I have configured Spring Batch, Boot (2.1.4) to retrieve the transaction manager from JBoss via JNDI and I still have conflicts with the transactionManager bean name, is it possible that every time it is always the same story but for different reasons?
Now the message is:
DEBUG [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter] (ServerService Thread Pool -- 76) Application failed to start due to an exception: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'transactionManager' defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]: Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/batch/core/configuration/annotation/SimpleBatchConfiguration.class]] for bean 'transactionManager': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.transaction.jta.JndiJtaConfiguration; factoryMethodName=transactionManager; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/transaction/jta/JndiJtaConfiguration.class]] bound.
Spring batch doesn't work with a transactional manager retrieved via JNDI?
Thanks
You need to give your transaction manager a name other than transactionManager because #EnableBatchProcessing already exposes a bean with that name (this is mentioned in the Javadoc of the annotation).

Spring cloud stream binder - Invalid bean definition

I am using:
some-eventhub.some-service-1.errors in my application's Service Activator to listen to the error channel.
Stream binder 1.2.8 and Spring boot 2.2.2
I get the below exception messages frequently in my app:-
Invalid bean definition with name 'some-eventhub.some-service-1.errors.recoverer' defined in null: Cannot register bean definition
[Root bean: class [org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] for bean 'some-eventhub.some-service-1.errors.recoverer': There is already [Root bean: class [org.springframework.integration.handler.advice.ErrorMessageSendingRecoverer]; scope=; abstract=false; lazyInit=null; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] bound.
It is because it keeps trying to register an error channel recoverer which has already been registered. You probably have more than one binding to the same error topic ("some-eventhub") by the same consumer group ("some-service-1").

Registering test bean with same name in Spring Framework 5.1 [duplicate]

This question already has answers here:
SpringBoot - BeanDefinitionOverrideException: Invalid bean definition
(8 answers)
Closed 3 years ago.
I'm having following config in my production files:
#Configuration
internal class Config {
#Bean
fun clock() = Clock.systemUTC()
}
In tests:
#Configuration
class ClockTestConfiguration {
#Bean
fun clock() = SetableClock()
}
My test annotations:
#SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = [
MyApplication::class,
ClockTestConfiguration::class
]
)
class MyTest {
...
When I was using Spring Boot 2.0.5.RELEASE it worked like a charm. After upgrading to 2.1.0.RELEASE it fails during bean registration.
Caused by: org.springframework.beans.factory.support.BeanDefinitionOverrideException: Invalid bean definition with name 'clock' defined in com.foo.clock.ClockTestConfiguration:
Cannot register bean definition [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=clockTestConfiguration; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred);
defined in com.foo.clock.ClockTestConfiguration] for bean 'clock': There is already [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=config; factoryMethodName=clock; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/foo/clock/Config.class]] bound.
at org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:894)
Is there a clean way to override such bean?
You could use the properties attribute of #SpringBootTest to set spring.main.allow-bean-definition-overriding=true.

SprinBoot app launched from IDE - SpelEvaluationException

I have a simple SpringBoot app with web security and method security config using a custom SecurityExpressionRoot.
When I launch the SB app from an IDE (e.g. STS or IDEA) and call an endpoint I get
org.springframework.expression.spel.SpelEvaluationException: EL1004E:
Method call: Method hasAnyAccess(java.lang.String) cannot be found on
org.springframework.security.access.expression.method.MethodSecurityExpressionRoot type
When I launch the SB app from CLI with the app jar file the endpoint call succeeds:
INFO 11282 --- [nio-8080-exec-4] c.e.CustomMethodSecurityExpressionRoot :
< CustomMethodSecurityExpressionRoot(): [org.springframework.security.authentication.UsernamePasswordAuthenticationToken#442a8f33: Principal: org.springframework.security.core.userdetails.User#36ebcb: Username: user; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_USER; Credentials: [PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authentication.WebAuthenticationDetails#166c8: RemoteIpAddress: 0:0:0:0:0:0:0:1; SessionId: 36E17CAA657E897B1682BFF27EA7DA1F; Granted Authorities: ROLE_USER]
INFO 11282 --- [nio-8080-exec-4] c.e.CustomMethodSecurityExpressionRoot :
> hasAnyAccess(): [[FULL_ACCESS]]
ANY help is highly appreciated.
Web security configuration:
#EnableWebSecurity
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
...
}
Method security configuration:
#Configuration
#EnableGlobalMethodSecurity(prePostEnabled = true)
public class MethodSecurityConfig extends GlobalMethodSecurityConfiguration {
#Override
protected MethodSecurityExpressionHandler createExpressionHandler() {
CustomMethodSecurityExpressionHandler expressionHandler = new CustomMethodSecurityExpressionHandler();
expressionHandler.setPermissionEvaluator(new CustomPermissionEvaluator());
return expressionHandler;
}
}
Method security expression handler:
public class CustomMethodSecurityExpressionHandler extends DefaultMethodSecurityExpressionHandler {
private AuthenticationTrustResolver trustResolver = new AuthenticationTrustResolverImpl();
#Override
protected MethodSecurityExpressionOperations createSecurityExpressionRoot(Authentication authentication, MethodInvocation invocation) {
CustomMethodSecurityExpressionRoot root = new CustomMethodSecurityExpressionRoot(authentication);
root.setPermissionEvaluator(getPermissionEvaluator());
root.setTrustResolver(this.trustResolver);
root.setRoleHierarchy(getRoleHierarchy());
return root;
}
}
Security expression root:
public class CustomMethodSecurityExpressionRoot extends SecurityExpressionRoot implements MethodSecurityExpressionOperations {
public CustomMethodSecurityExpressionRoot(Authentication authentication) {
super(authentication);
LOGGER.info("< CustomMethodSecurityExpressionRoot(): [{}]", authentication);
}
public final boolean hasAnyAccess(String... accessLevels) {
LOGGER.info("> hasAnyAccess(): [{}]", Arrays.asList(accessLevels));
return true;
}
}
Rest controller:
#RestController
#RequestMapping("/")
public class MyController {
#PreAuthorize("hasAnyRole('ROLE_USER') && hasAnyAccess('FULL_ACCESS')")
#RequestMapping(value = "hello", method = RequestMethod.GET)
public String hello(#RequestParam String name) {
return "Hello '" + name + "' at " + System.currentTimeMillis() + "\n";
}
}
Update 1:
The same SpelEvaluationException happens when the SB app is launched with
mvn spring-boot:run
Update 2:
From the logs it seem when launching from IDE my MethodSecurityConfig extension is overridden by GlobalMethodSecurityConfiguration:
INFO 5927 --- [ main] o.s.b.f.s.DefaultListableBeanFactory :
Overriding bean definition for bean 'methodSecurityInterceptor' with a different definition:
replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=methodSecurityConfig; factoryMethodName=methodSecurityInterceptor; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [com/example/MethodSecurityConfig.class]]
with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; factoryMethodName=methodSecurityInterceptor; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]]
INFO 5927 --- [ main] o.s.b.f.s.DefaultListableBeanFactory :
Overriding bean definition for bean 'methodSecurityMetadataSource' with a different definition:
replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=methodSecurityConfig; factoryMethodName=methodSecurityMetadataSource; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [com/example/MethodSecurityConfig.class]]
with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; factoryMethodName=methodSecurityMetadataSource; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]]
When launching from JAR my MethodSecurityConfig extension overrides the GlobalMethodSecurityConfiguration:
INFO 6092 --- [ main] o.s.b.f.s.DefaultListableBeanFactory :
Overriding bean definition for bean 'methodSecurityInterceptor' with a different definition:
replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; factoryMethodName=methodSecurityInterceptor; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]]
with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=methodSecurityConfig; factoryMethodName=methodSecurityInterceptor; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [com/example/MethodSecurityConfig.class]]
INFO 6092 --- [ main] o.s.b.f.s.DefaultListableBeanFactory :
Overriding bean definition for bean 'methodSecurityMetadataSource' with a different definition:
replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration; factoryMethodName=methodSecurityMetadataSource; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.class]]
with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=methodSecurityConfig; factoryMethodName=methodSecurityMetadataSource; initMethodName=null; destroyMethodName=(inferred);
defined in class path resource [com/example/MethodSecurityConfig.class]]
After some investigation it turned out the #EnableGlobalMethodSecurity(prePostEnabled = true) annotation specified for both
WebSecurityConfigurerAdapter extension and
GlobalMethodSecurityConfiguration extension
causes the problem. Removing the annotation from WebSecurityConfigurerAdapter solves the issue.

Spring-Boot disable load-on startup rabbitMQ

Why RabbitMQ is triggered or running while building the spring-boot jar.While running the Application.java or pom.xml.
i am able to see the following loggers
2016-07-01 16:40:04.334 INFO 7004 --- [ main] com.rabbit.App : No active profile set, falling back to default profiles: default
2016-07-01 16:40:04.391 INFO 7004 --- [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext#1da51a35: startup date [Fri Jul 01 16:40:04 CEST 2016]; root of context hierarchy
2016-07-01 16:40:05.331 INFO 7004 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'rabbitListenerContainerFactory' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=rabbitMqConfiguration; factoryMethodName=rabbitListenerContainerFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/rabbit/messaging/configuration/RabbitMqConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=taskConsumerConfiguration; factoryMethodName=rabbitListenerContainerFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/rabbit/messaging/configuration/TaskConsumerConfiguration.class]]
2016-07-01 16:40:05.334 INFO 7004 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'connectionFactory' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=rabbitMqConfiguration; factoryMethodName=connectionFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/rabbit/messaging/configuration/RabbitMqConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=taskConsumerConfiguration; factoryMethodName=connectionFactory; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/rabbit/messaging/configuration/TaskConsumerConfiguration.class]]
2016-07-01 16:40:05.334 INFO 7004 --- [ main] o.s.b.f.s.DefaultListableBeanFactory : Overriding bean definition for bean 'jsonMessageConverter' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=rabbitMqConfiguration; factoryMethodName=jsonMessageConverter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/rabbit/messaging/configuration/RabbitMqConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=taskConsumerConfiguration; factoryMethodName=jsonMessageConverter; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [com/rabbit/messaging/configuration/TaskConsumerConfiguration.class]]
2016-07-01 16:40:05.868 INFO 7004 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration' of type [class org.springframework.amqp.rabbit.annotation.RabbitBootstrapConfiguration$$EnhancerBySpringCGLIB$$ad9295b0] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2016-07-01 16:40:06.657 INFO 7004 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
Is there any option to disable this?
While building the jar during test phase it throws following exception.
LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor#76dc36e5]
2016-06-30 15:10:32.989 WARN 21614 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it
org.springframework.amqp.AmqpIOException: java.net.SocketTimeoutException: connect timed out
at org.springframework.amqp.rabbit.support.RabbitExceptionTranslator.convertRabbitAccessException(RabbitExceptionTranslator.java:67)
at
You get the log entry 'Overriding bean definition..' when you define a bean with that name more than once.
This leads to the question, which of the, at least two, beans do you use and how are they configured?
Set breakpoint(s) on the constructor(s) of the rabbitListenerContainerFactory bean and see which instances you are creating.
Looking up the stack trace may give you information about why this bean is created, so you can remove the duplicate from your spring configuration.
You can also add breakpoints, where the rabbit connection properties are set.
"connect timed out" also appears, when the given ip is wrong.
Anyway I would not recommend to set the properties in the build script, instead point to the property files on the command line, when you start the application. That allows you to use your build in different environments.
See howto-properties-and-configuration
Why it is throwing while building the jar?
You wrote you are setting the properties with a script during build.
This means that the tests are using those properties and now the tests are failing, may be because the rabbit mq server is not reachable with the provided properties or is not up.
As long as you are overriding beans, you do not really know which instance you use, so you dont really know which properties you use.
Try to log it out, than I am sure you will see....
You can't "lazy load" beans that implement SmartLifecycle (such as the listener container) because the context has to load the bean to call isAutoStartup() to see whether or not the bean should be started.
If the context is being loaded while "building the jar", you must have test cases that are using the beans. You can set autoStartup to false - but your tests will probably fail then.
You can skip running the tests with -DskipTests=true (maven) or -x test (gradle).
I have the same problem in my tests. I don't use rabbit and because the timeouts occur the tests duration increases.
As a simple workaround, you can disable the configuration of queues, exchanges etc. during tests using spring profiles.
Example:
Having the configuration that is disabled if the spring profile "no_rabbit" is present.
#Configuration
#Profile("!no_rabbit")
public class RabbitMQConfig {
#Bean
public Queue queue() {
return QueueBuilder.durable("NAMW").withArgument("x-message-ttl", 3600).build();
}
// more definitions of bean, exchanges etc.
}
Then, in the test simply add the profile "no_rabbit".
#RunWith(SpringRunner.class)
#ActiveProfiles({ "no_rabbit" })
public class TestClass {
...
}
The drawbacks of this approach is that you have to add the profile in every single test that don't uses rabbit. But for my case it's ok until I have a better solution.

Resources