在springboot相比于springmvc提供了一个极为重要的功能,就是自定义start(自定义jar包),同时spring也提供了一大波start。如spring-boot-starter-data-redis,spring-boot-starter-amqp。本文将教你如何自定义start。例子为从start中获取数据源。
1.自定义start能干什么
在架构考虑时,或许有一些通用方法抽取成start,在项目中使用时,直接引入即可。
2.创建自定义start
1.创建一个新的maven项目
创建一个普通的maven项目,作为start的开发环境
2.新建application.properties
在maven中创建application.properties文件。同时添加以下代码,作为常量配置。
login.className = com.mysql.jdbc.Driver login.url=jdbc:mysql://localhost:3306/humanresource?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC login.user=root login.password=root
3.创建LoginCheckProperties
创建类LoginCheckProperties,其中需要@ConfigurationProperties注解,prefix = “login” 的意思是调用项目中 ,application.properties中配置的参数。
@ConfigurationProperties(prefix = "login") public class LoginCheckProperties { private String className; private String url; private String user; private String password; public String getClassName() { return className; } public void setClassName(String className) { this.className = className; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getUser() { return user; } public void setUser(String user) { this.user = user; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
4.Config类
其中@EnableConfigurationProperties(LoginCheckProperties.class) 注解为固定写法,意义是实现自动配置上文中的Properties类。
@Configuration @EnableConfigurationProperties(LoginCheckProperties.class) @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory") public class MasterDataSourceConfig { // 精确到 master 目录,以便跟其他数据源隔离 static final String PACKAGE = "com.airboot.bootdemo.dao.master"; static final String MAPPER_LOCATION = "classpath*:mapper/master/*.xml"; private String className; private String url; private String user; private String password; public MasterDataSourceConfig(LoginCheckProperties loginCheckProperties) { this.className = loginCheckProperties.getClassName(); this.url = loginCheckProperties.getUrl(); this.user = loginCheckProperties.getUser(); this.password = loginCheckProperties.getPassword(); } @Bean(name = "masterDataSource") public DataSource masterDataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(className); dataSource.setUrl(url); dataSource.setUsername(user); dataSource.setPassword(password); return dataSource; } @Bean(name = "masterTransactionManager") public DataSourceTransactionManager masterTransactionManager() { return new DataSourceTransactionManager(masterDataSource()); } @Bean(name = "masterSqlSessionFactory") public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource) throws Exception { final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(masterDataSource); sessionFactory.setMapperLocations( new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION)); return sessionFactory.getObject(); } }
5.添加META-INF/spring.factories
最后在resources中新建文件夹META-INF,在其中建立文件spring.factories路径为上文Config中的路径。(需要向被引用者暴露)
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.*.*.*.*A,\ com.*.*.*.*B,\ com.*.*.*.*C,\ com.*.*.*.*D,\ com.*.*.*.*Util
6.显示start注释(选读)
如果想要在jar中加入注释 则需要在pom文件中修改以下代码 这样会生成一个 .jar 和一个:source.jar 需要将这两个jar包都加入项目中。
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-source-plugin</artifactId> <version>2.4</version> <configuration> <aggregate>true</aggregate> </configuration> <executions> <execution> <id>attach-javadocs</id> <goals> <goal>jar</goal> </goals> <configuration> <additionalparam>-Xdoclint:none</additionalparam> </configuration> </execution> </executions> </plugin> </plugins> </build>
7.打包
使用命令mvn install。即可生成jar包。
8.引入jar包
<dependency> <groupId>cn.baocl</groupId> <artifactId>check-spring-boot-starter</artifactId> <version>1.1-SNAPSHOT</version> </dependency>
在引用项目中的pom文件配置groupId,artifactId,version即可。下文为start中的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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.baocl</groupId> <artifactId>check-spring-boot-starter</artifactId> <version>1.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- Druid 数据连接池依赖 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.17</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <version>2.2.1.RELEASE</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>8.0.16</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> <version>2.2.1.RELEASE</version> </dependency> </dependencies> </project>
9.使用
然后就可以使用了,本文实现的为主项目定义一个从数据源。所以在主项目中就可以使用上文定义的数据源啦。
10.项目结构