为了账号安全,请及时绑定邮箱和手机立即绑定

SpringBoot自定义Starter

标签:
SpringBoot

一、创建stater项目

  1. 创建一个maven项目并在pom中引入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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>study.spring.boot</groupId>
    <artifactId>file-starter</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>file-starter</name>
    <description></description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <version>1.5.14.RELEASE</version>
        </dependency>
    </dependencies>

</project>
  1. 编写测试类

    
    public interface FileService {
    
    String upload(File file);
    
    File download(String filePath);

}

public class FileServiceImpl implements FileService {

@Override
public String upload(File file) {
    System.out.println("上传文件...");
    return null;
}

@Override
public File download(String filePath) {
    System.out.println("下载文件..."+filePath);
    return null;
}

}

 1. 创建配置类

@Configuration
public class FileAutoConfiguration {

@Bean
public FileService getFileService(){
    return new FileServiceImpl();
}

}

 2. 添加配置文件
 在resource目录下创建META-INF文件夹并在META-INF下创建spring.factories文件。在文件中加入以下配置

org.springframework.boot.autoconfigure.EnableAutoConfiguration=study.spring.boot.filestarter.configuration.FileAutoConfiguration

### 二、使用starter

 1. 创建一个SpringBoot项目,引入starter依赖

<?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>study.spring.boot</groupId>
<artifactId>starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>starter</name>
<description></description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.14.RELEASE</version>
    <relativePath/>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>study.spring.boot</groupId>
        <artifactId>file-starter</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

</project>

2. 测试

@RunWith(SpringRunner.class)
@SpringBootTest
public class StarterApplicationTests {

@Autowired
private FileService fileService;

@Test
public void starterTest() {
    fileService.download("/xxx/xxx/xx/tokyo-hot.avi");
}

}


测试结果:
![测试结果](https://img-blog.csdn.net/2018061514210138?watermark/2/text/aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3NvdWxfY29kZQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70)
点击查看更多内容
1人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消