Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass - java

currently i follow course video from this youtube:https://www.youtube.com/watch?v=BHoiNd64w0c&list=PLQag1tT77Ben3dupVMgYtoi_PVVQVRdD6&index=13
everything works fine, i run my project by clicking play button in eclipse, a little different by instructor because he used sts tool. But the error happen when trying to right click > run as > maven install
based on this question How to fix "Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass?, im already used the newer spring version. Here's my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.7</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>java-login-security</groupId>
<artifactId>login-system</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>login-system</name>
<description>login system</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.70</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
and here more error log:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.7.7)
2023-01-14 12:19:00.834 INFO 2373 --- [ main] j.l.LoginSystemApplicationTests : Starting LoginSystemApplicationTests using Java 17.0.4.1 on dna with PID 2373 (started by dna in /media/dna/data/koding/java/login-system)
2023-01-14 12:19:00.835 INFO 2373 --- [ main] j.l.LoginSystemApplicationTests : No active profile set, falling back to 1 default profile: "default"
2023-01-14 12:19:01.333 INFO 2373 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-01-14 12:19:01.380 INFO 2373 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 41 ms. Found 1 JPA repository interfaces.
2023-01-14 12:19:01.530 ERROR 2373 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.cglib.core.CodeGenerationException: java.lang.reflect.InaccessibleObjectException-->Unable to make protected final java.lang.Class java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int,java.security.ProtectionDomain) throws java.lang.ClassFormatError accessible: module java.base does not "opens java.lang" to unnamed module #78dd667e
at org.springframework.cglib.core.ReflectUtils.defineClass(ReflectUtils.java:598) ~[spring-core-5.3.24.jar:5.3.24]
at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:363) ~[spring-core-5.3.24.jar:5.3.24]
// ...more error log here...
my java version:
dna#dna:/$ java -version
openjdk version "17.0.5" 2022-10-18
OpenJDK Runtime Environment (build 17.0.5+8-Ubuntu-2ubuntu122.04)
OpenJDK 64-Bit Server VM (build 17.0.5+8-Ubuntu-2ubuntu122.04, mixed mode, sharing)
[update]
you can check my project here:
https://github.com/dhanyn10/java/tree/main/login-system

I had the same issue. It was because my project was written in Java 8 but I tried to start in Java 17. I just changed the configuration for Build&Run in IntelliJ from SDK 17 to the correct Java Version 8 and it worked. Maybe you used another Java version for building the project in IntelliJ.
Here's the configuration screenshot.

Related

Unable to create Custom Spring Boot Starter/AutoConfiguration

