spring boot to Tomcat deployment data source issue - spring

I am building an application with Spring Boot, Neo4j and MongoDB. I have used #Configuration annotation to load database properties from application.properties like this:
#Bean(name = "neoDriver")
#Profile("local")
public Driver getNeoDriver() {
return GraphDatabase.driver(
env.getProperty("datasource.neo4j.uri"),
AuthTokens.basic(env.getProperty("datasource.neo4j.username"), env.getProperty("datasource.neo4j.password")),
Config.build().toConfig());
}
The autowire code is
#Autowired
#Qualifier("neoDriver")
private Driver neoDriver;
when I run the application from IntelliJ, it runs fine; but when I try deploy the war file on Tomcat 8.5, it gives error.
Field neoDriver in com......repository.PositionRepositoryImpl required a bean of type 'org.neo4j.driver.v1.Driver' that could not be found.
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Controller': Unsatisfied dependency expressed through field 'positionService'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'Service': Unsatisfied dependency expressed through field 'positionRepository'; nested exception is
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'RepositoryImpl': Unsatisfied dependency expressed through field 'neoDriver'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.neo4j.driver.v1.Driver' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations:
{#org.springframework.beans.factory.annotation.Autowired(required=true), #org.springframework.beans.factory.annotation.Qualifier(value=neoDriver)}
Please let me know what did I do wrong when deploying this on Tomcat.

I bet you're not setting spring.profiles.active to local in your Tomcat deployment. Because your bean is marked with the #Profile annotation, so it will only be created if that profile is active.

Related

Spring Cloud Config Server with GIT Repo configured is not starting up successfully

I am building Spring Cloud Config server with Sprint Boot v2.6.12 and Spring Cloud Config Server v3.1.1. Below is the application.yml configuration,
spring:
application:
name: config-server
main:
allow-bean-definition-overriding: true
cloud:
config:
server:
git:
uri: git#github.aus.thenational.com:demo/case_configuration.git
skip-ssl-validation: true
default-label: master
clone-on-start: true
clone-submodules: true
force-pull: true
refresh-rate: 60
search-paths:
- case/folder1
- case/folder2
Exception we are getting while server startup is as below,
2022-12-21 09:51:10.827 WARN 11524 --- [main] [CID:] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthContributorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthContributorRegistry' parameter 2; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'configServerHealthIndicator' defined in class path resource [org/springframework/cloud/config/server/config/EnvironmentRepositoryConfiguration$ConfigServerActuatorConfiguration.class]: Unsatisfied dependency expressed through method 'configServerHealthIndicator' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'searchPathCompositeEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/CompositeConfiguration.class]: Unsatisfied dependency expressed through method 'searchPathCompositeEnvironmentRepository' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'defaultEnvironmentRepository' defined in class path resource [org/springframework/cloud/config/server/config/DefaultRepositoryConfiguration.class]: Unsatisfied dependency expressed through method 'defaultEnvironmentRepository' parameter 1; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'multipleJGitEnvironmentProperties' is expected to be of type 'org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties' but was actually of type 'com.sun.proxy.$Proxy140'
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'multipleJGitEnvironmentProperties' could not be injected because it is a JDK dynamic proxy
The bean is of type 'com.sun.proxy.$Proxy140' and implements:
org.springframework.cloud.config.server.support.HttpEnvironmentRepositoryProperties
org.springframework.cloud.config.server.support.EnvironmentRepositoryProperties
org.springframework.aop.SpringProxy
org.springframework.aop.framework.Advised
org.springframework.core.DecoratingProxy
Expected a bean of type 'org.springframework.cloud.config.server.environment.MultipleJGitEnvironmentProperties' which implements:
Action:
Consider injecting the bean as one of its interfaces or forcing the use of CGLib-based proxies by setting proxyTargetClass=true on #EnableAsync and/or #EnableCaching.
I tried configuring http and https proxy settings along with other GIT configurations. But that didnt work.
Expectation is, config server should be up successfully.
Tried to solve the issue by configuring spring AOP related configurations in application.yml , but that didnt work too.
Seems like something something to do with libraries versions.

NoSuchBeanDefinitionException `HttpSecurity` after adding `spring-restdocs-mockmvc` dependency

