spring framework documentation usage - java

As in spring site isn't allowed to ask question i will write here my question to Spring authors.
I would like to know how supposed to use spring documentation.
I spent a lot of time in Spring boot reference guide. Only for understand how spring boot is working.
There are some examples work principle of which are not explained in documentation. Or explained like this
11.3.2 The #EnableAutoConfiguration annotation
The second class-level annotation is #EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you will want to configure Spring, based on the jar dependencies that you have added. Since spring-boot-starter-web added Tomcat and Spring MVC, the auto-configuration will assume that you are developing a web application and setup Spring accordingly.
And there are nothing about how it works. Some kind of philosophy.
I read a lot of tutorial from other developer and they give the exactly same information as original documentation. Like nobody knows how it really works and what will happen if you will not use tutorial example)).
Now I have to handle another programmers code on spring boot and I can't find answer to one question. What method will call after this application.main?
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;
import javax.inject.Inject;
/**
* Root entry of our service, starting the Spring container and configuring our system.
*/
#SpringBootApplication
#EnableScheduling
public class Application {
#Inject
private EnvironmentEndpoint environmentEndpoint;
protected Application() {
}
/**
* Main method, used to run the application.
*/
public static void main(final String[] args) {
final SpringApplication application = new SpringApplication(Application.class);
application.run(args);
}
}

Related

This application has no explicit mapping for /error, so you are seeing this as a fallback. In my newly created Spring boot application

I am a beginner to Spring boot. When I created a spring boot application from https://start.spring.io/ and when I am going to run the application It shows the below White Label error page. It has only the newly created Application.java file. It's great pleasure to expect an answer from you!
Application.java
package com.markdown.auth.application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
You have to add controllers and endpoints,which will return to you information that you want. Without these you'll get 404 error
You can add End Points means Controler and BL in which what you want.
https://spring.io/guides/gs/rest-service/
go through this link

I am confused on usecases for SpringApplicationBuilder

I am learning spring boot for work. Up to this point I have mostly worked with JavaScript and Node. I am working through a tutorial and came across this
package com.example.demosubmissionform;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
#ComponentScan
#SpringBootApplication
public class DemoSubmissionFormApplication extends SpringBootServletInitializer {
// #Override
// protected SpringApplicationBuilder configure(SpringApplicationBuilder
// application) {
// return application.sources(DemoSubmissionFormApplication.class);
// }
public static void main(String[] args) {
SpringApplication.run(DemoSubmissionFormApplication.class, args);
}
}
I have been playing around with the code trying to figure it how it works and learn the classes used. I have look through the docs but the information I found doesn't really explain what the SpringApplicationBuilder class is doing. I have commented it out and the program still works perfectly.
Sorry for being long winded. I appreciate any assistance.
P.S : The #ComponentScan annotation is redundant right? Seeing as its already comes with the #SpringBootApplication annotation.
The code using SpringApplicationBuilder is required when you are packaging your Spring Boot application as a war file and deploying it to an application server or Servlet container. If you're packaging your application is a jar file and running it using java -jar – the typical approach when using Spring Boot – you don't need the configure method and you don't need to extend SpringBootServletInitializer either.
You can learn a bit more about SpringBootServletInitializer, SpringApplicationBuilder and traditional war deployments in the reference documentation.
You are correct that the #ComponentScan annotation is redundant. As you said, it's already part of #SpringBootApplication.

Use Java Websocket API in Spring Boot application

