I'm using Eclipse STS 3.8.1
Is there anyway to remove apps from the Boot Dashboard?
Reason for asking (Similar to my real world application/s):
I have multiple Spring Boot applications, some of which use a shared library. In this library I have some common configuration: Eg: Setup RestTemplate/s with Ribbon (#LoadBalancer)...
On order to have access to the #Configuration and #Bean annotations I include the below dependancy into my libraries pom:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.4.0.RELEASE</version>
</dependency>
The problem is now my library is displayed in the Boot Dashboard with my applications even though it is not a Boot Application.
This is rather annoying as it clutters the dashboard, how can I remove this?
Just to expand on my above example:
I'm using a multi-module maven project with multiple applications and libs under the same parent.
Go to the Preferences -> Spring -> Boot and configure the projects to be excluded from the boot dashboard by default. Just put the name of the project in that text field.
Related
When I try to run a Spring Boot JavaFX project with
mvn spring-boot:run
I get an error "JavaFX runtime components are missing". The JDK version is 17.
I did an online search on the error message. Two solutions I find. One is to make the application modular. I don't see that is a valid solution for the Spring Boot application. And the other one is to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules".
I also try to run the project with JDK only
java -jar app.jar
Although there aren't any errors, the GUI doesn't show up.
How to resolve this issue? A good solution would be that it is easy to run without many downloading, local setup etc.
Since Java 11, JavaFX was removed from the SDK. It is now in its own separate module, and if you want to use it in your application, you will need to specifically include it.
Regarding, "to make the application modular":
Spring 5 and Spring Boot are not friendly with Java Platform Module System (JPMS). It won't really be built for modules until Spring 6/Springboot 3 is released.
I do try the modular approach with Spring Boot 2. However, compilation
failed due to a known Lombok
error.
Regarding, "to add command line arguments for module path and modules names. The Maven command doesn't take arguments "--module-path" nor "--add-modules":
You can use non-modular approach without --module-path and --add-modules.
Maven configuration is pretty straightforward.
Firstly, Add the JavaFX modules you need as maven dependencies. For instance:
<properties>
<java.version>17</java.version>
<javafx.version>17.0.2</javafx.version>
</properties>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-controls</artifactId>
<version>${javafx.version}</version>
</dependency>
<dependency>
<groupId>org.openjfx</groupId>
<artifactId>javafx-fxml</artifactId>
<version>${javafx.version}</version>
</dependency>
The project can be built and run using Spring Boot Maven Plugin
NOTE: There is a maven plugin available for running JavaFX 11+ applications. So far, it is not required since the application will be packed and run using Spring Boot.
You may find a working example here, showcasing how to bootstrap JavaFX applications within Spring Boot application.
How to bootstrap JavaFX application within Spring Boot application?
The bootstrap process is heavily inspired by Mr. Awesome Josh Long’s
Spring Tips:
JavaFX
installment.
Instead of calling SpringBootApplication.run() use a custom bootstrap class inheriting from JavaFX Application. This is needed to initialize JavaFX correctly.
JavaFxApplication class does the heavy lifting for creating a proper JavaFX application with initialized Spring Context. It's responsible for:
Set Spring Boot web server type to NONE.
Programmatically create a Spring Boot context in the Application#init() method.
Kick off application logic by sending a StageReadyEvent containing the primary Stage as payload.
Support graceful shutdown for both Spring context and JavaFX platform.
Im literally searching for the same...
But I found there are some mods to do in your run-config to get this problem solved
How Spring Boot auto-configuration classes are activated based on the maven starter projects that are included in project pom.xml as dependencies?
for example, by adding spring-boot-starter-security to pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
class SecurityAutoConfiguration is activated. how does this happen? How SecurityAutoConfiguration is related to spring-boot-starter-security?
the starter project themselves are completely empty, there is no java code in these starter projects, and just a couple of dependencies is defined in pom.xml. I want to know exactly what happens when I add a starter project to my Spring Boot project and exactly what configurations are applied.
Autoconfiguration is one, or maybe the main one, feature of SpringBoot
Basically, depending on certain conditions, some configurations or/and beans are made available.
In your case here is what happens :
spring-boot-starter-security, as other starters, is just here to make some others dependencies available for your project, particulary spring-security-config which itself, has a dependency on spring-security-core
If your looking at the code source of SecurityAutoConfiguration you can see that is configuration class depends on the availabality of the class DefaultAuthenticationEventPublisher, which is true as now you have a (transitive) dependency on spring-security-core.
This SecurityAutoConfiguration class is listed as a Autoconfiguration class inside the config file org.springframework.boot.autoconfigure.AutoConfiguration.imports (spring.factories for some previous Spring Boot version) of the spring-boot-autoconfigure project, which make it active for your application.
Here you can find the documentation :
https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.developing-auto-configuration.understanding-auto-configured-beans
You can find it here - https://www.baeldung.com/spring-boot-security-autoconfiguration
Basically, either property for the starter can be disabled or exclude specific auto configuration from #springboot(exclude = SecurityAutoConfiguration.class
)
We have a spring boot based application / collection of applications. Already in the first sentence it is complicated to describe it correctly, because from the business view 'the thing' is one application, but from technical view it as a collection of spring boot applications and batch jobs handling use cases of one business software.
For example the software consists of a spring boot application providing a rest-api, multiple spring boot applications providing different web services and multiple batch jobs doing different things based on spring boot applications and other applications.
Now some parts of the code are used only from one of the spring boot applications - so this can be located easily into this particular spring boot application.
But many parts of the code is used from multiple spring boot applications, but usually less then the half of the applications. For this business code we have a central module imported by all spring boot applications. Adding this central module to a spring boot application increases the big number of dependencies (e.g dependency to a CRM system, dependency to an S3 storage, dependency to generate Excel files, dependency to a external webservice, dependency to rabbit, ...). To avoid the hundreds of dependencies we have defined many as optional. So every spring boot application has only the dependencies it needs.
The problem now is in the central module with the implemented services with business code. These services are injected by autowire. For example a spring boot application providing a webservice interface doesn't need the service to generate Excel files and for that don't have a dependency to the excel libraries and doesn't need to start the excel generation service.
Spring has the possibility to annotate the services with #ConditionalOnClass and #ConditionalOnBean but many of the services doesn't depend only on one condition but on two or more.
So what is the best approach to structure the services to start them only if needed and/or the needed dependencies are available?
At the moment we use #ComponentScan with excludeFilters at every spring boot application but that is error prone. Splitting the central module into multiple modules or microservices is no option because then the modules contains only 1 to 5 classes.
Any good practice to structure applications like this?
You have understood the problem well and broken down into modules enough. In your case to solve this problem of using #ComponentScan with excludeFilters, you must try to do what Spring Boot does with its Auto Configuration mechanism.
For example with the data source configured in properties or yaml file and Spring Boot JPA Starter is in the classpath, Spring Boot Auto Configures everything for us. You need write some custom Starters with logic to Auto Configure your services based on the multiple conditions you have. And in your microservices, batch jobs you can include this custom Starters to enable and inject the necessary Services and those alone.
Sometime back I wrote two blog articles on how to write custom Spring Boot Starters. These could be a good start for you. Post 1 and Post 2.
If your module is declaring the common module as a dependency, you can set the scope of that module to 'compile' and then set the dependencies that should be excluded from the common module.
I recently encountered a situation where I required JPA in my common module in order to create generic DAOs for other modules. But I thought it would be wasteful to add a dependency that large my other module that was not utilizing JPA.
This was the solution:
<!-- ... -->
<groupId>com.multimodule</groupId>
<artifactId>server-app</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>${spring.version}</version>
<relativePath />
</parent>
<dependencies>
<dependency>
<groupId>com.multimodule</groupId>
<artifactId>common</artifactId>
<version>1.0.0-SNAPSHOT</version>
<scope>compile</scope>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</exclusion>
<exclusion>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- ... -->
</dependencies>
<!-- ... -->
By doing this, in addition to excluding the JPA-related packages in the common module using #ComponentScan, the common dependency is included when the server-app module compiles, but does not include the JPA jars with it.
If #ComponentScan is not added, the module would still compile, but there would probably be exceptions as soon as a bean is wired from common that attempts to wire a bean from any of the excluded jars.
This is immediately true in my case, since my common module would have attempted to autoconfigure Jpa during startup, if it had not been excluded, because of the the #EnableAutoConfiguration in #SpringBootApplication.
fair warning: I am new to Spring and maven.
The project:
The client has provided us with a web project that uses jsp to render front ends and uses spring-context only to manage some of their classes as Spring Beans. This project uses xml configuration. Here is the maven section that includes the spring-context:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
Our job is to create an api layer for this project to open up access to the project's resources. The plan is to create a modern Spring Boot api using the spring-boot-web starter.
The problem:
The jsp project is split up into sub-modules. We are trying to take their business logic submodule as a maven dependency in our api project (it defines it's own spring context). The first problem that we have faced occurs when we simply add the module from the jsp project as a dependency in our api project. The spring boot initialization process provides no log feedback and hangs but does not exit (It runs fine when the dependency is not included). When crawling through with a debugger I have found that it is loading configuration files that are in the business module. My thinking is that one of the api's configuration files are getting overwritten which is breaking spring's logging and other behaviors.
The other more general problem is that I don't know if I can use the beans provided in the business logic module in my modern version of spring.
So the question is:
Is there a proper way to use beans defined in a dependent spring project?
My goal is understanding the J2EE lifecycle at a high-level with Spring, Hibernate, and Maven. From much research, I understand that Spring provides dependency injection and Hibernate provides object-relation mapping with databases. Maven is a tool to improve the build/deployment process from my understanding. With that said, everywhere I search I get more and more lost on configuration files (i.e. pom.xml, server.xml, etc.), terminology, and alternatives such as Gradle. I just want to build and launch the application and be able to see via http://localhost:8080 in tomcat.
At first, I couldn't get the default project (picture attached) built, but after further research found that I needed to Maven clean and Maven install.
I also modified settings in pom.xml changing version numbers and the database to use MySQL.
<properties>
<maven.test.failure.ignore>true</maven.test.failure.ignore>
<spring.framework.version>3.1.1.RELEASE</spring.framework.version>
</properties>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.1.1.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
The next issue I had was in OrderPersistenceTests.java it used org.hibernate.classic.Session which is no longer the correct class path and found that it needed to be org.hibernate.Session.
Finally, I was able to get it to build but cannot figure out how to deploy to Tomcat from Spring Tool Suite.
I have put together a simple example using Maven, Spring, Hibernate, and ExtJS for the front end at the following link:
https://github.com/drembert/java-webapp-sample
If you are using Spring Source Tool Suite which it looks like you are in the screen shot, you should be able to import everything using the "Import Maven Projects" option. The example uses Hypersonic as the in-memory DB to allow easier deployment. Keep in mind this example generates two different .war files (one is presentation-layer and the other is service-layer) to emulate a simple RESTful service so both will need to be deployed to the Spring tcServer (STS's version of Tomcat), but once they are you should be able to view the GUI at http://localhost:8080/presentation-layer. Another thing to make note of is that this example currently doesn't have a security layer which would normally be implemented using Spring security, but I am working on adding that in the near future.