Custom starter project named as: hello-service-spring-boot-start
Project Directory Structure is:
hello-service-spring-boot-start directory structure
In pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javadevjournal</groupId>
<artifactId>hello-service-spring-boot-start</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hello-service-spring-boot-start</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-autoconfigure -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
In Starter Class or HelloServiceSpringBootStartApplication.java
package com.javadevjournal.helloservicespringbootstart;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class HelloServiceSpringBootStartApplication {
public static void main(String[] args) {
SpringApplication.run(HelloServiceSpringBootStartApplication.class, args);
System.out.println("hello-service-spring-boot-start-application started...");
}
}
In HelloService.java
package com.javadevjournal.helloservicespringbootstart.service;
public interface HelloService {
void sayHello();
}
In HelloServiceImpl.java
package com.javadevjournal.helloservicespringbootstart.service;
public class HelloServiceImpl implements HelloService {
#Override
public void sayHello() {
System.out.println("Hello from the default starter hello service");
}
}
In HelloServiceAutoConfiguration.java
package com.javadevjournal.helloservicespringbootstart.config;
import com.javadevjournal.helloservicespringbootstart.service.HelloService;
import com.javadevjournal.helloservicespringbootstart.service.HelloServiceImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
#ConditionalOnClass(HelloService.class)
public class HelloServiceAutoConfiguration {
#Bean
#ConditionalOnMissingBean
public HelloService helloService() {
return new HelloServiceImpl();
}
}
In spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.javadevjournal.helloservicespringbootstart.HelloServiceSpringBootStartApplication
Then, I run mvn install to create jar file or to build the above starter.
The project in which I have used above starter named as: custom-app
Project Directory Structure is:
custom-app project directory structure
In pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.javadevjournal</groupId>
<artifactId>custom-app</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>custom-app</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.javadevjournal</groupId>
<artifactId>hello-service-spring-boot-start</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
In Starter class or CustomAppApplication.java class
package com.javadevjournal.customapp;
import com.javadevjournal.helloservicespringbootstart.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class CustomAppApplication implements CommandLineRunner {
#Autowired
HelloService helloService;
public static void main(String[] args) {
SpringApplication.run(CustomAppApplication.class, args);
}
#Override
public void run(String... args) throws Exception {
helloService.sayHello();
}
}
Output: Console.
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v3.0.2)
2023-02-01T21:49:48.215+05:30 INFO 11332 --- [ restartedMain] c.j.customapp.CustomAppApplication : Starting CustomAppApplication using Java 18.0.2.1 with PID 11332 (G:\Java\IntelliJ Workspace\Learning\Spring Boot By Raghu\CustomStarterAutoConfigurationActual\custom-app\target\classes started by himan in G:\Java\IntelliJ Workspace\Learning\Spring Boot By Raghu\CustomStarterAutoConfigurationActual\hello-service-spring-boot-start)
2023-02-01T21:49:48.217+05:30 INFO 11332 --- [ restartedMain] c.j.customapp.CustomAppApplication : No active profile set, falling back to 1 default profile: "default"
2023-02-01T21:49:48.261+05:30 INFO 11332 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable
2023-02-01T21:49:48.623+05:30 WARN 11332 --- [ restartedMain] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customAppApplication': Unsatisfied dependency expressed through field 'helloService': No qualifying bean of type 'com.javadevjournal.helloservicespringbootstart.service.HelloService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {#org.springframework.beans.factory.annotation.Autowired(required=true)}
2023-02-01T21:49:48.629+05:30 INFO 11332 --- [ restartedMain] .s.b.a.l.ConditionEvaluationReportLogger :
Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2023-02-01T21:49:48.653+05:30 ERROR 11332 --- [ restartedMain] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field helloService in com.javadevjournal.customapp.CustomAppApplication required a bean of type 'com.javadevjournal.helloservicespringbootstart.service.HelloService' that could not be found.
The injection point has the following annotations:
- #org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.javadevjournal.helloservicespringbootstart.service.HelloService' in your configuration.
Process finished with exit code 0
Screenshot.
output: console
I am trying to create custom starter project in spring boot and use it in another. But I am getting Error which explains that the bean that has been created inside custom-starter is not initialized.
I am expecting to print hello message of starter in the console.
spring-boot-3 no longer uses spring.factories to register autoconfiguration classes.
Create a file named org.springframework.boot.autoconfigure.AutoConfiguration.imports in folder META-INF/spring where you can put (all) your Configuration classes.
In your case this would be
/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
com.javadevjournal.helloservicespringbootstart.config.HelloServiceAutoConfiguration
Note this should not be a SpringBootApplication
Reference: Creating Your Own Auto-configuration

Why write the log output code after SpringApplication.run() , it will not be executed, while other code can be executed?

I create a demo program use SpringInitializr。
jdk8
maven 3.6.3
windows11
import
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.10</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
main application
package com.example.demo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
#Slf4j
public class Application {
public static void main(String[] args) {
log.info("log output before SpringApplication.run");
SpringApplication.run(Application.class, args);
System.out.println("after SpringApplication.run first System.out.println");
log.info("log output after SpringApplication.run");
System.out.println("System.out.println end");
}
}
result
enter image description here
03:23:26.850 [main] INFO com.example.demo.Application - log output before SpringApplication.run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.10)
after SpringApplication.run first System.out.println
System.out.println end
Why the console does not output log.info(), but can output System.out.println()?
I tried to change the logback version, or use log4j,has the same result
Thank you very much for helping me
It happens because you set logging level to WARN or ERROR in your application.properties file (or other logger-configuration file).
For example, in application.properties
logging.level.root=WARN
By default it is INFO - that is why you see the printed log before Spring application starts.
Then Spring application starts and changes level to one of the below INFO: ERROR or WARN - according to configuration you have provided.