i want to use the java websocket API in a Spring Boot application.
I created the following class as described here:
https://www.baeldung.com/java-websockets
import javax.websocket.*;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
#ServerEndpoint(value = "/test")
public class FrontendEndpoint {
#OnOpen
public void onOpen(Session session) throws IOException {
session.getBasicRemote().sendText("Test");
}
#OnMessage
public void onMessage(Session session, String message) throws IOException {
}
#OnClose
public void onClose(Session session) throws IOException {
}
#OnError
public void onError(Session session, Throwable throwable) {
}
}
I try to connect to that websocket but nothing happens. I saw a lot of articles in the internet but nothing helped me. I dont know how to get it to work.
When i try to connect to the websocket nothing happend.
Spring Boot version: 2.0.3
Websocket-API version: 1.1
I also don't see an open port for the websocket.
Thanks
BR
I once created a sample application with Spring Boot and Websocket API
https://github.com/simasch/spring-boot-websocket
but the problem is that the ServerEndpoint is not managed by Spring but by the Websocket implementation. So this is maybe not the way you should use websockets with Spring Boot.
I would recommend to have a look how Spring think that Websockets should be used:
https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#websocket
It's a bit late, but in case someone will come here for a solution.
It is not covered in Spring documentation how to use Java WebSocket API (JSR-356) in Spring and I didn't manage to find any tutorial describing how to achieve it despite I've spent several hours googling it.
In order to use Java WebSocket API with Spring you should use class org.springframework.web.socket.server.standard.SpringConfigurator:
https://docs.spring.io/spring/docs/current/javadoc-api/index.html?org/springframework/web/socket/server/standard/SpringConfigurator.html
from
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-websocket</artifactId>
<version>...</version>
</dependency>
Just add it to your
#ServerEndpoint(... configurator = SpringConfigurator.class)
and it is done. Your annotated class becomes a usual Spring-managed component legal for #Autowired injection and so on.
Hope it helps someone :)
UPD.
It should be mentioned, that JSR 356 WebSocket initialization through the SpringConfigurator is only supported in .war projects, that are to be deployed as usual artifacts in servlet container.
It will not work as expected when project is packaged in executable Spring Boot .jar or Spring Boot executable .war and when it is deployed it standalone mode via java -jar {app}
I didn't manage to find a way to initialize JSR 356 WebSocket with #Autowired Spring components in it in Spring Boot environment in executable archive.
It is possible to deploy JSR 356 WebSocket via ServerEndpointExporter.setAnnotatedEndpointClasses(...), but every class provided must have a no-arg constructor otherwise exception will be thrown. #Autowired field will not be processed by Spring in this case.
If you want to use java websocket API in a Spring Boot application you have to add this to spring configuration
#Configuration
public class WebSocketConfig {
#Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
Here is a full article.

How do I start a Spring Boot Web Application without using ComponentScan

I am trying to avoid component scanning to reduce start up time in our module tests, and in our web app in general.
When I replace #SpringBootApplication with #SpringBootConfiguration #EnableAutoConfiguration, I get the following error:
Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean
Can I manually import the EmbeddedServletContainerFactory somehow?
My suggestion is to first run your application with the debug flag on and write down all the activated auto-configurations. Then, disable auto-configuration and import those configurations by using #Import on your application class.
Alternatively, you can look at each of those configuration classes and see what Spring Boot configures for you and decide if you want to provide your own configurations instead - you can just mimic the auto-configuration classes and everything should work the same way.
Miloš and Pieter provided the means to find the answer. A minimal Spring Boot Web Application can be started with the following:
#SpringBootConfiguration
#Import({EmbeddedServletContainerAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {
...
}
ServerPropertiesAutoConfiguration.class might also be handy to pick up things like port number for the application.

Ultimate Aim of Spring-Boot [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
as you may know Spring4 comes with new features, and one of the most important feature among them is Spring-boot.
I am following the links below
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-documentation
https://github.com/spring-projects/spring-boot
Spring-boot feature comes with new class files in org.springframework.boot.* to start the Spring application. There are two question comes in mind that
1- for JavaSE, I can start-up the spring application with previoues spring versions easly, is the new feature spring-boot is just for simple boot
2- for JavaEE, as far as I know Spring-boot is not just for javaSE project, it can start-up web projects as well. So in the future spring-boot works as Application-server (like Glassfish)
Although Spring Boot only works with Spring 4+, it is technically a different project. What this means is that you can use Spring 4 without any Spring Boot code.
The aim of Spring Boot is to provide an easy way to configure a Spring application by providing sensible defaults and easy configuration options for stuff that is commonly used (and you otherwise have to implemented) over and over again in our applications.
As far as starting up a Java SE application, Spring Boot will easily start the application just like any other Java SE, with the main method, and looks something like this:
#Configuration
#EnableAutoConfiguration
//whatever other annotations
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
//do whatever
}
}
In order to use a web environment, Spring Boot uses an embedded servlet container (Tomcat by default, but Jetty is also available). That means that code like:
#ComponentScan
#EnableAutoConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
#RestController
public class HelloController {
#RequestMapping("/")
public String index() {
return "Greetings from Spring Boot!";
}
}
is enough to get everything started (providing that all the required dependencies are on the classpath).
Seeing working Spring code that is so light, is a breath of fresh air! You no longer need to loads of XML or Java config files, the defaults work great!
Also you can start and stop the whole application from the main method inside your IDE! Sweet!

Categories

Resources