I am new to working with Spring Boot, I have copied a project from 1.5.3 and have the application running with this code for the spring/application.java
@SpringBootApplication(scanBasePackages = "com.company")
@ComponentScan(basePackages = "com.company")
@EntityScan(basePackages = "com.company")
@EnableTransactionManagement
@EnableScheduling
public class Application {
private static final Logger log = LoggerFactory.getLogger(Application.class);
public static void main(String[] args) throws Exception {
// log.info("Starting up... main args: " Arrays.asList(args));
System.setProperty("spring.devtools.restart.enabled", "false");
// Runs Orchestration:run(args) via Spring
new SpringApplicationBuilder(Application.class)
// .web(WebApplicationType.NONE) <- this part is uncommented in 2.2.7 run
.run(args);
}
}
I have a @Component class too that runs after the SpringApplicatoinBuilder is called.
@Component
public class Orchestration implements CommandLineRunner {
@Override
public void run(String...args) throws Exception {
// stuff
}
}
When I upgrade all of my dependencies from 1.5.3.RELEASE to 2.2.7.RELEASE in the modules pom file, the application seems to not call the Orchestration @Component anymore. I have looked at other examples and brought in the migrator dependency as mentioned in this article https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide. But have not had luck running the Spring App correctly.
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jersey</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>${spring-boot.version}</version>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<version>${spring-boot.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring-boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
<version>${spring-boot.version}</version>
</dependency>
</dependencies>
The worst part is that I don't get any compilation or runtime errors so I'm not sure what is incorrect here. Can someone more experienced in Spring 2.2.7 help me with my projects setup?
EDIT 10/13/2021 6:18pm When updating to 2.5.5 release I do get an error message at the SpringApplicationBuilder class ...
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:49)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:108)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:58)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:88)
Caused by: java.lang.NoClassDefFoundError: org/springframework/core/metrics/ApplicationStartup
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:253)
at org.springframework.boot.SpringApplication.<init>(SpringApplication.java:266)
at org.springframework.boot.builder.SpringApplicationBuilder.createSpringApplication(SpringApplicationBuilder.java:108)
at org.springframework.boot.builder.SpringApplicationBuilder.<init>(SpringApplicationBuilder.java:97)
at com.planalytics.rp.orchestration.spring.Application.main(Application.java:35)
... 8 more
Caused by: java.lang.ClassNotFoundException: org.springframework.core.metrics.ApplicationStartup
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:151)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 13 more
CodePudding user response:
These dependencies are pulled by the Spring Boot core artifact that you declared. That is generally done via the spring-boot-starter-parent
that you declare as the parent pom of your project.
In order to update your project to Spring 5 (the actual released version), you have to update the spring-boot-starter-parent parent declaration from 1.5.3 to 2.X (or the spring-boot-dependencies
dependency version if you don't use the starter parent).
You can indeed read in the release note of Spring Boot 2 that :
Spring Boot 2.0 builds on and requires Spring Framework 5.
Note that updating from Spring Boot 1.5.3 to Spring Boot 2 may have as consequence some regressions for your application. So you should take care to test carefully your application to identify all of them. The Spring-Boot-2.0-Migration-Guide is also a good resource to ease the migration.
Here is a snippet of what you get by declaring org.springframework.boot:spring-boot-starter:jar:2.0.2.RELEASE as parent of your project
$ mvn dependency:tree
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------------------------------------
[INFO] Building demo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-dependency-plugin:3.0.2:tree (default-cli) @ demo ---
[INFO] com.example:demo:jar:0.0.1-SNAPSHOT
[INFO] - org.springframework.boot:spring-boot-starter:jar:2.5.5.RELEASE:compile
[INFO] | - org.springframework.boot:spring-boot:jar:2.5.5.RELEASE:compile
[INFO] | | \- org.springframework:spring-context:jar:5.0.6.RELEASE:compile
[INFO] | | - org.springframework:spring-aop:jar:5.0.6.RELEASE:compile
[INFO] | | - org.springframework:spring-beans:jar:5.0.6.RELEASE:compile
[INFO] | | \- org.springframework:spring-expression:jar:5.0.6.RELEASE:compile
[INFO] | - org.springframework.boot:spring-boot-autoconfigure:jar:2.5.5.RELEASE:compile
[INFO] | - org.springframework.boot:spring-boot-starter-logging:jar:2.5.5.RELEASE:compile`
...