I've discovered that spring boot silently ignores all datasource properties loaded through org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.
My application.yml contains:
spring:
datasource:
name: this is not ignored
url: this is ignored
During startup i see line:
o.s.j.d.e.EmbeddedDatabaseFactory : Creating embedded database 'this is not ignored'
Debugging shows that DataSourceProperties object has not any properties set during call to getUrl (debugging also shows that exception is thrown but it is silently ignored because it is not visible in logs).
Url is obvoiusly invalid but database is somehow initialized.
Tested on 1.2.7 version.
What can cause such strange behaviour? How can I debug it further?
Adding
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
to dependencies solved problem.
Problem was caused by Spring Boot autoconfiguration magic - I was not using correct spring boot starter.
Related
I use Spring boot 2.3.0.RELEASE.
application.properties
spring.profiles.active=dev
logging.config=classpath:logger/logback-spring.xml
logging.file.dir=reception-electronic-docs
logging.file.name.var=reception-electronic-docs.log
logging.file.archive.format.name=reception-electronic-docs.%d{dd-MM-yyyy}.log
Previously this code worked (Srping boot 2.2.5.RELEASE).
I use multi module structure in the project. But there, the Central pom does not manage the entire project. I create microservices.
During start up an application encounter an error:
Logging system failed to initialize using configuration from 'classpath:logger/logback-spring.xml'
java.io.FileNotFoundException: class path resource [logger/logback-spring.xml] cannot be resolved to URL because it does not exist
at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:137)
What are any ideas to correct this?
I add an dependencies
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.1.2</version>
</dependency>
But I don't understand reason.
After I removed the dependency. But my app runs without errors. I'm at a loss.
I have a Spring Webflux application where I am trying to load a dependency from an old module (old module is on Spring WebMVC framework).
When the application is launched, this error is thrown -
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'requestMappingHandlerAdapter', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/web/reactive/config/DelegatingWebFluxConfiguration.class] and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
I want all the beans from webflux package to be initiated, so I can't set spring.main.allow-bean-definition-overriding=true.
Also tried excluding all classes within org.springframework.boot at the time of component scan - #ComponentScan(excludeFilters = #Filter(type = FilterType.REGEX, pattern = "org.springframework.boot*").
Also tried excluding all spring packages in pom.xml of my webflux project like this -
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
Since I cannot modify the older dependency project to webflux, are there any options I could use the make the code work ?
In your springboot startup class , the #EnableAutoConfiguration annotation will auto configure the mvc part (WebMvcAutoConfiguration will fail due to same bean name in DelegatingWebFluxConfiguration)
So try ti exclude this from auto config and try as below
#SpringBootApplication
#EnableAutoConfiguration(exclude = {WebMvcAutoConfiguration.class })
public static void main(String[] args) {
...
SpringApplication.run(MyApp.class, args);
}
If I understand correctly you have some Web-related dependencies on the classpath but aren't building a web application, you can explicitly tell SpringApplication that you don't want a web application:
app.setWebEnvironment(false);
This is the way to disabling Web-related auto-configuration as it means you don't need to know what those auto-configuration classes are and lets Spring Boot take care of it for you.
As mentioned in your problem description, Using dependencies of Spring MVC in Spring Webflux can cause this issue. I have solved this issue by excluding the group "org.springframework.boot" while including the old dependency.
In gradle.build i have did something like below:
implementation("dependency-using-spring-mvc") {
exclude(group= "org.springframework.boot")
}
I have a spring boot application that's packaged as a .war file and running on a wildfly instance.
I just recently added the micrometer and actuator dependencies to my pom file:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
When I deploy the app to wildfly and it's up and running I can access the default endpoints /info and /health without any problems on the following urls:
http://localhost:8080/myApp/info
http://localhost:8080/myApp/health
But no matter what I do I cannot access /actuator or any of the other endpoints.
I'm currently using an external config file that maps custom properties to a Config class in my app. That config file also contains spring boot properties like these that are not mapped to the config class:
###################################################################
# Spring Boot properties
###################################################################
spring.http.multipart.max-file-size=500Mb
spring.http.multipart.max-request-size=500Mb
logging.level.org.springframework.web=DEBUG
logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=DEBUG
server.error.whitelabel.enabled=false
Those parameters are picked up by spring boot without any problems.
I've also added the following actuator properties to the same file:
###################################################################
# Actuator Properties
###################################################################
management.endpoints.web.exposure.include=*
management.endpoint.prometheus.enabled=true
management.endpoint.metrics.enabled=true
management.endpoint.status.enabled=true
management.endpoint.prometheus.show-details=always
management.endpoint.metrics.show-details=always
management.endpoint.status.show-details=always
management.endpoint.health.show-details=always
These properties make no difference, I can only ever access /info and /health
What am I missing?
Edit: There is currently no security module in this application.
If you are using spring boot version below 2.0 then try disabling security by using
management.security.enabled= false in properties file
I build some simple spring boot project, with jpa/postgres.
But when i debug this project, error say
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
But in my application.properties that have url.
spring.datasource.url=jdbc:postgresql://localhost:5432/oauth?currentSchema=oauth
spring.datasource.username=SOME_USER_NAME
spring.datasource.password=SOME_PASSWORD
spring.datasource.driverClassName=org.postgresql.Driver
spring.jpa.hibernate.ddl-auto=validate
#hibernate
spring.jpa.database = postgresql
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true
Does anyone know about this?
if validation is trouble than debug don't say like 'url attribute is not specified'.
And My application.properties name 'application-test.properties', and in debug configuration set 'test', and also log say 'the profiles test are currently active'.
It looks like properties is not matching, but log said profile is matched.
It makes me very confuse..
I found problem. Jonathan Johx and TinyOS is right, Thanks for them.
If project built by spring boot 2.1.1, Intellij with postgres, then Override build.gradle and pom.xml.
In Spring-boot 2.1.1 (I still don't know why default value is it)
default setting is
runtime('org.postgresql:postgresql')
but If you want to run right, than must write 'compile' like this.
compile group: 'org.postgresql', name: 'postgresql', version: '42.2.5'
version is not important. just find in maven repo what you want version.
While I was using Spring Boot 1.5, on application startup Hibernate executed schema.sql file located in /resources folder when appropriate configuration is set. After Spring Boot 2.0 release this feature does not work any more. I couldn't find anything about this change in documentation.
Here is my application.properties file content:
spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
#spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
Is there some change in Spring Boot 2.0 or is this an bug/issue?
Check the documents here.
In a JPA-based app, you can choose to let Hibernate create the schema
or use schema.sql, but you cannot do both. Make sure to disable
spring.jpa.hibernate.ddl-auto if you use schema.sql.
You have spring.jpa.hibernate.ddl-auto=create-drop that's why schema.sql is not executed.
Looks like this is the way Spring Boot works.
Edit
I think that the problem(not really a problem) is that your application points to a mysql instance.
See the current Spring Boot properties:
spring.datasource.initialization-mode=embedded # Initialize the datasource with available DDL and DML scripts.
The default value is embedded - e.g. initialize only if you're running and embedded database, like H2.
Also see the answer of Stephan here. He said:
Adding spring.datasource.initialization-mode=always to your project is
enough.
So try to set:
spring.datasource.initialization-mode=always
Not embedded (e.g. MySQL)
If you load a database that is not embedded, in Spring Boot 2 you need to add:
spring.datasource.initialization-mode=always
Check the Migration Guide:
Database Initialization
Basic DataSource initialization is now only enabled for embedded data
sources and will switch off as soon as you’re using a production
database. The new spring.datasource.initialization-mode (replacing
spring.datasource.initialize) offers more control.
Embedded (e.g. h2)
I once had a similar problem, even though it was an h2 (so it was an embedded DB), my h2 configuration was activated by a my-test profile.
My test class was like:
#RunWith(SpringRunner.class)
#SpringBootTest // does not work alone
#ActiveProfiles("my-test")
public class MyEntityRepositoryTest {
The problem is #SpringBootTest alone did not initialize the test database. I had to either use #DataJpaTest or #SpringBootTest+#AutoConfigureTestDatabase. Examples
#RunWith(SpringRunner.class)
#DataJpaTest // works
#ActiveProfiles("sep-test")
public class MyEntityRepositoryTest {
or
#RunWith(SpringRunner.class)
#SpringBootTest // these two
#AutoConfigureTestDatabase // together work
#ActiveProfiles("sep-test")
public class MyEntityRepositoryTest {
Recent Update
As of Spring Boot Version 2.7
the property spring.datasource.initialization-mode has been removed.
You should from this version and onwards use the replacement property spring.sql.init.mode
Example: spring.sql.init.mode:always
Spring Boot 2.7 changelog
It works fine for me, you can try it. Set datasource type to what you like instead of HikariCP.
spring.datasource.initialization-mode=always
spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
spring.jpa.hibernate.ddl-auto=none
There have another problem may result data.sql can not be executed,when you don't config the spring.jpa.hibernate.ddl-auto=none,that the data.sql will not be execute d.
I was able to make application run only after excluding Hikary CP like that:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<exclusions>
<exclusion>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</exclusion>
</exclusions>
</dependency>
Please, see the issue here