SpringBoot-自定义starter Leonan

1.创建Maven项目,(删除自动生成test文件夹及依赖,不删除亦可)、pom添加自动化配置依赖:

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

2.创建一个LeonanProperties类用来接收application.yml文件中注入的值:

package org.leonan.leonanstarter;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

/**
 * @author Leonan
 * @date 2020/5/19 20:57
 */
@Data
@Component
@ConfigurationProperties(prefix = "leonan")
public class LeonanProperties {

    private String name = "leonan";
    private String age = "26";

}

注:@ConfigurationProperties(prefix = “leonan”)是将application.yml文件中的leonan属性值注入到此类对应的属性中
application.yml如下(注意使用.yml书写规则属性和值之间有一个空格)

leonan:
  name: Forexer
  age: 28

这里采用@Data注解生成get、set方法,只需要在pom文件中引入lombok依赖就可以用了(当然用传统的代码get、set方法亦可):

<dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
</dependency>

有的小伙伴会有如下报错:在这里插入图片描述只需要添加@Component注解就可以解决

3.创建一个FindGirlfriendService类,里边可以定义我们要干的事情,比如定义一个needGirlfriend()方法,(明天520,而我需要一个女朋友

package org.leonan.leonanstarter;

import lombok.Data;

/**
* @author Leonan
* @date 2020/5/19 21:23
*/
@Data
public class FindGirlfriendService{

   private String name;
   private String age;

   public String needGirlfriend(String msg){
       return name + "男" + age + "岁," + msg;
   }
}

4:重点:创建一个自动配置类

package org.leonan.leonanstarter;

import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author Leonan
 * @date 2020/5/19 21:30
 */
@Configuration
@EnableConfigurationProperties(LeonanProperties.class)
@ConditionalOnClass(FindGirlfriendService.class)
public class FindGirlfriendAutoConfiguration {

    @Resource
    LeonanProperties leonanProperties;

    @Bean
    FindGirlfriendService findGirlfriend(){

        FindGirlfriendService findGirlfriendService = new FindGirlfriendService();
        findGirlfriendService.setName(leonanProperties.getName());
        findGirlfriendService.setAge(leonanProperties.getAge());
        return findGirlfriendService;
        
    }
}

注:注解不懂请百度。重点说一下@ConditionalOnClass(FindGirlfriendService.class),classpath目录下有FindGirlfriendService类,配置才会生效

5.最后需要在resources目录创建一个META-INF文件夹,这个都很熟悉,在该文件夹中创建一个spring.factories的文件,写入自动化配置类路径:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.leonan.leonanstarter.FindGirlfriendAutoConfiguration

注:org.leonan.leonanstarter.FindGirlfriendAutoConfiguration是自动化配置类FindGirlfriendAutoConfiguration的路径,别复制我这个,自己改一下。

6:接下来就是把我们的starter打包到本地Maven仓库了,双击idea右侧的Maven→Lifecycle→install等待控制台显示BUILD SUCCESS即安装成功。
测试
(1).创建一个test-starter项目,pom中引入自定义的依赖:

 <dependency>
            <groupId>org.leonan</groupId>
            <artifactId>leonan-starter</artifactId>
</dependency>

注:引入你自己创建的starter依赖,更改自己定义的名字。
(2).可以在application.yml中配置自己的属性值(注意属性和值之间的空格):

leonan:
  name: leonan
  age: 18       #我永远18(虽然28)

测试方法一:然后去test目录下进行单元测试

package org.leonan.teststarter;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.leonan.leonanstarter.FindGirlfriendService;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@RunWith(SpringRunner.class)
@SpringBootTest
class TestStarterApplicationTests {

    @Resource
    FindGirlfriendService findGirlfriendService;

    @Test
    void contextLoads() {
    	String msg = "征婚ing...";
        System.out.println(findGirlfriendService.needGirlfriend(msg));
    }
}

运行后控制台输出:
在这里插入图片描述
成功!!!
注:部分朋友的Test加上@RunWith(SpringRunner.class)注解会报错,只需将pom文件中test依赖的<exclusions></exclusions>及其内容删掉即可

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
</dependency>

测试方法二:写一个TestController类

package org.leonan.teststarter;

import org.leonan.leonanstarter.FindGirlfriendService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author Leonan
 * @date 2020/5/19 22:55
 */
@RestController
public class TestController {

    @Resource
    FindGirlfriendService findGirlfriendService;

    @GetMapping("/{msg}")
    public String testService(@PathVariable(value = "msg") String msg){
        return findGirlfriendService.needGirlfriend(msg);
    }
}

然后运行启动类,打开浏览器地址栏输入:
http://127.0.0.1:8080/征婚ing…
在这里插入图片描述
显示如下内容:
在这里插入图片描述
成功!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值