自定义maven仓库依赖

这里模拟一个自动配置的自定义
在这里插入图片描述

  • 新建空项目,随后在空项目中新建2个springboot模块
    在这里插入图片描述
  • flyyant-hello-spring-boot-starter中引入flyyant-hello-spring-boot-starter-configure的依赖
		<dependency>
            <groupId>com.flyyant</groupId>
            <artifactId>flyyant-hello-spring-boot-starter-configure</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
  • 清空flyyant-hello-spring-boot-starter项目内无用文件
  • 在这里插入图片描述
  • 清空flyyant-hello-spring-boot-starter-configure项目内无用依赖
    在这里插入图片描述
  • flyyant-hello-spring-boot-starter-configure项目中新建如下文件
    在这里插入图片描述
  • HelloServiceAutoConfiguration.java
@Configuration
@EnableConfigurationProperties(HelloProperties.class) //默认 HelloProperties放在容器中
public class HelloServiceAutoConfiguration {

    @ConditionalOnMissingBean(HelloService.class)
    @Bean
    public HelloService helloService(){
        HelloService helloService = new HelloService();
        return helloService;
    }
}
  • HelloProperties.java
@ConfigurationProperties("com.flyyant")
public class HelloProperties {

    private String prefix;

    private String suffix;

    public String getPrefix() {
        return prefix;
    }

    public String getSuffix() {
        return suffix;
    }

    public void setPrefix(String prefix) {
        this.prefix = prefix;
    }

    public void setSuffix(String suffix) {
        this.suffix = suffix;
    }
}
  • HelloService.java
/**
 * 默认不放在容器中
 */
public class HelloService {

    @Autowired
    HelloProperties helloProperties;

    public String sayHello(String name) {
        return helloProperties.getPrefix() + ": " + name + ">>" + helloProperties.getSuffix();
    }
}
  • spring.factories
# Auto Configure
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.flyyant.hello.auto.HelloServiceAutoConfiguration

spring.factories的内容为什么这么写可以关注我看我粉丝文章中的springboot专栏

  • 将项目打包安装到maven仓库
    在这里插入图片描述
  • 新建springboot项目testcustomer,引入依赖
 		<dependency>
            <groupId>com.flyyant</groupId>
            <artifactId>flyyant-hello-spring-boot-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
  • application.properties中配置内容
com.flyyant.prefix=flyyant
com.flyyant.suffix=6666

这样配的原因是之前我们写的HelloProperties.java文件中我门定义了@ConfigurationProperties(“com.flyyant”)

  • 新建controller文件
    在这里插入图片描述
  • HelloController.java
@RestController
public class HelloController {

    @Autowired
    HelloService helloService;

    @GetMapping("/hello")
    public String hello(){
        String s = helloService.sayHello("张三");
        return s;
    }
}
  • 运行,访问http://localhost:8080/hello
    在这里插入图片描述
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值