@File
2020-01-16T06:21:37.000000Z
字数 8084
阅读 198
web
java
<!-- web服务包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- mybaits -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- druid 连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.17</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
yml基本语法规则:
数字:
num: 1
字符串:str: 李大爷
布尔值:bool: true
空值:null: ~
数组:array: - value
行对象格式:inline-obj: {name: 李大爷,age: 10}
行数组格式:inline-list: [哈哈哈,啊啊啊,嗯嗯嗯]
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/boot?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
# druid 配置
initial-size: 5
max-active: 20
min-idle: 3
# 配置监控统计拦截器 日志配置 Slf4j logback
# stat监控数据库性能
# wall 用于防火墙
# 日志先关 slf4j logback log4j log4j2
filter: stat,wall,slf4j
web-stat-filter:
enabled: true
log-slow-sql: true
url-pattern: '/*'
# 排除不拦截的 请求
exclusions: "*.js,*.png,/druid/*"
stat-view-servlet:
enabled: true
url-pattern: /druid/*
# 是否可以使用重置功能
reset-enable: true
login-username: admin
login-password: admin
# 允许访问的id
allow: 127.0.0.1
# 和名单
deny: ""
mybatis:
# 别名
type-aliases-package: com.vip.demo.domain.entity
mapper-locations: classpath:mappers/**/*.xml
# mybatis-plus
loggin:
level:
com.lidaye.domo: debug
spring:
# 指定到 application-dev.yml
profiles:
active: dev
@Configuration
public class MassageConverConfiguration {
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
// 1、需要先定义一个 convert 转换消息的对象;
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
// 2、添加fastJson 的配置信息,比如:是否要格式化返回的json数据;
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
// 3、在convert中添加配置信息.
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
}
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Bean
public WebMvcConfigurer corsConfigurer()
{
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
//允许跨域的域名,可以用*表示允许任何域名使用
.allowedOrigins("*")
//允许任何方法(post、get等)
.allowedMethods("*")
//允许任何请求头
.allowedHeaders("*")
//带上cookie信息
.allowCredentials(true)
//maxAge(3600)表明在3600秒内,不需要再发送预检验请求,可以缓存该结果
.exposedHeaders(HttpHeaders.SET_COOKIE).maxAge(3600L);
}
};
}
}
// 注意:命名时不要与系统配置冲突
lidaye:
name: 李大爷
@Value
取一个配置value: 配置名
// 一个普通接口类
@RestController
public class DemoController {
// 取配置值
@Value("${lidaye.name}")
private String name;
// 测试接口
@GetMapping("Hello")
public String hello(){
return name;
}
}
@ConfigurationProperties
批量取配置value: 前缀
ignoreInvalidFields:
ignoreUnknownFields:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
@Component
@Data
@ConfigurationProperties("lidaye")
public class User {
private String name;
}
@RestController
public class DemoController {
@Resource
private User user;
@GetMapping("Hello")
public String hello(){
return user.getName();
}
}
@MapperScan
指定 mapper 扫描范围value: 指定的范围(数组)
basePackages:
basePackageClasses:
nameGenerator:
annotationClass:
markerInterface:
sqlSessionTemplateRef:
sqlSessionFactoryRef:
factoryBean:
lazyInitialization:
@SpringBootApplication
@MapperScan("com.vip.demo.mapper")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@Configuration
声明配置类value:
proxyBeanMethods:
@Bean
声明配置value:
autowire:
autowireCandidate:
initMethod:
destroyMethod:
// 主要用在 @Configuration 注解声明的类里
@Configuration
public class AppConfig {
// 方法名就是 id
// 如:<bean id="transferService" class="com.acme.TransferServiceImpl"/>
@Bean
public TransferService transferService() {
return new TransferServiceImpl();
}
}
@EnableScheduling
入口开启定时任务
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class);
}
}
@Scheduled
设置定时任务cron: 通过cron表达式控制触发时间(常用)
fixedRate: 间隔多少毫秒再次触发(常用)
fixedDelay: 完成后延迟多少毫秒(常用)
initialDelay: 延迟多少毫秒后再触发(常用)
zone: 时区设置,默认当前时区
fixedRateString: 字符串形式的fixedRate
fixedDelayString: 字符串形式的fixedDelay
initialDelayString: 字符串形式的initialDelay
@Scheduled("0 0 10,14,16 * * ?") // 每天上午10点,下午2点,4点
@Scheduled(0 0/30 9-17 * * ?) // 朝九晚五工作时间内每半小时
@Scheduled(0 0 12 ? * WED) // 表示每个星期三中午12点
@Scheduled("0 0 12 * * ?") // 每天中午12点触发
@Scheduled("0 15 10 ? * *") // 每天上午10:15触发
@Scheduled("0 15 10 * * ?") // 每天上午10:15触发
@Scheduled("0 15 10 * * ? *") // 每天上午10:15触发
@Scheduled("0 15 10 * * ? 2005") // 2005年的每天上午10:15触发
@Scheduled("0 * 14 * * ?") // 在每天下午2点到下午2:59期间的每1分钟触发
@Scheduled("0 0/5 14 * * ?") // 在每天下午2点到下午2:55期间的每5分钟触发
@Scheduled("0 0/5 14,18 * * ?") // 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
@Scheduled("0 0-5 14 * * ?") // 在每天下午2点到下午2:05期间的每1分钟触发
@Scheduled("0 10,44 14 ? 3 WED") // 每年三月的星期三的下午2:10和2:44触发
@Scheduled("0 15 10 ? * MON-FRI") // 周一至周五的上午10:15触发
@Scheduled("0 15 10 15 * ?") // 每月15日上午10:15触发
@Scheduled("0 15 10 L * ?") // 每月最后一日的上午10:15触发
@Scheduled("0 15 10 ? * 6L") // 每月的最后一个星期五上午10:15触发
@Scheduled("0 15 10 ? * 6L 2002-2005") // 2002年至2005年的每月的最后一个星期五上午10:15触发
@Scheduled("0 15 10 ? * 6#3") // 每月的第三个星期五上午10:15触发
@PostConstruct
构造方法后执行的方法@PropertySource
读取配置文件@SpringBootApplication
入口声明(不用改)ApplicationContextAware
接口的类
@Component
public class ApplicationContextHolder implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextHolder.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取已注册的对象
*/
public static Object getBean(String name) {
return getApplicationContext().getBean(name);
}
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}
}
@SpringBootApplication
// 继承 SpringBootServletInitializer
public class SecKillFrontedApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SecKillFrontedApplication.class, args);
}
// 重写 configure 方法
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
// 指定打包当前项目资源信息
return builder.sources(SecKillFrontedApplication.class);
}
}
<!-- 修改打包方式 -->
<packaging>war</packaging>
<!-- jar包排除 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!-- 排除tomcat -->
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 重新导入tomcat -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<!-- 提供编译,但是打包的时候不加入到项目的libs下面 -->
<scope>provided</scope>
</dependency>
</dependencies>
<!-- 暂时不知道有什么用 -->
<build>
<finalName>sec-kill</finalName>
</build>