Spring自定义starter依赖以及相关引入依赖无法导入的问题

一 自定义Starter

1 starter依赖介绍与使用原理

1.Starter介绍    
Starter场景启动器是Spring Boot中一种非常重要的机制,它将繁琐的配置统一集成到stater中,我们只需要通过在Maven将starter的依赖导入项目,SpringBoot 就能自动扫描并加载相应的默认配置。starter的出现简化了开发人员的工作量,从繁琐的框架配置中解救出来,让更多的时间专注在业务逻辑的开发,提高了开发效率。

2.命名规范    
SpringBoot 提供的 starter 以 spring-boot-starter-xxx 的形式命名。为了与 SpringBoot 生态提供的 starter 进行区分,官方建议第三方开发者或技术(例如 Druid、Mybatis 等等)厂商自定义的 starter 使用 xxx-spring-boot-starter 的形式命名,例如 mybatis-spring-boot-starter、druid-spring-boot-starter 等等。

3.模块规范    
Spring Boot 官方建议我们在自定义 starter 时,创建两个 Module :autoConfigure Module(SpringBoot模块) 和 starter Module(Maven 模块),其中 starter Module 依赖于 autoConfigure Module。当然,这只是 Spring Boot 官方的建议,并不是硬性规定,若不需要自动配置代码和依赖项目分离,我们也可以将它们组合到同一个 Module 里。

4.自定义Starter
starter启动原理
starter-pom引入 autoconfigure 包

autoconfigure包中配置使用 META-INF/spring.factories 中 EnableAutoConfiguration 的值,使得项目启动加载指定的自动配置类

编写自动配置类 xxxAutoConfiguration -> xxxxProperties
@Configuration //配置类

@ConditionalOnMissingBean //条件装配

@EnableConfigurationProperties(xxxProperties.class) //开启属性绑定功能(在配置文件中修改值)+将绑定类xxxProperties加入容器

@Bean //生成组件

引入starter — xxxAutoConfiguration — 容器中放入组件 ---- 绑定xxxProperties ---- 配置项修改

2 新建maven工程

该起始依赖主要用于动态读取配置文件,通过springmvc响应给请求页面
在这里插入图片描述

3 引入所需相关依赖

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>demo-spring-boot-starter</artifactId>
    <version>0.0.1-RELEASE</version>
    <name>demo-spring-boot-starter</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
			// springmvc需要用到
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>
</project>

4 定义实体类映射配置信息

package com.demo.starter.properties;

import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * 描述:配置信息 实体
 *
 **/
//  @ConfigurationProperties(prefix = "demo") 它可以把配置文件中叫“demo”的配置项名称映射成实体类,比如我们这里指定 prefix = "demo" 这样,我们就能将以demo为前缀的配置项拿到了。
@ConfigurationProperties(prefix = "demo")
public class DemoProperties {
    private String sayWhat;
    private String toWho;

    public String getSayWhat() {
        return sayWhat;
    }

    public void setSayWhat(String sayWhat) {
        this.sayWhat = sayWhat;
    }

    public String getToWho() {
        return toWho;
    }

    public void setToWho(String toWho) {
        this.toWho = toWho;
    }
}

5 定义一个service返回信息

package com.demo.starter.service;

/**
 * 描述:随便定义一个Service,用于把读取到的配置文件信息返回
 *
 **/
public class DemoService {
    public String sayWhat;
    public String toWho;
    public DemoService(String sayWhat, String toWho){
        this.sayWhat = sayWhat;
        this.toWho = toWho;
    }
    public String say(){
        return this.sayWhat + "!  " + toWho;
    }
}

6 定义一个被spring管理的自动配置类

这里,我们将DemoService类定义为一个Bean,交给Ioc容器。

▲ @Configuration 注解就不多说了。

▲ @EnableConfigurationProperties 注解。该注解是用来开启对3步骤中 @ConfigurationProperties 注解配置Bean的支持。也就是@EnableConfigurationProperties注解告诉Spring Boot 能支持@ConfigurationProperties。

当然了,也可以在 @ConfigurationProperties 注解的类上添加 @Configuration 或者 @Component 注解

▲ @ConditionalOnProperty 注解控制 @Configuration 是否生效。简单来说也就是我们可以通过在yml配置文件中控制 @Configuration 注解的配置类是否生效。

package com.demo.starter.config;

import com.demo.starter.properties.DemoProperties;
import com.demo.starter.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 描述:配置类
 *
 **/
@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnProperty(
        prefix = "demo",
        name = "isopen",
        havingValue = "true"
)
public class DemoConfig {
    @Autowired
    private DemoProperties demoProperties;

    @Bean(name = "demo")
    public DemoService demoService(){
        return new DemoService(demoProperties.getSayWhat(), demoProperties.getToWho());
    }
}

7 创建spring.factories文件

新建META-INF文件夹,然后创建spring.factories文件,

在这里插入图片描述
在该文件中加入如下配置,该配置指定上步骤中定义的配置类为自动装配的配置。

// spring启动后通过以下路径查找,就能把我们自定义的starter注册进入容器内,实现自动配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.demo.starter.config.DemoConfig

8 maven打包放入本地仓库

在demo-spring-boot-starter工程中执行mvn clean 然后install 一个自定义的starter就放入进本地仓库了。

9 测试

新建一个测试项目
然后打开mavn刷新本地仓库
在这里插入图片描述
引入自定义依赖

           <dependency>
             <groupId>com.demo</groupId>
             <artifactId>demo-spring-boot-starter</artifactId>
             <version>0.0.1-RELEASE</version>
            </dependency>

在application.property中编写配置文件

 demo.isopen=true
 demo.say-what=hello
 demo.to-who=shf
 server.port = 8080

