继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

SpringBoot自定义Starter

Coley_5
关注TA
已关注
手记 86
粉丝 8548
获赞 6550

一、创建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)
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP