When I define a datasource ,what's the different between server.xml datesource and application.properties - spring-boot

My application runs on liberty application server, using springboot .And I define a datasource in server.xml file , and also I define the datasource in application.properties.
What's the different between the two file?

Related

How to load datasource from hibernate.cfg.xml file when launching spring boot app

Intro :
My app contains 2 datasource, i used tomcat context.xml to configure them and it's working fine,
but now i need to launch the app as a spring-boot application,
one of datasource which is declared in hibernate.cfg.xml file doesn't seem to be loaded properly
What i tried:
i created a bean for the second data source which is in hibernat.cfg.xml file (same as i did for the first datasource) in a class annotated #configuration but i got this error:
Error parsing JNDI name [java:/comp/env/jdbc/mySecondDB]
i tried also to modify the jndi value to : java:comp/env/jdbc/mySecondDB
but it's not working
how i can load this 2nd datasource from the hibernate.cfg.xml file ?

Define properties in a custom file, for Spring data JPA

I have a custom properties file for my spring boot application for database configurations. I use spring data JPA for persistence, By default spring boot choose application.properties to load the configurations.
How to avoid it and use any custom file, to read the database configuration on application startup
Example : database-connection.properties
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://ip:5432/HP
spring.datasource.username=hpadmin
spring.datasource.password=hp#12345
Thank you
You will avoid future complications if you use just one and the classic application.properties.
Spring requires more than just database configurations so replace the entire application.properties by a simple database-connection.properties is not a good idea.
Anyway, if you need to add more properties alongside application.properties you could use
one of the following approaches:
PropertyPlaceholderConfigurer
You could load extra properties alongside the application.properties or replace it entirely if you delete the application.properties
In your main class:
#Configuration
public class PropertiesConfiguration {
#Bean
public PropertyPlaceholderConfigurer properties() {
final PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
ppc.setIgnoreResourceNotFound(true);
final List<Resource> resourceLst = new ArrayList<Resource>();
resourceLst.add(new ClassPathResource("database-connection.properties"));
ppc.setLocations(resourceLst.toArray(new Resource[]{}));
return ppc;
}
Shell
You could replace the entire application.properties file at the moment of run of your jar with spring parameter --spring.config.location
java -jar app.jar --spring.config.location=/foo/bar/database-connection.properties
PropertySource
Similar to PropertiesConfiguration but without java code, just #annotations
#PropertySources({#PropertySource(value = "classpath:database-connection.properties")})
public class Application {

Set a Spring Boot property using a value from JNDI

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}

Is it possible to use Spring boot with web.xml and annotation based configurataion

I want to use Spring Boot with web.xml and servlet 3.1 configuration.Is there any example?
I want to define my context(Dispatcher servlet/SpringBootServletInitializer) in Web.xml mean time define all other configuration using annotation based.Ex want to load application.properties/yml values using pojos.
Need this type of configuration to deploy the app in Liberty profile as Liberty expecting application context in web.xml when using liberety global sharelib.

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