circular dependency error for dataSource after adding spring DATA JPA - spring-boot

I am working in spring boot security with Oauth2. Oauth2 to makes use of jdbcAuthentication in AuthorizationServerConfigurerImpl. But in my webSecurityCofigurer implementing class i want to use Spring data JPA to implement userDetailsService. After enabling #EnableJpaRepositories it is throwing circular depenency error and not able to load dataSource which i am defining in AuthorizationServerConfigurerImpl.
Please help me resolving issue

Related

Spring boot Jpa is not working with Spring batch and spring integration

I am working with spring batch. I needed to add some jpa repositories. So previously i was using JDBCTemplate which was working fine.
But when I started working with JPA, the spring boot application could not find the repos. Which were there.
#Autowired
ClassLevelConfigRepo clcr;
I checked these things as the best practices.
Added #EnableJpaRepositories in springBoot application class.
Added #Repostiories to the repository interfaces.
extended the interfaces with JpaRepository<Account, String>
Added #Entity to the entity classes and defined the #Table and # Column annotations properly.
But I am still getting below error.
Field clcr in com.cloudtask.batchconfig.util.LhmUtility required a bean of type 'com.cloudtask.batchconfig.repo.ClassLevelConfigRepo' that could not be found.
I tried checking all the dependencies in pom.xml it was as per recommended. And I have all the tables defined properly in data base.
I was expecting the application to return the Autowired clcr object propely.
Edit 1 : spring boot application annotations
#SpringBootApplication
#ComponentScan({"com.cloudtask"})
#EnableAsync
#IntegrationComponentScan({"com.cloudtask"})
#EnableIntegrationManagement(defaultLoggingEnabled = "true")
#EnableJpaRepositories
#EntityScan
public class imclassApplication ```
When you work with Spring Data Jpa with those basic points you should also keep track of below points.
you have added spring-boot-starter-data-jpa in your pom.xml
you have added the entity and repo package below one level of the application package.
If you the package is at same level you should specify the exact package details in the annotation. in your case it should be like :
#EnableJpaRepositories("com.cloudtask.batchconfig.repo")
#EntityScan(basePackages = {"com.cloudtask.batchconfig.entity"})
Happy programming!

How to access datasource information in spring boot with spring data jpa and hibernate

I need to perform an health check on my application that uses spring boot with spring data jpa and hibernate.
I need to do this with jpa and not make specific to any implementation.
In EclipseLink we can use EntityManagerFactoryDelegate as shown below, but I dont know How to do this with spring data jpa and hibernate.
EntityManagerFactoryDelegate delegate = entityManager.getEntityManagerFactory().unwrap(EntityManagerFactoryDelegate.class);
PersistenceInfo = delegate.getSetupImpl().getPersistenceUnitInfo();
DataSourceImpl dataSource = (DataSource) info.getNpnJtaDataSource();
return dataSource.getName();
Can anyone suggest me how to do this in spring data jpa using hibernate.
You can use spring boot actuator dependency for healthcheck doesn't need to configure externally. Once you define Datasource Bean it will auto pick database healthcheck.
if you want to enable/disable database health check you can use the below property,
management.health.db.enabled=<boolean, true || false>
implementation reference :https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

How to register a hibernate typedescriptor in a "classic" Spring 5 application with jpa entitymanager

I have a classic spring 5 application (NO spring boot). For a custom basic type i have created a typedescriptor by subclassing AbstractTypeDescriptor according to Hibernate 5.2 Documentation - 2.3.5. Explicit BasicTypes. I am using hibernate via an jpa entitymanager. I have two spring beans configured for that: a LocalContainerEntityManagerFactoryBean and a HibernateJpaVendorAdapter.
Now i am a bit lost, how to register the typedescriptor during startup of the application. The docs mention an approach using a hibernate configuration object. But i have no idea, how to get this? Any help appreciated!

Spring Boot Scanning Classes from jars issue

In my sample spring boot application, i have added a dependency of a custom jar. My sample application has a support for web and jpa.
The jar which i've created contains a Spring MVC controller. Below is the sample code
#Controller
public class StartStopDefaultMessageListenerContainerController {
#Autowired(required=false)
private Map<String, DefaultMessageListenerContainer> messageListeners;
I haven't manually created a bean instance of this controller anywhere in my code.
Problem - When i start my spring boot application by running the main class, i get an error in console that prob while autowiring DefaultMessageListenerContainer.
My question here is, even though this class StartStopDefaultMessageListenerContainerController is just present in the classpath, it's bean shouldn't be created and autowiring should not happen. But spring boot is scanning the class automatically and then it tries to autowire the fields.
Is this the normal behavior of spring and is there anyway i can avoid this?
If the StartStopDefaultMessageListenerContainerController class is part of component scanning by spring container, Yes spring tries to instantiate and resolve all dependencies.
Here your problem is #Autowired on collection. Spring docs says,
Beans that are themselves defined as a collection or map type cannot be injected through #Autowired, because type matching is not properly applicable to them. Use #Resource for such beans, referring to the specific collection or map bean by unique name.
And also Refer inject-empty-map-via-spring

create beans with annotation spring

In struts2 i almost did not use any xml configs and used much of annotations for MVC. I build a small application in that way using struts2. Now i want to develop same project using spring 3.2. Now i want to use annotation to create beans and request mapping (this i used). I want a clear example of using bean annotations and is it possible to set properties using annotations. I am getting confused because the documentation is too large, and many annotations. providing a simple list of annotations and their usage with simple example will be a great help.
Iam doing sample project on Spring 3.1.
I have used some annotations to create beans.Below are the annotations i have used.
#Component - Annotation used to create a bean given by Spring
#Resource,#Bean
JSR Annotations: #Controller,#Repository, #Service
If you are annotating your class with above annotations Spring Container will create beans for you.
Your properties will be set with help of #Autowired annotation.

Resources