Set a Spring Boot property using a value from JNDI - spring

How can I put a string stored in JNDI to Spring Boot properties?
A bit more details:
I get my DataSource from JNDI using this property in application.properties:
spring.datasource.jndi-name=my_data_source_jndi_name. Currently, the DB schema is hard coded in application.properties in the following way: spring.jpa.properties.hibernate.default_schema=my_schema. There is a JNDI record that containd the DB schema name. How can I get the schema from JNDI too?

As outlined here:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
Spring will resolve properties from JNDI and so you can therefore parameterise it in various ways. You can bypass the properties file and use the #Value() annotation directly in Java config
#Value("my.property.in.jndi")
private String schema;
or you can do as below in the properties file:
spring.jpa.properties.hibernate.default_schema=${my.property.in.jndi}

Related

Read YAML file like properties

I am developing a spring boot project, and I need some external configuration. So I am storing that in a file called "config.yml" placed in "src/main/resources" folder.
Now I want the properties in config.yml to be injected in my class. Now if we use SnakeYML or any other parser, we would need to make Java classes to define the schema.
What I want is I can read the yml just like properties using #Value annotation. For e.g.
logging:
class:
name:
location:
I need to access "name" or "location" property using
#Value(${logging.class.name})
private String name;
Is there a way to do that in spring boot?
you don't need to add separate yml. you add custom properties to application.yml or application-{env}.yml
Spring recognise it and you can it via
#Value
Spring environment
Using Spring ConfigurationProperties

How to access defined in the application.properties file in Spring Boot?

I want to access values provided in application.properties, e.g.:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
I want to access userBucket.path in my main program in a Spring Boot application
You can use the #Value annotation and access the property in whichever Spring bean you're using
#Value("${userBucket.path}")
private String userBucketPath;

Properties resolver in Spring

I am having following properties in a file-
dir.path=path/of/directory
file.path=${dir.path}/file_name
In Spring Bean, I am referring it as given below -
#Value("${file.path}")
String filePath;
However filePath is set by Spring as '${dir.path}/file_name' and it does not resolved completely. I am using Java Configuration ( no xml). I will appreciate all suggestions.

Configure data source in spring and mapping in hibernate

Is it possible to configure to DataSource configuration in spring xml and keep the resource mapping in hibernate configuration file.
You can find an example in the Spring reference (http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#orm-hibernate).

SpringBoot DataSource configuration

I'm trying to use the application.properties file to configure the datasource Spring Boot will have to use.
I've put the following properties inside :
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.user=test
spring.datasource.password=test
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/test
The application.properties file is well used by other systems. But I can't get it to work for the automatic datasource configuration.
I'm still getting this Exception :
org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE.
The postgresql driver is included and loaded. And I can configure the datasource using a Configuration class, and the same parameters as above.
I've also added the #EnableAutoConfiguration and #EnableJpaRepositories to my Application.class.
Any clues?
You should use spring.datasource.url to configure the JDBC URL rather than spring.datasource.jdbcUrl.
spring.datasource.jdbcUrl will work if the specific DataSource implementation that you're using has a setJdbcUrl method (HikariCP, for example) where as spring.datasource.url will work with any of the supported datasources.
Using spring.datasource.url also has the added benefit that you don't need to specify spring.datasource.driverClassName as it will be inferred from the url.

Resources