[关闭]
@File 2020-01-16T06:21:37.000000Z 字数 8084 阅读 198

spring-boot

web java


相关连接

一、idea 搭建

1. 选择项目类型

image_1dkfrkhpuahp1kjrp75cie3a6p.png-55.3kB

2. 设置项目信息

image_1dkfs322fpb71tgqfbvqi79bc16.png-40.6kB

3. 选择依赖

image_1dkpo8qnv5a5e5umru1ktv15da9.png-99.5kB

4. 存储位置

image_1dkfubd6i1ju4ln5svt74u17ud20.png-24.6kB

5. 等待配置完全加载(右下角 )

image_1dkfufk5oeq2j8127tkecme52d.png-4.8kB

6. 起飞

image_1dkg0p83d10gp1v0n1gq479dg9.png-48kB

二、 依赖

1. springboot(idea快捷创建时自带)

  1. <!-- web服务包 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- mysql -->
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <scope>runtime</scope>
  11. </dependency>
  12. <!-- mybaits -->
  13. <dependency>
  14. <groupId>org.mybatis.spring.boot</groupId>
  15. <artifactId>mybatis-spring-boot-starter</artifactId>
  16. <version>2.1.0</version>
  17. </dependency>
  18. <!-- lombok -->
  19. <dependency>
  20. <groupId>org.projectlombok</groupId>
  21. <artifactId>lombok</artifactId>
  22. <optional>true</optional>
  23. </dependency>

2. druid

  1. <!-- druid 连接池 -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid-spring-boot-starter</artifactId>
  5. <version>1.1.17</version>
  6. </dependency>

3. fastjson

  1. <!-- fastjson -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.46</version>
  6. </dependency>

三、yml配置

yml基本语法规则:

数字:num: 1
字符串:str: 李大爷
布尔值:bool: true
空值:null: ~
数组:array: - value
行对象格式:inline-obj: {name: 李大爷,age: 10}
行数组格式:inline-list: [哈哈哈,啊啊啊,嗯嗯嗯]

1. druid 配置

druid 配置参考

  1. spring:
  2. datasource:
  3. type: com.alibaba.druid.pool.DruidDataSource
  4. druid:
  5. driver-class-name: com.mysql.cj.jdbc.Driver
  6. username: root
  7. password: root
  8. url: jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
  9. # druid 配置
  10. initial-size: 5
  11. max-active: 20
  12. min-idle: 3
  13. # 配置监控统计拦截器 日志配置 Slf4j logback
  14. # stat监控数据库性能
  15. # wall 用于防火墙
  16. # 日志先关 slf4j logback log4j log4j2
  17. filter: stat,wall,slf4j
  18. web-stat-filter:
  19. enabled: true
  20. log-slow-sql: true
  21. url-pattern: '/*'
  22. # 排除不拦截的 请求
  23. exclusions: "*.js,*.png,/druid/*"
  24. stat-view-servlet:
  25. enabled: true
  26. url-pattern: /druid/*
  27. # 是否可以使用重置功能
  28. reset-enable: true
  29. login-username: admin
  30. login-password: admin
  31. # 允许访问的id
  32. allow: 127.0.0.1
  33. # 和名单
  34. deny: ""

2. mybatis整合

  1. mybatis:
  2. # 别名
  3. type-aliases-package: com.vip.demo.domain.entity
  4. mapper-locations: classpath:mappers/**/*.xml
  5. # mybatis-plus

3. log日志

  1. loggin:
  2. level:
  3. com.lidaye.domo: debug

4. 多配置切换

  1. spring:
  2. # 指定到 application-dev.yml
  3. profiles:
  4. active: dev

四、json转换器配置

  1. @Configuration
  2. public class MassageConverConfiguration {
  3. @Bean
  4. public HttpMessageConverters fastJsonHttpMessageConverters() {
  5. // 1、需要先定义一个 convert 转换消息的对象;
  6. FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
  7. // 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
  8. FastJsonConfig fastJsonConfig = new FastJsonConfig();
  9. fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
  10. // 3、在convert中添加配置信息.
  11. fastConverter.setFastJsonConfig(fastJsonConfig);
  12. HttpMessageConverter<?> converter = fastConverter;
  13. return new HttpMessageConverters(converter);
  14. }
  15. }

