春天(Spring & Spring Boot)

基本概念

春天

Spring 是用于开发 Java 应用程序的开源框架,为解决企业应用开发的复杂性而创建。

  1. Spring 的基本设计思想是利用 IOC(依赖注入)和 AOP (面向切面)解耦应用组件,降低应用程序各组件之间的耦合度。
  2. 在这两者的基础上,Spring 逐渐衍生出了其他的高级功能:如 Security,JPA 等。

Spring MVC

Spring MVC 是 Spring 的子功能模块,专用于 Web 开发。

Spring MVC 基于 Servlet 实现,将 Web 应用中的数据业务、显示逻辑和控制逻辑进行分层设计。开发者可以直接调用 Spring MVC 框架中 Spring 解耦的组件,快速构建 Web 应用。

Spring 引导

Spring Boot 是用于简化创建 Spring 项目配置流程,快速构建 Spring 应用程序的辅助工具。Spring Boot 本身并不提供 Spring 框架的核心特性以及扩展功能。但 在创建 Spring 项目时,Spring Boot 可以:

  1. 自动添加 Maven 依赖,不需要在 pom.xml 中手动添加配置依赖。
  2. 不需要配置 XML 文件,将全部配置浓缩在一个 appliaction.yml 配置文件中。
  3. 自动创建启动类,代表着本工程项目和服务器的启动加载。
  4. 内嵌 Tomcat 、Jetty 等容器,无需手动部署 war 文件。

Spring Boot 配置

依赖

在Spring Boot中,引入的所有包都是 starter 形式:

spring-boot-starter-web-services,针对 SOAP Web Services spring-boot-starter-web,针对 Web 应用与网络接口 spring-boot-starter-jdbc,针对 JDBC spring-boot-starter-data-jpa,基于 Hibernate 的持久层框架 spring-boot-starter-cache,针对缓存支持

默认映射路径

  • classpath:/META-INF/resources/
  • classpath:/resources/
  • classpath:/static/
  • classpath:/public/

优先级顺序:META-INF/resources > 资源 > static > public

全局配置

位于 resources 文件夹下,支持以下两种格式。由 Spring Boot 自动加载。

  1. application.properties
  2. application.yml
复制代码
`#端口号
server.port=8080
#访问前缀
server.servlet.context-path=/demo`
java 复制代码
#端口号
server.port=8080
#访问前缀
server.servlet.context-path=/demo

#数据库驱动
jdbc.driver=com.mysql.jc.jdbc.Driver
#数据库链接
jdbc.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC
#数据库用户名
jdbc.username=root
#数据库密码
jdbc.password=wdh19970506

#Mybatis
#配置文件路径
mybatis_config_file=mybatis-config.xml
#SQL语句配置路径
mapper_path=/mapper/**.xml
#实体类所在包
type_alias_package=com.example.demo.entity
复制代码
  • JDBC 连接 Mysql5 驱动: com.mysql.jdbc.Driver
  • JDBC 连接 Mysql6 驱动: com.mysql.cj.jdbc.Driver , URL 必须要指定时区 serverTimezone !

多重配置

在 Spring Boot 中,我们往往需要配置多个不同的配置文件去适应不同的环境:

  • application-dev.properties开发环境
  • application-test.properties测试环境
  • application-prod.properties生产环境

只需要在程序默认配置文件 中设置环境,就可以使用指定的配置。application.properties

java 复制代码
spring.profiles.active=dev

启动类

@SpringBootApplication类:作为程序入口,在创建 Spring Boot 项目时自动创建。

等同于 + + ,会自动完成配置并扫描路径下所有包。@Configuration``@EnableAutoConfiguration``@ComponentScan

java 复制代码
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}
复制代码

Spring 需要定义调度程序 servlet ,映射和其他支持配置。我们可以使用 web.xml 文件或 Initializer 类来完成此操作:

java 复制代码
public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.setConfigLocation("com.pingfangushi");
          container.addListener(new ContextLoaderListener(context));
          ServletRegistration.Dynamic dispatcher = container
          .addServlet("dispatcher", new DispatcherServlet(context));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }
}

还需要将 注释添加到 类,并定义一个视图解析器来解析从控制器返回的视图:@EnableWebMvc``@Configuration

java 复制代码
@EnableWebMvc
@Configuration
public class ClientWebConfig implements WebMvcConfigurer { 
   @Bean
   public ViewResolver viewResolver() {
      InternalResourceViewResolver bean
        = new InternalResourceViewResolver();
      bean.setViewClass(JstlView.class);
      bean.setPrefix("/WEB-INF/view/");
      bean.setSuffix(".jsp");
      return bean;
   }
}
相关推荐
BD_Marathon7 小时前
配置文件分类
spring boot
M***Z2107 小时前
springboot中配置logback-spring.xml
spring boot·spring·logback
f***28148 小时前
Springboot中使用Elasticsearch(部署+使用+讲解 最完整)
spring boot·elasticsearch·jenkins
小毅&Nora9 小时前
【后端】【SpringBoot】① 源码解析:从启动到优雅关闭
spring boot·后端·优雅关闭
问今域中10 小时前
Spring Boot 请求参数绑定注解
java·spring boot·后端
计算机程序设计小李同学10 小时前
婚纱摄影集成管理系统小程序
java·vue.js·spring boot·后端·微信小程序·小程序
一 乐10 小时前
绿色农产品销售|基于springboot + vue绿色农产品销售系统(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·后端·宠物
3***688411 小时前
Spring Boot中使用Server-Sent Events (SSE) 实现实时数据推送教程
java·spring boot·后端
k***19511 小时前
Spring 核心技术解析【纯干货版】- Ⅶ:Spring 切面编程模块 Spring-Instrument 模块精讲
前端·数据库·spring
C***u17611 小时前
Spring Boot问题总结
java·spring boot·后端