编写测试类

package com.example.test.controller;

import com.demo.starter.service.DemoService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *
 **/
@RestController
public class DemoController {
    @Resource(name = "demo")
    private DemoService demoService;

    @GetMapping("/say")
    public String sayWhat(){
        return demoService.say();
    }

}

打开浏览器访问
在这里插入图片描述

二 常见问题

自定义starter无法引入,一直爆红

记得要刷新maven,重新扫描本地仓库

spring boot maven install 生成jar含有BOOT-INF ,导致被其他工程依赖时找不到类

自定义的依赖能够被成功引入,但是引入后项目无法使用,一直爆红,这多是因为依赖中含有BOOT-INf,导致被其他工程依赖时找不到类。
解决方法:

首先jar中多了这个BOOT-INF文件夹的原因,主要是因为我们在maven的pom文件中加入了spring-boot-maven-plugin这个插件。 既然知道原因了,那么怎么解决呢?其实非常简单,我们只需要将加个configuration标签,并在里面嵌套加入一个skip子标签,并将skip的值设为true,意思也就是跳过这个插件的配置 .或者直接注释掉

<plugins>
    <plugin>
        <groupId>org.spingframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
            <mainClass> XXXXX </mainClass>
            <skip>true</skip>
        </configuration>
    </plugin>
</plugins>
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot StarterSpring Boot 应用程序的预配置模块,可以帮助我们快速集成常用框架。MyBatis 是一个持久层框架,用于映射 Java 对象到数据库表。 如果要在 Spring Boot 应用程序中使用 MyBatis,可以创建自定义Spring Boot Starter 来简化配置。 步骤如下: 1. 创建一个 maven 项目,并在 pom 文件中添加依赖 spring-boot-starter、mybatis-spring-boot-starter。 2. 创建一个配置类,来配置 MyBatis。 3. 创建一个自动配置类,用于自动配置 MyBatis。 4. 创建一个 starter 类,用于向 Spring Boot 提供自动配置。 5. 在 pom 文件中添加相关信息,用于发布到 maven 仓库。 6. 发布到 maven 仓库,并在其他项目中使用。 如果你想要详细了解,可以参考官网上关于 Spring Boot Starter 的文档。 ### 回答2: 要自定义 Spring Boot Starter 操作 MyBatis 数据库,可以按照以下步骤进行操作: 1. 创建一个 Maven 项目,并指定父项目为 Spring Boot Starter Parent。 2. 在项目的 pom.xml 文件中添加必要的依赖,包括 Spring Boot Starter、MyBatis 和相应的数据库驱动程序。 3. 创建一个自定义的配置类,用于配置 MyBatis 的数据源、事务管理器等。 4. 在配置类中使用 @Configuration 注解标注该类为配置类,并使用 @EnableConfigurationProperties 注解引入配置属性。 5. 创建一个自定义的 Starter 类,用于自动配置 MyBatis 相关的组件。 6. 在 Starter 类中使用 @Configuration 注解标注该类为配置类,并使用 @EnableAutoConfiguration 注解启用自动配置。 7. 在 Starter 类中使用 @ConditionalOnClass 注解,指定条件,在类路径下存在 MyBatis 相关的类时才进行自动配置。 8. 在 Starter 类中使用 @Import 注解,导入配置类,将自定义的配置应用到 Spring Boot 项目中。 9. 编写自定义的配置文件,用于配置 MyBatis 的相关属性,例如数据库连接信息、Mapper 扫描路径等。 10. 在项目的 pom.xml 文件中添加 spring.factories 文件,将自定义的 Starter 类注册到 Spring Boot 应用中。 11. 在 Spring Boot 项目中添加对自定义 Starter依赖,可通过 Maven 依赖坐标来引入。 12. 配置项目的 application.properties 或 application.yml 文件,指定数据库相关的信息以及其他自定义属性。 经过以上步骤的操作,就可以自定义 Spring Boot Starter 来操作 MyBatis 数据库了。可以通过引入自定义的 Starter 来简化项目的配置,并在应用中直接使用 MyBatis 进行数据库操作,提高开发效率和代码的可维护性。 ### 回答3: 自定义Spring Boot Starter操作Mybatis数据库涉及以下步骤: 1. 创建一个新的Maven项目,并在pom.xml文件中添加Spring Boot和Mybatis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> ``` 2. 创建一个自定义的starter模块,在其中定义配置类和Mybatis相关配置: ```java @Configuration @AutoConfigureAfter(DataSourceAutoConfiguration.class) @EnableConfigurationProperties(MybatisProperties.class) public class MybatisAutoConfiguration { @Autowired private MybatisProperties properties; @Autowired(required = false) private List<Interceptor> interceptors; @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setConfiguration(properties.getConfiguration()); if (interceptors != null) { factory.setPlugins(interceptors.toArray(new Interceptor[0])); } return factory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3. 创建自定义的配置类MybatisProperties,用于定义数据库的相关配置: ```java @ConfigurationProperties(prefix = "spring.mybatis") public class MybatisProperties { private Configuration configuration; // 其他配置... // Getter和Setter方法 } ``` 4. 在resources目录下创建配置文件application.yml,配置数据库相关信息: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: username password: password spring: mybatis: configuration: map-underscore-to-camel-case: true cache-enabled: true ``` 5. 在使用的项目中添加自定义的starter依赖,并在application类上添加@EnableMybatis注解: ```java @SpringBootApplication @EnableMybatis public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上步骤,我们就可以自定义Spring Boot Starter来操作Mybatis数据库了。这样做的好处是,可以将Mybatis的配置和操作封装在starter中,使得项目更加简洁,并且能够方便地重用该starter
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值