五、跨域请求 CorsConfig

  1. @Configuration
  2. public class CorsConfig implements WebMvcConfigurer {
  3. @Bean
  4. public WebMvcConfigurer corsConfigurer()
  5. {
  6. return new WebMvcConfigurer() {
  7. @Override
  8. public void addCorsMappings(CorsRegistry registry) {
  9. registry.addMapping("/**")
  10. //允许跨域的域名,可以用*表示允许任何域名使用
  11. .allowedOrigins("*")
  12. //允许任何方法(post、get等)
  13. .allowedMethods("*")
  14. //允许任何请求头
  15. .allowedHeaders("*")
  16. //带上cookie信息
  17. .allowCredentials(true)
  18. //maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
  19. .exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
  20. }
  21. };
  22. }
  23. }

六、注解

  1. // 注意:命名时不要与系统配置冲突
  2. lidaye:
  3. name: 李大爷

1. @Value 取一个配置

value: 配置名

  1. // 一个普通接口类
  2. @RestController
  3. public class DemoController {
  4. // 取配置值
  5. @Value("${lidaye.name}")
  6. private String name;
  7. // 测试接口
  8. @GetMapping("Hello")
  9. public String hello(){
  10. return name;
  11. }
  12. }

2. @ConfigurationProperties 批量取配置

value: 前缀
ignoreInvalidFields:
ignoreUnknownFields:

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-configuration-processor</artifactId>
  4. <optional>true</optional>
  5. </dependency>
  1. @Component
  2. @Data
  3. @ConfigurationProperties("lidaye")
  4. public class User {
  5. private String name;
  6. }
  1. @RestController
  2. public class DemoController {
  3. @Resource
  4. private User user;
  5. @GetMapping("Hello")
  6. public String hello(){
  7. return user.getName();
  8. }
  9. }

3. @MapperScan 指定 mapper 扫描范围

value: 指定的范围(数组)
basePackages:
basePackageClasses:
nameGenerator:
annotationClass:
markerInterface:
sqlSessionTemplateRef:
sqlSessionFactoryRef:
factoryBean:
lazyInitialization:

  1. @SpringBootApplication
  2. @MapperScan("com.vip.demo.mapper")
  3. public class DemoApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. }
  7. }

4. @Configuration 声明配置类

value:
proxyBeanMethods:

5. @Bean 声明配置

value:
autowire:
autowireCandidate:
initMethod:
destroyMethod:

  1. // 主要用在 @Configuration 注解声明的类里
  2. @Configuration
  3. public class AppConfig {
  4. // 方法名就是 id
  5. // 如:<bean id="transferService" class="com.acme.TransferServiceImpl"/>
  6. @Bean
  7. public TransferService transferService() {
  8. return new TransferServiceImpl();
  9. }
  10. }

6. @EnableScheduling 入口开启定时任务

  1. @SpringBootApplication
  2. @EnableScheduling
  3. public class Application {
  4. public static void main(String[] args) throws Exception {
  5. SpringApplication.run(Application.class);
  6. }
  7. }

7. @Scheduled 设置定时任务

scheduled 详细说明

