1.springboot中的测试
1.引入测试依赖
<!--测试的启动器 可以帮助你快速的完成测试 并且这个启动器中封装了junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.构建测试类
@SpringBootTest(classes = App.class) //声明当前类为springboot的测试类 并且指定入口类
@RunWith(value = SpringRunner.class) // 在容器环境下启动测试
public class TestDao {
@Autowired
UserDao userDao;
@Test
public void testUserDao() {
List<User> users = userDao.queryAll();
for (User user : users) {
System.out.println(user);
}
}
}
2.springboot中的配置文件切分
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-85Njgqyw-1594617525780)(.\assets\1561539084795.png)]](https://i-blog.csdnimg.cn/blog_migrate/b32c7c4cd334c2c3cafc872038bbee89.png)
创建三个配置文件 application为主配置文件,两个小配置文件分别为application-xxx.yml,如果在主配文件中想切换小配置文件采用以下配置
spring:
profiles:
active: product
3.springboot中的编码过滤器
web包中自动配置类HttpEncodingAutoConfiguration
@Bean
@ConditionalOnMissingBean(CharacterEncodingFilter.class)
public CharacterEncodingFilter characterEncodingFilter() {
CharacterEncodingFilter filter = new OrderedCharacterEncodingFilter();
filter.setEncoding(this.properties.getCharset().name());
filter.setForceRequestEncoding(this.properties.shouldForce(Type.REQUEST));
filter.setForceResponseEncoding(this.properties.shouldForce(Type.RESPONSE));
return filter;
}
配置文件中配置编码形式
spring:
http:
encoding:
charset: utf-8
force: true #强制使用utf-8
force-request: true #强制请求使用utf-8
force-response: true #强制响应使用utf-8
4.springboot中的热部署
1.导入相关依赖
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bn4oYRMY-1594617525781)(assets\1561537178849.png)]](https://i-blog.csdnimg.cn/blog_migrate/52e27b7afedc9e1e37c67fc2e3e92b6a.png)
<!--热部署依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
注意:只支持jsp的热部署,如果想实现后台代码的热部署,需要进行ide(java的集成开发环境)的进行配置
2.开启idea的自动编译
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-aLI6endm-1594617525781)(assets\1561537662596.png)]](https://i-blog.csdnimg.cn/blog_migrate/fb3fd90c030d1a3422797ab1f06cbe85.png)
3.开启运行自动编译
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-RmvhiQ9s-1594617525782)(assets\1561537823192.png)]](https://i-blog.csdnimg.cn/blog_migrate/ccaecd01ca52906906a2fcb76c6706fd.png)
5.springboot中的jar包部署 java -jar jar包
springboot的jar包的目录结构
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-bRTMFjpb-1594617525783)(assets\1561602579366.png)]](https://i-blog.csdnimg.cn/blog_migrate/ad30bcaa616443f66eb29bac6531f110.png)
将springboot中的jsp加入到META-INF
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZHEqdRrN-1594617525783)(assets\1561603523175.png)]](https://i-blog.csdnimg.cn/blog_migrate/49d577a5433d16b26480d44aa1778986.png)
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
<!--打jar包注意事项1-->
<configuration>
<mainClass>com.baizhi.App</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
<resources>
<!-- 打包时将jsp文件拷贝到META-INF目录下-->
<resource>
<!-- 指定resources插件处理哪个目录下的资源文件 -->
<directory>src/main/webapp</directory>
<!--注意此次必须要放在此目录下才能被访问到-->
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<excludes>
<exclude>**/*.java</exclude>
</excludes>
</resource>
</resources>
6.springboot中的日期格式
日期格式的返回
第一种方式属性上加注解
@JsonFormat(pattern = "YYYY-MM-dd")
第二种方式采用配置文件完成自动配置
spring:
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
日期格式的接受 springboot默认可以转换这种格式 2018/11/11
@DateTimeFormat(pattern = "YYYY-MM-dd")
全局配置文件
spring:
mvc:
date-format: yyyy-MM-dd HH:mm:ss
7.文件上传
springboot文件上传的时候需要配置文件上传大小 默认最大文件为1MB 一次请求最大为 10MB
spring:
http:
multipart:
max-file-size: 200MB
max-request-size: 1000MB
8.springboot注解式aop的开发
1.引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>1.5.8.RELEASE</version>
</dependency>
2.注解式aop相关的注解
@Aspect //代表切面
@Pointcut //代表切入点
@Before() //前置通知
@After() //后置通知
@Around() //环绕通知
@AfterThrowing //异常通知
3.切面的开发
@Aspect
@Component
@Slf4j
public class LogAspect {
@Pointcut(value = "execution(* com.baizhi.service.*.*(..))")
public void pt(){
//空方法
}
// @After(value = "pt()")
// //连接点对象为什么声明就能用 原始方法产生的代理类调用的
// public void aa(JoinPoint joinPoint){
// //额外功能
// log.info("========================{}");
// //原始方法对象
// Signature signature = joinPoint.getSignature();
// //方法名
// String name = joinPoint.getSignature().getName();
// //参数列表
// Object[] args = joinPoint.getArgs();
// //原始方法所在类的对象
// Object target = joinPoint.getTarget();
// //代理对象
// Object aThis = joinPoint.getThis();
// }
//
// @Before(value = "pt()")
// public void bb(JoinPoint joinPoint){
// //额外功能
// log.info("+++++++++++++++++++++");
// //原始方法对象
// Signature signature = joinPoint.getSignature();
// //方法名
// String name = joinPoint.getSignature().getName();
// //参数列表
// Object[] args = joinPoint.getArgs();
// //原始方法所在类的对象
// Object target = joinPoint.getTarget();
// //代理对象
// Object aThis = joinPoint.getThis();
// }
//
@Around(value = "pt()")
public Object cc(ProceedingJoinPoint proceedingJoinPoint){
log.info("------方法执行之前------------");
Object proceed =null;
try {
//原始方法的返回值
proceed = proceedingJoinPoint.proceed();
log.info("this is result=====>{}",proceed);
} catch (Throwable throwable) {
throwable.printStackTrace();
}
log.info("------方法执行之后------------");
//必须返回 否则controller拿不到结果
return proceed;
}
}
4万+

被折叠的 条评论
为什么被折叠?



