加入收藏 | 设为首页 | 会员中心 | 我要投稿 威海站长网 (https://www.0631zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 移动互联 > 评测 > 正文

Spring和SpringBoot比较,解惑区别

发布时间:2019-04-05 19:37:30 所属栏目:评测 来源:SanLi
导读:1、概述: 对于 Spring 和 SpringBoot 到底有什么区别,我听到了很多答案,刚开始迈入学习 SpringBoot 的我当时也是一头雾水,随着经验的积累、我慢慢理解了这两个框架到底有什么区别,我相信对于用了 SpringBoot 很久的开发人员来说,有绝大部分还不是很

SpringBoot1X只需要spring-boot-starter-thymeleaf的依赖 项 来启用Web应用程序中的       Thymeleaf支持。但是由于Thymeleaf3.0中的新功能, 我们必须将thymeleaf-layout-dialect 添加 为SpringBoot2XWeb应用程序中的依赖项。一旦依赖关系到位,我们就可以将模板添加到src/main/resources/templates文件夹中,SpringBoot将自动显示它们。

4.4、Spring Security 配置

为简单起见,我们使用框架默认的HTTP Basic身份验证。让我们首先看一下使用Spring启用Security所需的依赖关系和配置。
       Spring首先需要依赖 spring-security-webspring-security-config 模块。接下来, 我们需要添加一个扩展WebSecurityConfigurerAdapter的类,并使用@EnableWebSecurity注解:

  1. @Configuration 
  2. @EnableWebSecurity 
  3. public class CustomWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 
  4.    
  5.     @Autowired 
  6.     public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
  7.         auth.inMemoryAuthentication() 
  8.           .withUser("admin") 
  9.             .password(passwordEncoder() 
  10.             .encode("password")) 
  11.           .authorities("ROLE_ADMIN"); 
  12.     } 
  13.    
  14.     @Override 
  15.     protected void configure(HttpSecurity http) throws Exception { 
  16.         http.authorizeRequests() 
  17.           .anyRequest().authenticated() 
  18.           .and() 
  19.           .httpBasic(); 
  20.     } 
  21.       
  22.     @Bean 
  23.     public PasswordEncoder passwordEncoder() { 
  24.         return new BCryptPasswordEncoder(); 
  25.     } 

这里我们使用inMemoryAuthentication来设置身份验证。同样,Spring Boot也需要这些依赖项才能使其工作。但是我们只需要定义spring-boot-starter-security的依赖关系,因为这会自动将所有相关的依赖项添加到类路径中。

Spring Boot中的安全配置与上面的相同。
5、应用程序引导配置

SpringSpring Boot中应用程序引导的基本区别在于servlet
Spring使用web.xmlSpringServletContainerInitializer作为其引导入口点。
Spring Boot仅使用Servlet 3功能来引导应用程序,,下面让我们详细来了解下

5.1、Spring 是怎样引导配置的呢?

Spring支持传统的web.xml引导方式以及最新的Servlet 3+方法。

让我们看一下 web.xml方法的步骤:

Servlet容器(服务器)读取web.xml
web.xml中定义的DispatcherServlet由容器实例化
DispatcherServlet通过读取WEB-INF / {servletName} -servlet.xml来创建WebApplicationContext
最后,DispatcherServlet注册在应用程序上下文中定义的bean

以下是使用Servlet 3+方法的Spring引导:

容器搜索实现ServletContainerInitializer的类并执行
SpringServletContainerInitializer找到实现所有类WebApplicationInitializer
WebApplicationInitializer创建具有XML或上下文@Configuration
WebApplicationInitializer创建DispatcherServlet的 与先前创建的上下文。

5.2、SpringBoot 有是如何配置的呢?

Spring Boot应用程序的入口点是使用@SpringBootApplication注释的类:
  1. @SpringBootApplication 
  2. public class Application { 
  3.     public static void main(String[] args) { 
  4.         SpringApplication.run(Application.class, args); 
  5.     } 

(编辑:威海站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读