Reputation: 31
I want to develop a modular app that uses a MySql db, Java 11 and spring boot. The app works perfectly well until I add the module descriptors; so I can only guess I am missing something in the module-info.java, but I can't figure out what it is. I appreciate any help. Here is my code
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>11</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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/modules?serverTimezone=UTC
username: modulesAdmin
password: modules
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
properties:
hibernate.format_sql: true
module-info.java
requires java.persistence;
requires spring.context;
requires spring.beans;
requires spring.data.jpa;
requires spring.data.commons;
requires spring.web;
requires spring.boot;
requires spring.boot.autoconfigure;
opens com.jamsws.demojpa to spring.core;
exports com.jamsws.demojpa to spring.beans, spring.context;
exports com.jamsws.demojpa.resource to spring.beans;
User.java
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String name;
private String teamName;
private Integer salary;
public User() {
}
public User(String name, String teamName, Integer salary) {
this.name = name;
this.teamName = teamName;
this.salary = salary;
}
// getters and setters
}
UsersRepository.java
@Repository
public interface UsersRepository extends JpaRepository<User, Integer> {
}
UserResources.java
@RestController
@RequestMapping(value = "rest/users")
public class UserResources {
@Autowired
private UsersRepository usersRepository;
@PostMapping(value = "/load")
public List<User> persist(@RequestBody final User user) {
usersRepository.save(user);
return usersRepository.findAll();
}
}
DemojpaApplication.java
@SpringBootApplication
@EntityScan(basePackages = "com.jamsws.demojpa.model")
@EnableJpaRepositories(basePackages = "com.jamsws.demojpa.repository")
public class DemojpaApplication {
public static void main(String[] args) {
SpringApplication.run(DemojpaApplication.class, args);
}
}
Error trace
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to stop component [WebappLoader[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:na]
at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191) ~[na:na]
at [email protected]/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:982) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389) ~[tomcat-embed-core-9.0.36.jar:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at [email protected]/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.36.jar:na]
at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
at [email protected]/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.StandardService.stopInternal(StandardService.java:473) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.StandardServer.stopInternal(StandardServer.java:992) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.startup.Tomcat.stop(Tomcat.java:478) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stopTomcat(TomcatWebServer.java:273) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stop(TomcatWebServer.java:331) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:148) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.RELEASE.jar:na]
at demojpa/com.jamsws.demojpa.DemojpaApplication.main(DemojpaApplication.java:15) ~[classes/:na]
Caused by: org.apache.catalina.LifecycleException: Failed to stop component [WebappLoader[StandardEngine[Tomcat].StandardHost[localhost].TomcatEmbeddedContext[]]]
at [email protected]/org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:267) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5431) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389) ~[tomcat-embed-core-9.0.36.jar:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at [email protected]/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.36.jar:na]
at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
at [email protected]/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976) ~[tomcat-embed-core-9.0.36.jar:na]
... 23 common frames omitted
Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException
at java.base/java.lang.Class.getDeclaredMethods0(Native Method) ~[na:na]
at java.base/java.lang.Class.privateGetDeclaredMethods(Class.java:3166) ~[na:na]
at java.base/java.lang.Class.getMethodsRecursive(Class.java:3307) ~[na:na]
at java.base/java.lang.Class.getMethod0(Class.java:3293) ~[na:na]
at java.base/java.lang.Class.getMethod(Class.java:2106) ~[na:na]
at [email protected]/org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesJdbc(WebappClassLoaderBase.java:1697) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.loader.WebappClassLoaderBase.clearReferences(WebappClassLoaderBase.java:1619) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.loader.WebappClassLoaderBase.stop(WebappClassLoaderBase.java:1555) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.loader.WebappLoader.stopInternal(WebappLoader.java:449) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
... 31 common frames omitted
Caused by: java.lang.ClassNotFoundException: java.sql.SQLException
at [email protected]/org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader.loadClass(TomcatEmbeddedWebappClassLoader.java:72) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188) ~[tomcat-embed-core-9.0.36.jar:na]
... 41 common frames omitted
2020-07-19 19:51:17.437 ERROR 10199 --- [ main] org.apache.catalina.core.ContainerBase : A child container failed during stop
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: A child container failed during stop
at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122) ~[na:na]
at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:191) ~[na:na]
at [email protected]/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:982) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.StandardService.stopInternal(StandardService.java:473) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.StandardServer.stopInternal(StandardServer.java:992) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.startup.Tomcat.stop(Tomcat.java:478) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stopTomcat(TomcatWebServer.java:273) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.web.embedded.tomcat.TomcatWebServer.stop(TomcatWebServer.java:331) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:148) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:758) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.refresh(SpringApplication.java:750) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:315) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:1237) ~[spring-boot-2.3.1.RELEASE.jar:na]
at [email protected]/org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.1.RELEASE.jar:na]
at demojpa/com.jamsws.demojpa.DemojpaApplication.main(DemojpaApplication.java:15) ~[classes/:na]
Caused by: org.apache.catalina.LifecycleException: A child container failed during stop
at [email protected]/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:990) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:257) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1400) ~[tomcat-embed-core-9.0.36.jar:na]
at [email protected]/org.apache.catalina.core.ContainerBase$StopChild.call(ContainerBase.java:1389) ~[tomcat-embed-core-9.0.36.jar:na]
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[na:na]
at [email protected]/org.apache.tomcat.util.threads.InlineExecutorService.execute(InlineExecutorService.java:75) ~[tomcat-embed-core-9.0.36.jar:na]
at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:140) ~[na:na]
at [email protected]/org.apache.catalina.core.ContainerBase.stopInternal(ContainerBase.java:976) ~[tomcat-embed-core-9.0.36.jar:na]
... 16 common frames omitted
2020-07-19 19:51:17.441 INFO 10199 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-19 19:51:17.469 ERROR 10199 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field usersRepository in com.jamsws.demojpa.resource.UserResources required a bean named 'entityManagerFactory' 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 named 'entityManagerFactory' in your configuration.
EDIT: Following @Naman suggestion I added a requires java.sql;
to the module descriptor file which helped but the issue about "No bean named 'entityManagerFactory' available" is still there
2020-07-20 06:57:54.679 INFO 21643 --- [ main] com.jamsws.demojpa.DemojpaApplication : Starting DemojpaApplication on anderson-ubuntu with PID 21643 (/home/anderson/Documents/JavaApps/PracticeProjects/demojpa/target/classes started by anderson in /home/anderson/Documents/JavaApps/PracticeProjects/demojpa)
2020-07-20 06:57:54.681 INFO 21643 --- [ main] com.jamsws.demojpa.DemojpaApplication : No active profile set, falling back to default profiles: default
2020-07-20 06:57:54.973 INFO 21643 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-07-20 06:57:54.996 INFO 21643 --- [ main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 20ms. Found 1 JPA repository interfaces.
2020-07-20 06:57:55.272 INFO 21643 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2020-07-20 06:57:55.276 INFO 21643 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2020-07-20 06:57:55.276 INFO 21643 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.36]
2020-07-20 06:57:55.295 INFO 21643 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2020-07-20 06:57:55.295 INFO 21643 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 579 ms
2020-07-20 06:57:55.327 WARN 21643 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userResources': Unsatisfied dependency expressed through field 'usersRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'usersRepository' defined in com.jamsws.demojpa.repository.UsersRepository defined in @EnableJpaRepositories declared on DemojpaApplication: Cannot create inner bean '(inner bean)#5c080ef3' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#5c080ef3': Cannot resolve reference to bean 'entityManagerFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' available
2020-07-20 06:57:55.328 INFO 21643 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-07-20 06:57:55.338 INFO 21643 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-07-20 06:57:55.370 ERROR 21643 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter :
***************************
APPLICATION FAILED TO START
***************************
Description:
Field usersRepository in com.jamsws.demojpa.resource.UserResources required a bean named 'entityManagerFactory' 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 named 'entityManagerFactory' in your configuration.
Process finished with exit code 1
Upvotes: 3
Views: 952
Reputation: 434
I have gone through your stack-trace. Can you try below steps
Upvotes: 0
Reputation: 31958
The primary cause of the exception seems to be
Caused by: java.lang.NoClassDefFoundError: java/sql/SQLException
At the same time, the mention in your question that the "app works perfectly well until I add the module descriptors", seems to indicate that prior to introducing the module-info.java
, your application must have been using the classpath, where it was able to find the class mentioned above through transitive dependencies.
1Another aspect would be, that your application doesn't directly need this class, else you would have faced a compile-time error stating that the imports are unrecognized.
This boils down to the module dependencies that you've mentioned in your application's descriptor, so one or more of them requires java.sql.SQLException
now to be present on the modulepath and has itself missed specifying that explicitly (quite possible in the case of automatic modules). You would need to identify which module(s) amongst those required by you could need the specific class for a long term resolution.
1One way would be then to say, report it to the library/module owner and ask them to define an explicit dependency. This might require you to wait for them to be modular themself and then make use of the upgraded version. In such a case, you can until there upgrade add an argument to your application
--add-modules=java.sql
Another could be that your application itself at runtime needs the java.sql
module and that the libraries just make use of it when declared by you explicitly. In this case, the solution would be to add the following to your module-info.java
file:
requires java.sql
Upvotes: 1