Config Client is not working in Spring boot

Config Client is not working in Spring boot
I was trying out config server client in Spring boot and came across this weird issue.
I have been able to successfully spin up the config server up and running but while trying to start the config client/consumer, it seems like the client isn't fetching any information from the config server.
Output while running client:
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.4.1)
2020-12-24 02:01:28.717 INFO 3000 --- [ main] c.r.service.profile.ProfileApplication : Starting ProfileApplication using Java 1.8.0_121 on DESKTOP-RID7KR with PID 3000 (E:\Spring\rent-a-car\profile\target\classes started by Dawg in E:\Spring\rent-a-car\profile)
2020-12-24 02:01:28.727 INFO 3000 --- [ main] c.r.service.profile.ProfileApplication : No active profile set, falling back to default profiles: default
2020-12-24 02:01:30.161 INFO 3000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-12-24 02:01:30.263 INFO 3000 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 82 ms. Found 1 JPA repository interfaces.
2020-12-24 02:01:30.642 INFO 3000 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=99ce795d-c5e8-3f6e-87d9-3efbe096b86a
2020-12-24 02:01:32.012 INFO 3000 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-12-24 02:01:32.027 INFO 3000 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-12-24 02:01:32.027 INFO 3000 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.41]
2020-12-24 02:01:32.287 INFO 3000 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-12-24 02:01:32.287 INFO 3000 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3430 ms
2020-12-24 02:01:32.431 WARN 3000 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-12-24 02:01:32.444 INFO 3000 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-12-24 02:01:32.478 INFO 3000 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-12-24 02:01:32.513 ERROR 3000 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
Process finished with exit code 1
bootstrap.yml (config-client)
spring:
application:
name: profile
profiles:
active: prod
cloud:
config:
name: profile
uri: http://localhost:8181
Have not included any application.yml file for config-client.
My config server is up on port 8181 and i am able to fetch the config from github successfully,
Sample success response:
{
"name":"profile",
"profiles":[
"prod"
],
"label":null,
"version":"24275f56bf516d847f171c9fc419ddef141bd39b",
"state":null,
"propertySources":[
{
"name":"https://github.com/vishu221b/rentaca-config-store.git/file:C:\\Users\\Vishal\\AppData\\Local\\Temp\\config-repo-2331899650646506\\service-config\\profile-service\\profile-prod.yml",
"source":{
"server.port":8081
}
},
{
"name":"https://github.com/vishu221b/rentaca-config-store.git/file:C:\\Users\\Vishal\\AppData\\Local\\Temp\\config-repo-2331899650646506\\application.yml",
"source":{
"spring.datasource.url":"jdbc:postgresql://127.0.0.1:5432/rent-a-car?createDatabaseIfNotExist=true",
"spring.datasource.username":"cofix",
"spring.datasource.password":"C0fiX",
"spring.datasource.driver-class-name":"org.postgresql.Driver",
"spring.jpa.hibernate.naming.physical-strategy":"org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl",
"spring.jpa.properties.hibernate.dialect":"org.hibernate.dialect.PostgreSQLDialect"
}
}
]
}
I know there must be some key/value errors in the files above, i'll manage that but only given that my config-client starts fetching config from my config-server running successfully.
There is no security in my config-server so i won't need username, password.
pom.xml (config-client)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.reantacar.service</groupId>
<artifactId>profile</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>profile</name>
<description>Profile service for Rent A Car application.</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.0</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- <!–The jar needs to be imported to make the bootstrap.yml configuration file take effect –>-->
<!-- <dependency>-->
<!-- <groupId>org.springframework.cloud</groupId>-->
<!-- <artifactId>spring-cloud-context</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.rentacar.commons</groupId>
<artifactId>commons</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
</repository>
</repositories>
</project>
PS: I have tried debugging this a lot over other stackoverflow questions and official docs on internet, but nothing seems to be fixing my issue. Initially i thought that my bootstrap.yml file must not be loading for some reasons, but now i am feeling that the config isn't working for my client at all because in the startup log it is mentioned that configuration from specific config-server-name/link:port was being tried to be fetched.
Also, please ignore the errors in my grammar :)
2020.0.0 Spring Cloud Config does not support bootstrap file.
For a workaround use bootstrap.{yml|properties} and add a dependency on
spring-cloud-starter-bootstrap to restore the old behavior.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
More information:
https://github.com/spring-cloud/spring-cloud-release/wiki/Spring-Cloud-2020.0-Release-Notes#breaking-changes
However, this was working before, but now and as mentioned above you have to your client project
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
spring-boot ver=2.4.3
spring-cloud ver=2020.0.1
Also, you can set the below property in your bootstrap. properties to make the client project fails to startup if fails to connect to the config server:
spring.cloud.config.fail-fast=true
To resolve this problem in Spring Cloud Config
If you are using a 2.4.0 or greater version of spring boot and
If you are using the 2020.0.0 or greater version of spring-cloud.
you need to add this dependency to the pom.xml (spring-cloud-config-client)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
Example of my pom.xml (spring-cloud-config-client)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>pay-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>pay-service</name>
<description>Pay project for Spring Boot</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Try like this, I hope it will work
Sometimes, it happen because you added the 'config client of spring cloud config' dependency, it required configuration in application.properties, add the configuration of your config server so you see the example below
src/main/resources/application.properties
server.port=8100
spring.config.import=optional:configserver:http://localhost:8888
Note: SNAPSHOT, M1, M2, M3, and M4 releases typically WORK IN PROGRESS. The Spring team is still working on them, Recommend NOT using them.

