An util library with WhereIsFrom, provides methods for getting information about a method invocation as StackTraceElement.
implementation 'dev.alexengrig:whereisfrom:1.1'
<dependency>
<groupId>dev.alexengrig</groupId>
<artifactId>whereisfrom</artifactId>
<version>1.1</version>
</dependency>
Example of a simple Spring Boot application:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Usually we have to pass a Class to SpringApplication#run(Class<?> primarySource, String... args) in which this method is called.
We can create an util method that can define
a Class - using WhereIsFrom#here()
:
public class AwareSpringApplication {
public static ConfigurableApplicationContext run(String... args) {
try {
String className = WhereIsFrom.here().getClassName();
Class<?> targetClass = Class.forName(className);
return SpringApplication.run(targetClass, args);
} catch (ClassNotFoundException e) {
throw new IllegalStateException(e);
}
}
}
And use this util method:
@SpringBootApplication
public class AwareApplication {
public static void main(String[] args) {
AwareSpringApplication.run(args);
}
}
Full code in demo project.
This project is licensed under Apache License, version 2.0.