spring-boot起步依赖
- 自动整合第三方组件 与 spring/springMVC框架
- 起步依赖本质就是 利用spring spi机制,将组件的配置类,有spring框架在符合条件时,自动完成配置类的实例化。
- 然后在配置类中使用JavaConfig方式(编写代码的方式),完成对组件的配置(故:组件配置类编写时,可以参考xml方式配置,进行实例化一些bean对象,并给bean设置对应的属性值)
spring spi机制
- Java中自带了所谓SPI机制,按照约定去META-INF/services目录里找各个接口的配置文件,找到接口的实现类,然后使用当前线程上线文类加载器定位到实现类加载器,通过其加载实现类,然后再反射newInstance得到实现类的实例。
- Spring里也有类似的SPI,思路与上面类似,从classpath下所有jar包的META-INF/spring.factories 配置文件中加载标识为EnableAutoConfiguration的配置类,然后将其中定义的bean注入到Spring容器。
起步依赖基本原理
- 通过MEAT-INF/spring.factories文件,配置自己的 自动装配类。
- spring在启动过程中,会自动扫描spring.factories文件,并将里面配置的自动装配类进行实例化到spring容器中。
- 通过自动装配类中的 @Configuration注解 + @Bean注解(注解在自动装配类里面的方法上),将组件需要交给Spring容器管理的Bean对象,注入到Spring容器中。
- 当组件的关键Bean对象注入到Spring容器中后,后续代码中可以直接使用。
spring-boot起步依赖约定
- 命名规则:
- 官方提供:spring-boot-starter-xxx,如:spring-boot-starter-web
- 第三方提供:xxx-spring-boot-starter,如:mybatis-spring-boot-starter
示例
- 以配置MyBatis与spring框架为例。写一个自己的MyBatis起步依赖。
起步依赖项目(selfmybatis-spring-boot-starter)
- 新建一个springboot项目,并添加:MyBatis、MyBatis与spring支持、Druid数据库连接池、spring事务依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>self.mybatis</groupId>
<artifactId>selfmybatis-spring-boot-starter</artifactId>
<version>1.0.1</version>
<name>selfmybatis-spring-boot-starter</name>
<description>Demo project for Spring Boot Starter</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- spring-jdbc spring事务 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.18.RELEASE</version>
</dependency>
<!-- mybatis-spring Spring集成MyBatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<!-- druid 阿里巴巴数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.9</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 编写加载application.properties配置信息的类
package self.mybatis.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "self.datasource")
@Data
public class SelfMyBatisProperties {
private String url;
private String user;
private String pwd;
private int maxActive = 5;
private String mapperLocations;
private String basePackage;
}
- 编写自动装配类
package self.mybatis.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import java.io.IOException;
// 表示该类,是一个配置类
@Configuration
// 当classpath下存在SqlSessionFactory.class文件时,才进入实例化该配置自动装配类
@ConditionalOnClass(SqlSessionFactory.class)
// 将SelfMyBatisProperties配置类,启用进来。
@EnableConfigurationProperties(SelfMyBatisProperties.class)
public class SelfMyBatisAutoConfiguration {
// 配置Druid数据库连接池
@Bean(name = "dataSource", initMethod = "init", destroyMethod = "close")
public DruidDataSource dataSource(SelfMyBatisProperties properties) {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(properties.getUrl());
dataSource.setUsername(properties.getUser());
dataSource.setPassword(properties.getPwd());
dataSource.setMaxActive(properties.getMaxActive());
return dataSource;
}
// 配置SqlSessionFactoryBean对象
@Bean(name = "sessionFactory")
public SqlSessionFactoryBean sessionFactory(DruidDataSource dataSource, SelfMyBatisProperties properties) {
SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();
sessionFactoryBean.setDataSource(dataSource);
// 配置MyBatis的mappers路径
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
Resource[] resources = resolver.getResources(properties.getMapperLocations());
sessionFactoryBean.setMapperLocations(resources);
} catch (IOException e) {
e.printStackTrace();
}
// 配置MyBatis的日志输出类
org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
configuration.setLogImpl(org.apache.ibatis.logging.stdout.StdOutImpl.class);
sessionFactoryBean.setConfiguration(configuration);
return sessionFactoryBean;
}
// 配置Dao接口文件扫描配置,这个也可不配置,使用@MapperScan注解完成。
// @MapperScan注解,在使用该起步依赖的项目中的启动类上或配置类上即可。
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(Environment env) {
MapperScannerConfigurer configurer = new MapperScannerConfigurer();
// 这里使用spring的Environment来获取配置,不知道为啥,这里通过SelfMyBatisProperties这个配置类,读取不到配置信息。
// 网上说:因为这个MapperScannerConfigurer类加载早于SelfMyBatisProperties配置类注入。
configurer.setBasePackage(env.getProperty("self.datasource.base-package"));
configurer.setSqlSessionFactoryBeanName("sessionFactory");
return configurer;
}
// 配置 Spring的事务管理器
@Bean
public DataSourceTransactionManager dataSourceTransactionManager(DruidDataSource dataSource) {
return new DataSourceTransactionManager(dataSource);
}
}
- 在src/main/resources/META-INF目录下,配置spring.factories文件,将SelfMyBatisAutoConfiguration作为自动装配类。(META-INF目录自己建一个就可以)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=self.mybatis.config.SelfMyBatisAutoConfiguration
测试起步依赖项目(新建另一个spring-boot项目)
- 新建一个用来测试起步依赖的项目,并在pom中加入需要的依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>test.springboot.thymeleaf</groupId>
<artifactId>springboot-thymeleaf</artifactId>
<version>1.0.1</version>
<name>springboot-thymeleaf</name>
<description>Test Self MyBatis Starter for Spring Boot</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!-- 注释掉官方提供的起步依赖 -->
<!--<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>-->
<!-- mysql连接驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>self.mybatis</groupId>
<artifactId>selfmybatis-spring-boot-starter</artifactId>
<version>1.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>test/springboot/thymeleaf/dao/**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>
- 在application.properties中添加self.datasource配置项
#官方MyBatis配置(注释掉)
#mybatis.mapper-locations=classpath:/gk/springboot/thymeleaf/dao/mapper/**/*.xml
#mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
##使用Druid数据库连接池
#spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
#spring.datasource.url=jdbc:mysql://localhost:3306/book
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#自定义self配置信息
gk.datasource.base-package=gk.springboot.thymeleaf.dao
gk.datasource.driver-name=com.mysql.cj.jdbc.Driver
gk.datasource.mapper-locations=classpath:/self/springboot/thymeleaf/dao/mapper/**/*.xml
gk.datasource.max-active=5
gk.datasource.pwd=root
gk.datasource.url=jdbc:mysql://localhost:3306/book
gk.datasource.user=root
-
写代码,测试。看测试结果
测试结果图
问题
- MapperScannerConfigurer这个Bean创建方法里,读取的SelfMyBatisProperties配置信息全是null?有哪位大神留言解答一下。谢谢
网友评论