一.Spring的注解替代xml文件
1、 @PropertySource
作用:加载*.properties,等价于<context:property-placeholder/>
属性:
value:指定*.properties文件的位置
2、 @Bean
作用:把实体bean放到ioc容器里,等于<bean/>
属性:
value:指定IOC容器的key\
3、 @ComponentScan
作用:指定spring要扫描的包,等价于<context:component-scan base-package="com.by.service">
属性:
value:指定要扫描的包
4、 @Import
作用:导入配置类,等价于<import resource="classpath:applicationContext-dao.xml"/>
属性:
value:配置类的字节码
5、 @Configuration
作用:等价于<beans/>,spring读取到该注解会创建一个ioc容器
二.SpringBoot入门
一、springboot介绍
springboot(spring+springmvc):不是对spring功能的增强,而是提供了一种快速开发spring应用的方式
特点:
简化xml配置
简化maven配置
内嵌tomcat
二、springboot项目搭建
1、创建maven工程,并继承springboot父工程
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
2、添加启动器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3、启动类
package com.by;//(controller、service、mapper的上级目录)
@SpringbootApplication //标识当前类是springboot的启动类
public class SpringbootHelloworldApp{
pulic static void main(String[] args){
SpringApplication.run(SpringbootHelloworldApp.class, agrs);
}
}
@SpringBootApplication
@MapperScan("com.by.mapper") // @MapperScan 用户扫描MyBatis的Mapper接口
public class MybatisApp {
public static void main(String[] args) {
SpringApplication.run(MybatisApp.class, args);
}
}
三、springboot的starter
1、starter是什么?
starter(启动器):是一堆依赖和配置类的集合
2、stareter的命名规范
官方:
前缀:spring-boot-starter-
规范:spring-boot-starter-模块名
举例:spring-boot-starter-web
第三方:
前缀:-spring-boot-starter
规范:模块名-spring-boot-starter
举例:mybatis-spring-boot-starter
四、springboot的配置文件
1、application.properties
server.port=9999
server.servlet.context-path=/springboot-helloworld
2、application.yml(树状接口)
server:
port: 9999
servlet:
context-path: /springboot-helloworld
yml语法:
①“.”------->“:”
②“=”------->“:空格”
③空格缩进
五、springboot的两种发布方式
1、jar方式
1)添加spring-boot-maven-plugin插件(自动检测main函数)
2)打jar包
3)java -jar xxx.jar
2、war方式
1)设置打包方式:<packaging>war</packaging>
2)设置tomcat启动器的依赖范围:<scope>provided</scope>
3)修改启动类(告诉tomcat启动类在哪)
public class SpringbootHelloworldApp extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SpringbootHelloworldApp.class);
}
}
4)打war包
三.springboot整合junit
一、main方法启动spring
new ClasspathXmlApplicationContext("applicationContext.xml");
二、spring整合junit
//@RunWith(SpringJUnit4ClassRunner.class)
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
三、springboot整合junit
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes={SpringbootJunit.class})
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {SpringbootJunitApp.class})
public class UserServiceTest {
@Autowired
private UserServiceImpl userServiceImpl;
@Test
public void testAddUser(){
this.userServiceImpl.addUser();
}
}
04.springboot整合全局异常处理器
一、非ajax
public class GloableExceptionHandler implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
ModelAndView mv = new ModelAndView();
if(e instanceof NullPointerException){
mv.setViewName("error1");
}else if(e instanceof ArithmeticException){
mv.setViewName("error2");
}
mv.addObject("msg", e.toString());
return mv;
}
}
二、ajax
@ControllerAdvice
public class AjaxGlobalExceptionHandler {
@ResponseBody
@ExceptionHandler
public Map errorHandler(Exception e){
Map<String, Object> map = new HashMap<>();
map.put("status", 500);
map.put("msg", e.toString());
return map;//{status:500, msg:异常信息}
}
}
四.springboot整合mybatis
//
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
//
<dependencies>
<!--thymeleaf-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- springBoot 的启动器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Mybatis 启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<!-- mysql 数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<!-- druid 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- 添加 junit 环境的 jar 包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.0</version>
</dependency>
</dependencies>
<build>
<!-- 如果不添加此节点src/main/java目录下的所有配置文件都会被漏掉。 -->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
五、springboot的多环境配置
、多环境配置的语法
application-环境名称.yml
2、创建多套环境
application-dev.yml:
server:
port: 8090
application-test.yml:
server:
port: 8091
...
3、激活环境
application.yml:
spring:
profiles:
active: test
单个yml方式
spring:
profiles.active: dev
# 开发环境配置
spring:
profiles: dev
server:
port: 8080
# 测试环境配置
spring:
profiles: test
server:
port: 8091
# 生产环境配置
spring:
profiles: prod
server:
port: 8092
六.springboot自动配置原理
@SpringBootApplication
@SpringBootConfiguration:标识启动类是一个IOC容器的配置类
@EnableAutoConfiguration:
@AutoConfigurationPackage:扫描启动类所在包及子包中所有的组件,生成实体bean并交给IOC容器管理
@Import({AutoConfigurationImportSelector.class}):会加载META-INF/spring.factories文件,并调用该文件中的自动配置类完成自动配置工作,
所以我们只需再application.yml中提供mysql的url、用户名、密码等信息即可完成mybatis的自动配置
@ComponentScan:配置springboot要扫描的包