How to reproduce Spring Boot Configuration POJO creation but in Spring Core (No boot) - spring

The Context
As described in the Spring Boot documentation, one of the ways to create a POJO that holds configuration properties values is:
//On the configuration class
#EnableConfigurationProperties(value = {
POJO_CLASS_HERE.class
})
//On the POJO Class
#ConfigurationProperties(prefix = "PROPERTIES_PREFIX_HERE")
#ConstructorBinding
It works fine - when you have Spring boot in the project.
Now, I have another project that has only Spring V4 (core + batch) - No boot.
The Question
How to use/reproduce the Configuration POJO creation of Spring Boot in Spring core ?
Spring Boot - Type Safe Configuration properties

Related

How to register ApplicationContextInitializer in Spring boot 2.7/3

I'm using Spring boot 2.7.7 and Spring boot 3.
I've already switched to the new way write my custom autoconfiguration class - that is with #AutoConfiguration and org.springframework.boot.autoconfigure.AutoConfiguration.imports inside META-INF/spring folder.
I want to add ApplicationContextInitializer. How do I do it the last spring boot versions (2.7.7 and 3)? I tried to add it to org.springframework.boot.autoconfigure.AutoConfiguration.imports file but initialize method is not being called.

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

"httptrace" endpoint of Spring Boot Actuator doesn't exist anymore with Spring Boot 2.2.0

With Spring Boot 2.2.0 the "httptrace" Actuator endpoint doesn't exist anymore. How can I get this functionality back?
The functionality has been removed by default in Spring Boot 2.2.0.
As a workaround, add this configuration to the Spring environment:
management.endpoints.web.exposure.include: httptrace
and provide an HttpTraceRepository bean like this:
#Configuration
// #Profile("actuator-endpoints")
// if you want: register bean only if profile is set
public class HttpTraceActuatorConfiguration {
#Bean
public HttpTraceRepository httpTraceRepository() {
return new InMemoryHttpTraceRepository();
}
}
http://localhost:8080/actuator/httptrace works again.
You need to enable httptrace by having following application properties. By default it is disabled
management.trace.http.enabled: true
management.endpoints.web.exposure.include: httptrace
and Requires an HttpTraceRepository bean. You can use Your own Custom implementation or InMemoryHttpTraceRepository

Is there any bean created for spring #propertySource annotation

I have used spring #propertysource annotation to load properties file in my spring boot project . I just need to know is there any bean created for this annotation when application gets started
#PropertySource annotation is used to let spring-boot know the location of the properties required by your application. It will not create any bean.

Spring boot #ConfigurationProperties not loading from external properties file or class path property file

Hi I am trying to use a reusable library that I have created using spring boot(2.0.5) from 2 applications I am able to bind properties from application.properties which is in my classpath to my bean as follows and I see the schemas being set through the setters in my debug in my first spring batch application which is also created with spring boot(2.0.5)
This is the property bean class in my library which holds some service api- this library is just a jar package created with spring boot.
package com.test.lib.config
#ConfigurationProperties("job")
#PropertySources({
#PropertySource(value = "${ext.prop.dir}", ignoreResourceNotFound = true),
#PropertySource(value = "classpath:application.properties", ignoreResourceNotFound = true)
})
public class ServiceProperties {
/**
* Schema for the service
*/
private String schema;
public String getSchema() {
return schema;
}
public void setSchema(String schema) {
this.schema = schema;
}
}
And the config bean for this library is as follows in the same package.
#Configuration
#EnableConfigurationProperties(ServiceProperties.class)
#ComponentScan("com.test.lib")
public class LibraryModuleConfig {
}
This code works perfectly fine when called from a sprint boot spring batch application which includes this library as a dependency and the corresponding setters are called and I can see the schema set when I add job.schema=testSchema in application.properties
I try to use this same library in an existing spring mvc web application started from a tomcat server with external files directory as launch arguments( This application was not created with spring boot) and added the appropriate context:component-scan to include the beans (java config beans) from the library in the application-context(appn-context.xml). The job.schema property is passed from both application.properties file and even a external file in C drive as given by the ${ext.prop.dir}" in the #propertySources annotation. The schema property in ServiceProperties Bean never gets set and even the setters never get called in the debug. Why will this libray config bean not work in an existing spring mvc application but work with the spring batch application. Both of them add the library as a dependency. I have been at this at a long time and except for the spring property binding the other functionality seem to work.
#EnableConfigurationProperties is a handy annotation that Spring Boot provides. (Spring MVC doesn't provides in default)
For a legacy Spring MVC app (specifically for Spring 3.x), you can use #Value annotation for the properties.
#Value annotation also works in Spring Boot so I guess you can make a change so that it works with older version (Spring 3.x) and newer version just works without changing anything at all.
Hope this helps! Happy Coding :)

Resources