cron: 通过cron表达式控制触发时间(常用)
fixedRate: 间隔多少毫秒再次触发(常用)
fixedDelay: 完成后延迟多少毫秒(常用)
initialDelay: 延迟多少毫秒后再触发(常用)
zone: 时区设置,默认当前时区
fixedRateString: 字符串形式的fixedRate
fixedDelayString: 字符串形式的fixedDelay
initialDelayString: 字符串形式的initialDelay

  1. @Scheduled("0 0 10,14,16 * * ?") // 每天上午10点,下午2点,4点
  2. @Scheduled(0 0/30 9-17 * * ?) // 朝九晚五工作时间内每半小时
  3. @Scheduled(0 0 12 ? * WED) // 表示每个星期三中午12点
  4. @Scheduled("0 0 12 * * ?") // 每天中午12点触发
  5. @Scheduled("0 15 10 ? * *") // 每天上午10:15触发
  6. @Scheduled("0 15 10 * * ?") // 每天上午10:15触发
  7. @Scheduled("0 15 10 * * ? *") // 每天上午10:15触发
  8. @Scheduled("0 15 10 * * ? 2005") // 2005年的每天上午10:15触发
  9. @Scheduled("0 * 14 * * ?") // 在每天下午2点到下午2:59期间的每1分钟触发
  10. @Scheduled("0 0/5 14 * * ?") // 在每天下午2点到下午2:55期间的每5分钟触发
  11. @Scheduled("0 0/5 14,18 * * ?") // 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
  12. @Scheduled("0 0-5 14 * * ?") // 在每天下午2点到下午2:05期间的每1分钟触发
  13. @Scheduled("0 10,44 14 ? 3 WED") // 每年三月的星期三的下午2:10和2:44触发
  14. @Scheduled("0 15 10 ? * MON-FRI") // 周一至周五的上午10:15触发
  15. @Scheduled("0 15 10 15 * ?") // 每月15日上午10:15触发
  16. @Scheduled("0 15 10 L * ?") // 每月最后一日的上午10:15触发
  17. @Scheduled("0 15 10 ? * 6L") // 每月的最后一个星期五上午10:15触发
  18. @Scheduled("0 15 10 ? * 6L 2002-2005") // 2002年至2005年的每月的最后一个星期五上午10:15触发
  19. @Scheduled("0 15 10 ? * 6#3") // 每月的第三个星期五上午10:15触发

8. @PostConstruct 构造方法后执行的方法

9. @PropertySource 读取配置文件

10. @SpringBootApplication 入口声明(不用改)

六、 通过 Aware 接口取容器对象

  1. @Component
  2. public class ApplicationContextHolder implements ApplicationContextAware {
  3. private static ApplicationContext applicationContext;
  4. @Override
  5. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  6. ApplicationContextHolder.applicationContext = applicationContext;
  7. }
  8. public static ApplicationContext getApplicationContext() {
  9. return applicationContext;
  10. }
  11. /**
  12. * 获取已注册的对象
  13. */
  14. public static Object getBean(String name) {
  15. return getApplicationContext().getBean(name);
  16. }
  17. public static <T> T getBean(Class<T> clazz) {
  18. return getApplicationContext().getBean(clazz);
  19. }
  20. public static <T> T getBean(String name, Class<T> clazz) {
  21. return getApplicationContext().getBean(name, clazz);
  22. }
  23. }

七、集成redis

八、生成 war 包

1. 入口文件继承 SpringBootServletInitializer

  1. @SpringBootApplication
  2. // 继承 SpringBootServletInitializer
  3. public class SecKillFrontedApplication extends SpringBootServletInitializer {
  4. public static void main(String[] args) {
  5. SpringApplication.run(SecKillFrontedApplication.class, args);
  6. }
  7. // 重写 configure 方法
  8. @Override
  9. protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
  10. // 指定打包当前项目资源信息
  11. return builder.sources(SecKillFrontedApplication.class);
  12. }
  13. }

2. 修改依赖

  1. <!-- 修改打包方式 -->
  2. <packaging>war</packaging>
  3. <!-- jar包排除 -->
  4. <dependencies>
  5. <dependency>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-web</artifactId>
  8. <exclusions>
  9. <!-- 排除tomcat -->
  10. <exclusion>
  11. <groupId>org.springframework.boot</groupId>
  12. <artifactId>spring-boot-starter-tomcat</artifactId>
  13. </exclusion>
  14. </exclusions>
  15. </dependency>
  16. <!-- 重新导入tomcat -->
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter-tomcat</artifactId>
  20. <!-- 提供编译,但是打包的时候不加入到项目的libs下面 -->
  21. <scope>provided</scope>
  22. </dependency>
  23. </dependencies>
  24. <!-- 暂时不知道有什么用 -->
  25. <build>
  26. <finalName>sec-kill</finalName>
  27. </build>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注