springboot简写笔记一

springboot简写笔记

springboot的诞生让spring,springmvc整合得更密切并且使mvc的xml配置简化得更方便,省去了一大堆代码,而且不需要web项目,直接内部集成tomcat,starter方法配上注解@SpringBootApplication便可以直接通过运行来启动web程序。

随着动态语⾔流⾏(Ruby、Scala、NodeJs等),Java 开发变得相对笨重,配置繁琐,开发效率低下,部署流程复杂,以及第三⽅集成难度也相对较⼤,针对该环境,Spring Boot 被开发出来,其使⽤“习惯⼤于配置⽬标”,借助Spring Boot 能够让项⽬快速运⾏起来,同时借助 Spring Boot 可以快速创建 web应⽤并独⽴进⾏部署(jar包 war 包⽅式,内嵌 servlet 容器),同时借助 Spring Boot 在开发应⽤时可以不⽤或很少去进⾏相关 xml 环境配置,简化了开发,⼤⼤提⾼项⽬开发效率。

基本注解:

@Component: 组件 没有明确规定其⻆⾊,作⽤在类级别上声明当前类为⼀个业务组件,被Spring Ioc容器维护
@Service: 在业务逻辑层(Service 层)类级别进⾏声明
@Repository: 在数据访问层(dao 层) 类级别声明
@Controller: 在展现层(MVC) 使⽤ 标注当前类为⼀个控制器

@AutoWired: Spring 官⽅提供注解
@Inject: JSR-330 提供注解(标准制定⽅)
@Resource: JSR-250 提供注解

@Configuration: 作⽤与类上,将当前类声明为⼀个配置类,相当于⼀个xml 配置⽂件
@ComponentScan: ⾃动扫描指定包下标注有@Repository,@Service,@Controller
@Component: 注解的类并由Ioc 容器进⾏实例化和维护
@Bean: 作⽤于⽅法上,相当于xml ⽂件中<bean> 声明当前⽅法返回值为⼀个bean
@Value: 获取properties ⽂件指定key value值

创建一个简单的springboot

创建一个java的maven项目,添加依赖

<parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.2.2.RELEASE</version>
    </parent>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
    </dependencies>

导入web坐标

<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

controller层代码

@Controller
public class HelloController {
@RequestMapping("hello")
@ResponseBody
public String hello(){
return "Hello SpringBoot";
}
}

starter.java启动程序

@SpringBootApplication
public class Starter{
public static void main(String[] args) {
	SpringApplication.run(Starter.class);
	}
}

效果图:
在这里插入图片描述
启动的时候会有一个图标,在src/main/resources ⽬录下的 banner.txt 图标⽂件,然后去网站 [图标模样]可以选取模样(http://patorjk.com/software/taag/#p=display&f=Graffiti&t=Type%20Something)在这里插入图片描述

在这里插入图片描述

配置文件:
我们用application.yml文件来代替xml文件
比如

server:
  # 设置项目启动的端口号
  port: 8080
  # 设置项目的访问路径(上下文路径)
  servlet:
    context-path: /springboot_mybatis
## 数据源配置
spring:
  datasource:
    type: com.mchange.v2.c3p0.ComboPooledDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/scott?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8
    username: root
    password: root

就是代替application.properties文件。

Profile 配置

我们有时候会通过不同的配置环境来运行,所以可以写多个开发环境配置
application-dev.yml 开发环境配置⽂件

server:
	port: 8989

application-test.yml 测试环境配置⽂件

server:
	port: 9999

我们只需要在主配置文件中引入其文件就可以了
application.yml 主配置⽂件

## 环境选择配置
spring:
	profiles:
		active: dev

项⽬中⽇志信息输出

在springboot中我们常用slf4j的日志而不是log4j,导入依赖后。

package com.xxxx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Starter {
	private static Logger logger = LoggerFactory.getLogger(Starter.class);
	public static void main(String[] args) {
		logger.info("SpringBoot 应⽤开始启动...");
		SpringApplication.run(Starter.class);
}
}

在yml配置文件中我们可以设置日志的输出格式

logging:
	pattern:
		console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger- %msg%n"
		level: debug
	file:
		path: "."
		name: "springboot.log"

SpringBoot 静态资源访问

这里的静态文件指的是image,css或者js等文件,springboot指定在resources下的public和static目录下放静态资源,springboot会自动扫描文件夹。

Freemarker & Thymeleaf 视图技术集成

和springmvc大致相同,这里的使用方式是,默认在resources的templates下,课通过配置文件更改路径以及文件格式:

spring:
  freemarker:
    suffix: .ftl
    content-type: text/html
    charset: UTF-8
    template-loader-path: classpath:/views/
  thymeleaf:
      prefix: classpath:/html/
      ## 关闭 thymeleaf ⻚⾯缓存
      cache: false

在这里插入图片描述

SpringBoot应⽤打包与部署

在公司部署项目的时候我们常常要打包
在这里插入图片描述

maven下clean compile package -Dmaven.test.skip=true 执⾏打包命令,target ⽬录会得到待部署的项⽬⽂件。
在这里插入图片描述
如何运行打包好的项目:
方式一:在cmd中输入命令
在这里插入图片描述
方式二:
把打好的包放入tomcat下的webapps目录下,再去bin目录找到start.bat运行tomcat就会自动运行包。

如果想打war包:
目录的依赖下要插入
在这里插入图片描述
并且starter中要添加:

@SpringBootApplication
public class Starter extends SpringBootServletInitializer {
	private static Logger logger = LoggerFactory.getLogger(Starter.class);
	public static void main(String[] args) {
		logger.info("SpringBoot 应⽤开始启动...");
		SpringApplication.run(Starter.class);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder
builder) {
		return builder.sources(Starter.class);
}
}

再执行jar包相同的操作就行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值