Webnovel Test1108 - LightNovelsOnl.com
You're reading novel online at LightNovelsOnl.com. Please use the follow button to get notifications about your favorite novels and its latest chapters so you can come back anytime and won't miss anything.
Iran's Red Crescent said there is no chance of finding survivors.
The aircraft belonging to Ukraine International Airlines crashed just after take-off from Iran's Imam Khomeini airport in Tehran, said the Fars state news agency.
Preliminary reports suggest that the plane was en route to the Ukrainian capital of Kyiv.
It is unclear whether the incident is linked to the Iran-US confrontation.
Rescue teams have been sent to the area, near the airport, where the aircraft crashed.
The head of Iran's Red Crescent told state media that it was "impossible" for anyone to have survived the crash.
Ukrainian President Volodymyr Zelensky said efforts were being made to establish the circ.u.mstances of the crash and the death toll.
"My sincere condolences to the relatives and friends of all pa.s.sengers and crew," he said in a statement.
Mr Zelensky was cutting short a visit to Oman and returning to Kyiv, the statement said.
1. Overview
In this write-up, we're going to look at the differences between the standard Spring frameworks and Spring Boot.
We'll focus on and discuss how the modules of Spring, like MVC and Security, differ when used in core Spring versus when used with Boot.
Further reading:
Configure a Spring Boot Web Application
Some of the more useful configs for a Spring Boot application.
Read more →
Migrating from Spring to Spring Boot
See how to properly migrate from a Spring to Spring Boot.
Read more →
2. What Is Spring?
Simply put, the Spring framework provides comprehensive infrastructure support for developing Java applications.
It's packed with some nice features like Dependency Injection and out of the box modules like:
Spring JDBC
Spring MVC
Spring Security
Spring AOP
Spring ORM
Spring Test
These modules can drastically reduce the development time of an application.
For example, in the early days of Java web development, we needed to write a lot of boilerplate code to insert a record into a data source. But by using the JDBCTemplate of the Spring JDBC module we can reduce it to a few lines of code with only a few configurations.
3. What Is Spring Boot?
Spring Boot is basically an extension of the Spring framework which eliminated the boilerplate configurations required for setting up a Spring application.
It takes an opinionated view of the Spring platform which paved the way for a faster and more efficient development eco-system.
Here are just a few of the features in Spring Boot:
Opinionated 'starter' dependencies to simplify build and application configuration
2
3
4
5
org.springframework.boot
spring-boot-starter-web
2.0.5.RELEASE
All other dependencies are added automatically to the final archive during build time.
Another good example is testing libraries. We usually use the set of Spring Test, JUnit, Hamcrest, and Mockito libraries. In a Spring project, we should add all these libraries as dependencies.
But in Spring Boot, we only need the starter dependency for testing to automatically include these libraries.
Spring Boot provides a number of starter dependencies for different Spring modules. Some of the most commonly used ones are:
spring-boot-starter-datjpa
spring-boot-starter-security
spring-boot-starter-test
spring-boot-starter-web
spring-boot-starter-thymeleaf
For the full list of starters, also check out the Spring doc.u.mentation.
5. MVC Configuration
Let's explore the configuration required to create a JSP web application using both Spring and Spring Boot.
Spring requires defining the dispatcher servlet, mappings, and other supporting configurations. We can do this using either the web.xml file or an Initializer cla.s.s:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public cla.s.s MyWebAppInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) {
AnnotationConfigWebApplicationContext context
= new AnnotationConfigWebApplicationContext();
context.setConfigLocation("com.baeldung");
container.addListener(new ContextLoaderListener(context));
ServletRegistration.Dynamic dispatcher = container
.addServlet("dispatcher", new DispatcherServlet(context));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
We also need to add the @EnableWebMvc annotation to a @Configuration cla.s.s and define a view-resolver to resolve the views returned from the controllers:
1
2
3
4
5
6
7
8
9
10
11
12
13
@EnableWebMvc
@Configuration
public cla.s.s ClientWebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver bean
= new InternalResourceViewResolver();
bean.setViewCla.s.s(JstlView.cla.s.s);
bean.setPrefix("/WEB-INF/view/");
bean.setSuffix(".jsp");
return bean;
}
}
By comparison to all this, Spring Boot only needs a couple of properties to make things work, once we've added the web starter:
1
2
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
All the Spring configuration above is automatically included by adding the Boot web starter, through a process called auto-configuration.
What this means is that Spring Boot will look at the dependencies, properties, and beans that exist in the application and enable configuration based on these.
Of course, if we want to add our own custom configuration, then the Spring Boot auto-configuration will back away.
5.1. Configuring Template Engine
Let's now learn how to configure a Thymeleaf template engine in both Spring and Spring Boot.
In Spring we need to add the thymeleaf-spring5 dependency and some configurations for the view resolver:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@Configuration
@EnableWebMvc
public cla.s.s MvcWebConfig implements WebMvcConfigurer {
@Autowired
private ApplicationContext applicationContext;
@Bean
public SpringResourceTemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver =
new SpringResourceTemplateResolver();
templateResolver.setApplicationContext(applicationContext);
templateResolver.setPrefix("/WEB-INF/views/");
templateResolver.setSuffix(".html");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
templateEngine.setEnableSpringELCompiler(true);
return templateEngine;
}
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
registry.viewResolver(resolver);
}
}
Spring Boot 1 required only the dependency of spring-boot-starter-thymeleaf to enable Thymeleaf support in a web application. But because of the new features in Thymeleaf3.0, we have to add thymeleaf-layout-dialect also as a dependency in a Spring Boot 2 web application.
Once the dependencies are in place, we can add the templates to the src/main/resources/templates folder and the Spring Boot will display them automatically.
6. Spring Security Configuration
For the sake of simplicity, we'll see how the default HTTP Basic authentication is enabled using these frameworks.
Let's start by looking at the dependencies and configuration we need to enable Security using Spring.
Spring requires both the standard the spring-security-web and spring-security-config dependencies to set up Security in an application.
Next, we need to add a cla.s.s that extends the WebSecurityConfigurerAdapter and makes use of the @EnableWebSecurity annotation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Configuration
@EnableWebSecurity
public cla.s.s CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication()
.withUser("user1")
.pa.s.sword(pa.s.swordEncoder()
.encode("user1Pa.s.s"))
.authorities("ROLE_USER");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.httpBasic();
}
@Bean
public Pa.s.swordEncoder pa.s.swordEncoder() {
return new BCryptPa.s.swordEncoder();
}
}
Here we're using the inMemoryAuthentication to set up the authentication.
Similarly, Spring Boot also requires these dependencies to make it work. But we need to define only the dependency of spring-boot-starter-security as this will automatically add all the relevant dependencies to the cla.s.spath.
The security configuration in Spring Boot is the same as the one above.
If you need to know how the JPA configuration can be achieved in both Spring and Spring Boot, then check out our article A Guide to JPA with Spring.
7. Application Bootstrap
The basic difference in bootstrapping of an application in Spring and Spring Boot lies with the servlet. Spring uses either the web.xml or SpringServletContainerInitializer as its bootstrap entry point.
On the other hand, Spring Boot uses only Servlet 3 features to bootstrap an application. Let' talk about this in detail.
7.1. How Spring Bootstraps?
Spring supports both the legacy web.xml way of bootstrapping as well as the latest Servlet 3+ method.
Let's see the web.xml approach in steps:
Servlet container (the server) reads web.xml
The DispatcherServlet defined in the web.xml is instantiated by the container
DispatcherServlet creates WebApplicationContext by reading WEB-INF/{servletName}-servlet.xml
Finally, the DispatcherServlet registers the beans defined in the application context
Here's how Spring bootstraps using Servlet 3+ approach:
The container searches for cla.s.ses implementing ServletContainerInitializer and executes
The SpringServletContainerInitializer finds all cla.s.ses implementing WebApplicationInitializer
The WebApplicationInitializer creates the context with XML or @Configuration cla.s.ses
The WebApplicationInitializer creates the DispatcherServlet with the previously created context.
7.2. How Spring Boot Bootstraps?
The entry point of a Spring Boot application is the cla.s.s which is annotated with @SpringBootApplication:
1
2
3
4
5
6
@SpringBootApplication
public cla.s.s Application {
public static void main(String[] args) {
SpringApplication.run(Application.cla.s.s, args);
}
}
By default, Spring Boot uses an embedded container to run the application. In this case, Spring Boot uses the public static void main entry-point to launch an embedded web server.
Also, it takes care of the binding of the Servlet, Filter, and ServletContextInitializer beans from the application context to the embedded servlet container.
Another feature of Spring Boot is that it automatically scans all the cla.s.ses in the same package or sub packages of Main-cla.s.s for components.
Spring Boot provides the option of deploying it as a web archive in an external container as well. In this case, we have to extend the SpringBootServletInitializer:
1
2
3
4
@SpringBootApplication
public cla.s.s Application extends SpringBootServletInitializer {
// ...
}
Here the external servlet container looks for the Main-cla.s.s defined in the METINF file of the web archive and the SpringBootServletInitializer will take care of binding the Servlet, Filter, and ServletContextInitializer.
8. Packaging and Deployment
Finally, let's see how an application can be packaged and deployed. Both of these frameworks support the common package managing technologies like Maven and Gradle. But when it comes to deployment, these frameworks differ a lot.
For instance, the Spring Boot Maven Plugin provides Spring Boot support in Maven. It also allows packaging executable jar or war archives and running an application "in-place".
Some of the advantages of Spring Boot over Spring in the context of deployment include:
Provides embedded container support
Provision to run the jars independently using the command java -jar
Option to exclude dependencies to avoid potential jar conflicts when deploying in an external container
Option to specify active profiles when deploying
Random port generation for integration tests
9. Conclusion
In this tutorial, we've learned about the differences between Spring and Spring Boot.
In a few words, we can say that the Spring Boot is simply an extension of Spring itself to make the development, testing, and deployment more convenient.
I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:
>> CHECK OUT THE COURSE