springboot高级功能(二)springboot核心架构,自定义start全解析(start中配置从数据源)


创建一个新的maven项目

新建Properties类(相当于调用的项目传入start的参数)

其中需要@ConfigurationProperties注解

prefix = "login" 的意思是调用的spring boot项目 application.properties中配置的参数

调用时传入的为

1. login.className = com.mysql.jdbc.Driver
2. login.url=jdbc:mysql://localhost:3306/humanresource?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
3. login.user=root
4. login.password=root
1. @ConfigurationProperties(prefix = "login")
2. public class LoginCheckProperties {
3. 
4. private String className;
5. private String url;
6. private String user;
7. private String password;
8. 
9. public String getClassName() {
10. return className;
11.     }
12. 
13. public void setClassName(String className) {
14. this.className = className;
15.     }
16. 
17. public String getUrl() {
18. return url;
19.     }
20. 
21. public void setUrl(String url) {
22. this.url = url;
23.     }
24. 
25. public String getUser() {
26. return user;
27.     }
28. 
29. public void setUser(String user) {
30. this.user = user;
31.     }
32. 
33. public String getPassword() {
34. return password;
35.     }
36. 
37. public void setPassword(String password) {
38. this.password = password;
39.     }
40. }

Config类

其中@EnableConfigurationProperties(LoginCheckProperties.class) 注解为固定写法 意义是实现自动配置 ,类为上文中的Properties类

1. @Configuration
2. @EnableConfigurationProperties(LoginCheckProperties.class)
3. @MapperScan(basePackages = MasterDataSourceConfig.PACKAGE, sqlSessionFactoryRef = "masterSqlSessionFactory")
4. public class MasterDataSourceConfig {
5. 
6. // 精确到 master 目录,以便跟其他数据源隔离
7. static final String PACKAGE = "com.airboot.bootdemo.dao.master";
8. static final String MAPPER_LOCATION = "classpath*:mapper/master/*.xml";
9. 
10. private String className;
11. private String url;
12. private String user;
13. private String password;
14. 
15. public MasterDataSourceConfig(LoginCheckProperties loginCheckProperties) {
16. this.className = loginCheckProperties.getClassName();
17. this.url = loginCheckProperties.getUrl();
18. this.user = loginCheckProperties.getUser();
19. this.password = loginCheckProperties.getPassword();
20.     }
21. 
22. @Bean(name = "masterDataSource")
23. public DataSource masterDataSource() {
24. DruidDataSource dataSource = new DruidDataSource();
25.         dataSource.setDriverClassName(className);
26.         dataSource.setUrl(url);
27.         dataSource.setUsername(user);
28.         dataSource.setPassword(password);
29. //dataSource.setDriverClassName("com.mysql.jdbc.Driver");
30. //dataSource.setUrl("jdbc:mysql://localhost:3306/humanresource?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC");
31. //dataSource.setUsername("root");
32. //dataSource.setPassword("root");
33. return dataSource;
34.     }
35. 
36. @Bean(name = "masterTransactionManager")
37. 
38. public DataSourceTransactionManager masterTransactionManager() {
39. return new DataSourceTransactionManager(masterDataSource());
40.     }
41. 
42. @Bean(name = "masterSqlSessionFactory")
43. public SqlSessionFactory masterSqlSessionFactory(@Qualifier("masterDataSource") DataSource masterDataSource)
44. throws Exception {
45. final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
46.         sessionFactory.setDataSource(masterDataSource);
47.         sessionFactory.setMapperLocations(
48. new PathMatchingResourcePatternResolver().getResources(MasterDataSourceConfig.MAPPER_LOCATION));
49. return sessionFactory.getObject();
50.     }
51. 
52. 
53. }

META-INF/spring.factories

最后在resources中新建文件夹META-INF,在其中建立文件spring.factories.路径为上文Config中的路径

org.springframework.boot.autoconfigure.EnableAutoConfiguration=logincheckspringbootstarter.config.ds.MasterDataSourceConfig

如果要暴露多个使用以下代码

1. org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
2. com.*.*.*.*Util,\
3. com.*.*.*.*Util,\
4. com.*.*.*.*Util,\
5. com.*.*.*.*Util,\
6. com.*.*.*.*Util

如果想要在jar中加入注释 则需要在pom文件中修改以下代码 这样会生成一个 *.jar 和一个*:source.jar 需要将这两个jar包都加入项目中。

1. <build>
2. <plugins>
3. <plugin>
4. <groupId>org.apache.maven.plugins</groupId>
5. <artifactId>maven-source-plugin</artifactId>
6. <version>2.4</version>
7. <configuration>
8. <aggregate>true</aggregate>
9. </configuration>
10. <executions>
11. <execution>
12. <id>attach-javadocs</id>
13. <goals>
14. <goal>jar</goal>
15. </goals>
16. <configuration>
17. <additionalparam>-Xdoclint:none</additionalparam>
18. </configuration>
19. </execution>
20. </executions>
21. </plugin>
22. </plugins>
23. </build>

打包

使用命令mvn install

引入jar包

其中groupId 公司名称 我这里是cn.baocl

       artifactId为项目名

这些在start项目的pom文件中配置

上一下pom文件

1. <?xml version="1.0" encoding="UTF-8"?>
2. <project xmlns="http://maven.apache.org/POM/4.0.0"
3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5. <modelVersion>4.0.0</modelVersion>
6. 
7. <groupId>cn.baocl</groupId>
8. <artifactId>check-spring-boot-starter</artifactId>
9. <version>1.1-SNAPSHOT</version>
10. 
11. <dependencies>
12. <dependency>
13. <groupId>org.springframework.boot</groupId>
14. <artifactId>spring-boot-starter-web</artifactId>
15. <version>2.2.1.RELEASE</version>
16. </dependency>
17. 
18. <dependency>
19. <groupId>org.mybatis.spring.boot</groupId>
20. <artifactId>mybatis-spring-boot-starter</artifactId>
21. <version>1.3.2</version>
22. </dependency>
23. <!-- Druid 数据连接池依赖 -->
24. <dependency>
25. <groupId>com.alibaba</groupId>
26. <artifactId>druid</artifactId>
27. <version>1.1.17</version>
28. </dependency>
29. 
30. <dependency>
31. <groupId>org.springframework.boot</groupId>
32. <artifactId>spring-boot-devtools</artifactId>
33. <scope>runtime</scope>
34. <version>2.2.1.RELEASE</version>
35. </dependency>
36. 
37. <dependency>
38. <groupId>mysql</groupId>
39. <artifactId>mysql-connector-java</artifactId>
40. <scope>runtime</scope>
41. <version>8.0.16</version>
42. </dependency>
43. 
44. <dependency>
45. <groupId>org.springframework.boot</groupId>
46. <artifactId>spring-boot-configuration-processor</artifactId>
47. <optional>true</optional>
48. <version>2.2.1.RELEASE</version>
49. </dependency>
50. </dependencies>
51. 
52. </project>
1. <dependency>
2. <groupId>cn.baocl</groupId>
3. <artifactId>check-spring-boot-starter</artifactId>
4. <version>1.1-SNAPSHOT</version>
5. </dependency>

使用

然后就可以使用了 本文实现的为主项目定义一个从数据源。所以在主项目中

com.airboot.bootdemo.dao.master,classpath*:mapper/master/*.xml

这两个路径文件使用的就是start中的配置的数据源

阿里多数据源配置的博客请参考:

项目结构

springboot高级功能(二)springboot核心架构,自定义start全解析(start中配置从数据源)


上一篇:Spring高级应用之注入嵌套Bean


下一篇:vue observer 源码学习