Reason: Failed to determine a suitable driver class : spring boot using eclipse

I am getting following error in console :
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2020-10-09 19:17:13.032 INFO 2692 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-10-09 19:17:13.065 INFO 2692 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-10-09 19:17:13.073 ERROR 2692 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
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
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
pom.xml :
?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<groupId>com.merchant</groupId>
<artifactId>merchantconfiguration</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>merchantconfiguration</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.properties :
spring.datasource.url=jdbc:postgresql://localhost:5432/hitachipyg_db
spring.datasource.username=postgres
spring.datasource.password=hitachi#123
spring.jpa.show.sql=true
spring.jpa.properties.hibernate.dialect =org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto= update spring.datasource.driver-class-name=org.postgresql.Driver
Could you please try the below property in you application.properties file.
spring.datasource.driver-class-name=org.postgresql.Driver

I am working on springboot example following and I am getting ApplicationContextException

I used different version of tomcat and iam getting error as Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
i get the above error on starup, Any quick help?
Error
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2020-03-16 23:04:01.268 INFO 3452 --- [ main] c.example.demo.CoursesWebappApplication : Starting CoursesWebappApplication on DESKTOP-10R8SDT with PID 3452 (D:\workspace-spring-tool-suite-4-4.5.1.RELEASE\CoursesWebapp\target\classes started by admin in D:\workspace-spring-tool-suite-4-4.5.1.RELEASE\CoursesWebapp)
2020-03-16 23:04:01.268 INFO 3452 --- [ main] c.example.demo.CoursesWebappApplication : No active profile set, falling back to default profiles: default
2020-03-16 23:04:01.581 WARN 3452 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
2020-03-16 23:04:01.596 ERROR 3452 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:156) ~[spring-boot-2.2.5.RELEASE.jar:2.2.5.RELEASE]
ControllerClass
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
#Controller
public class CoursesController {
#RequestMapping(value="/course", method = RequestMethod.GET)
public void courses () {
System.out.println("welcome");
}
}
DemoClass
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
#SpringBootConfiguration
public class CoursesWebappApplication {
public static void main(String[] args) {
SpringApplication.run(CoursesWebappApplication.class, args);
}
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>CoursesWebapp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>CoursesWebapp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
I suppose you are using Spring boot, so Use #SpringBootApplication instead of #SpringBootConfiguration to make sure Spring provide all requires autoconfiguration for your project.
Just change your Annotation on Main class to #SpringBootApplication from #SpringBootConfiguration so that spring will autoconfiure all stuffs needed to run the app.
#SpringBootApplication
public class CoursesWebappApplication {
public static void main(String[] args) {
SpringApplication.run(CoursesWebappApplication.class, args);
}
}

Categories