I have a spring boot application which have different folders for repository, service and api controller. The DB is Postgress and we are using Gradle as our build tool.
The test cases for repository were working fine, but as soon as i added test cases for controller layer (by also adding the dependency
testImplementation("org.springframework.restdocs:spring-restdocs-mockmvc:2.0.6.RELEASE")
my repository tests start to fail with the below exception
Caused by: java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at org.springframework.test.context.junit.jupiter.SpringExtension.getApplicationContext(SpringExtension.java:283)
at org.springframework.test.context.junit.jupiter.SpringExtension.resolveParameter(SpringExtension.java:269)
at org.junit.jupiter.engine.execution.ExecutableInvoker.resolveParameter(ExecutableInvoker.java:216)
... 70 more
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'managementSecurityFilterChain' defined in class path resource [org/springframework/boot/actuate/autoconfigure/security/servlet/ManagementWebSecurityAutoConfiguration.class]: Unsatisfied dependency expressed through method 'managementSecurityFilterChain' parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800)
at app//org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:541)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582)
at app//org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at app//org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at app//org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953)
at app//org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at app//org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at app//org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at app//org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at app//org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at app//org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:132)
at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
at app//org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
... 74 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1799)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1355)
at app//org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1309)
at app//org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:887)
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:791)
... 92 more
As soon as I remove the above dependency, my repository test cases work again but I need this dependency for my controller tests. Since we don't have modules, I cannot have individual gradle files and import the dependency there. Any suggestion how can I fix this?
I was thinking about excluding the specific module from spring-restdocs-mockmvc which is causing the issue but I don't know which module is the reason.

How to use the default keycloak.json instead of spring properties

I was following this example to set up keycloak with springboot
https://www.baeldung.com/spring-boot-keycloak
I have used the bellow bean so I can use spring boot properties file.
which all worked fine.
But if I wanted to use the Keycloak.json file instead of spring boot properties, what I did was commented out the KeycloakSpringBootConfigResolver been and included the keycloak.json file in resources/WEB-INF/keycloak.json. but I keep getting the following error.
am I missing something? how do I use the keycloak.json file and where should this file go? or is there some configuration I missed? all the examples show using spring properties and not json file. Any ideas? or if you could point me to an example that uses the default keycloak.json
Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.keycloak.adapters.springboot.KeycloakAutoConfiguration': Unsatisfied dependency expressed through method 'setKeycloakSpringBootProperties' parameter 1; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.keycloak.adapters.springboot.KeycloakSpringBootConfigResolver' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
#Bean
public KeycloakSpringBootConfigResolver KeycloakConfigResolver() {
return new KeycloakSpringBootConfigResolver();
}
Add this to your configuration file:
keycloak:
configurationFile: "classpath:keycloak.json"
Put the keycloak.json directly into the resources directory.
Use only keycloak adapter as dependency: org.keycloak:keycloak-spring-security-adapter

Flyway integration(Enterprise Version) in Spring Boot problem

Hello i got this error when i use flyway enterprise in my springboot project:
16:27:46 WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.scheduling.annotation.ProxyAsyncConfiguration': Unsatisfied dependency expressed through method 'setConfigurers' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springAsyncConfig': Unsatisfied dependency expressed through field 'customAsyncExceptionHandler'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAsyncExceptionHandler': Unsatisfied dependency expressed through field 'importHistoryRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'importHistoryRepository': Cannot create inner bean '(inner bean)#31db34da' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#31db34da': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration$FlywayConfiguration': Unexpected exception during bean creation; nested exception is java.lang.TypeNotPresentException: Type org.flywaydb.core.api.callback.FlywayCallback not present
Here is my pom.xml file:
<dependency>
<groupId>org.flywaydb.enterprise</groupId>
<artifactId>flyway-core</artifactId>
<version>6.0.3</version>
</dependency>
Related to this Issue Flyway 6.x.x is not compatible with Spring Boot < 2.2.0.
So you got 2 Choices:
Update Spring Boot to >= 2.2.0
Use flyway 5.x.x
Hope this helps

how to resolve UnsatisfiedDependencyException with spring boot hibernate?

I am getting the following issue when Spring boot hibernates Connection.
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookController': Unsatisfied dependency expressed through field 'bookService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImp': Unsatisfied dependency expressed through field 'bookDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookDaoImp': Unsatisfied dependency expressed through field 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getSessionFactory' defined in class path resource [com/go/myapp/Config/AppConfig.class]: Invocation of init method failed; nested exception is org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
If you are building your project with maven look for the pom file. when you add dependencies there are Compile Dependencies for those dependencies. For an Example SpringBoot Starter 1.5.8.Release has https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter/1.5.8.RELEASE
SpringBoot 1.5.8.Release
SpringCore 4.3.12.Release
So please check your dependencies and there compile dependencies .
posting your POM file would help

Resources