手写一个简单的SpringBoot Starter

手写一个简单的SpringBoot Starter

首先通过maven建立一个空项目;
创建一个maven项目
建立好一个空的项目以后进行一个pom.xml 文件的导入

   <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <!--  防止传递依赖-->
            <optional>true</optional>
        </dependency>
    </dependencies>

然后进行一个文件的建立
项目目录结构
目录结构如上图;
首先我们先建立对应的文件然后对TestConfiguration进行编写

//标记为配置类
@Configuration
//启动配置属性
@EnableConfigurationProperties(TestProperties.class)
@ConditionalOnClass(TestService.class)
@ConditionalOnProperty(prefix = "test", value = "enabled", matchIfMissing = true)
public class TestConfiguration {
    @Autowired
    TestProperties testProperties;

    @Bean
    public  TestService TestService(){
        TestService service = new TestService();
        service.setName(testProperties.getName());
        return service;
    }

}

然后对TestProperties文件进行编写

@ConfigurationProperties(prefix = "test")
public class TestProperties {
    private static final String DEFAULT_NAME = "lht";

    private String name = DEFAULT_NAME;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

然后对TestService文件进行编写

public class TestService {

    private String name ;
    public String hello(){
        return "starter-name:"+getName();
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

上面写好以后,在 resources 文件夹下创建 META-INF/spring.factories 文件,通过上面的代码,Spring Boot 在启动的时候就是读取该文件下的配置类,从而将 Bean 加载到容器中。

org.springframework.boot.autoconfigure.EnableAutoConfiguration= \
  com.lht.TestConfiguration

然后通过mvn install 进行一个打包。

在新建maven工程
将自定义的starter进行导入`

   <dependency>
            <groupId>org.example</groupId>
            <artifactId>test-starter</artifactId>
            <version>1.0-SNAPSHOT</version>
   </dependency>

然后建立一个SpringBoot的启动类

@SpringBootApplication
@RestController
public class StarterTestApplicationTest{

    @Autowired
    TestService testService;

    @GetMapping("/a")
    public String index(){
        System.out.println(testService.getName());
        return testService.getName();
    }

    public static void main(String[] args) {
        SpringApplication.run(StarterTestApplicationTest.class, args);
    }
}

在application.yml中进行属性的配置

test:
  name: 111111

然后启动项目 对这个路径进行访问:
启动项目运行后显示的结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
当然可以!以下是一个简单的示例,展示了如何手写一个Spring Boot Starter: 首先,创建一个 Maven 项目,并添加以下依赖项: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <version>2.5.4</version> </dependency> </dependencies> ``` 接下来,创建一个自定义的自动配置类,用于配置你的 Starter: ```java @Configuration @EnableConfigurationProperties(MyStarterProperties.class) public class MyStarterAutoConfiguration { private final MyStarterProperties properties; public MyStarterAutoConfiguration(MyStarterProperties properties) { this.properties = properties; } // 在此处定义你的自动配置逻辑 @Bean public MyStarterService myStarterService() { return new MyStarterService(properties); } } ``` 然后,创建一个属性类,用于将外部配置绑定到属性上: ```java @ConfigurationProperties("my.starter") public class MyStarterProperties { private String message; // 提供 getter 和 setter } ``` 最后,创建一个自定义的服务类,该服务类将在你的 Starter 中使用: ```java public class MyStarterService { private final MyStarterProperties properties; public MyStarterService(MyStarterProperties properties) { this.properties = properties; } public void showMessage() { System.out.println(properties.getMessage()); } } ``` 现在,你的 Spring Boot Starter 已经准备就绪了!你可以将其打包并使用在其他 Spring Boot 项目中。在其他项目的 `pom.xml` 文件中,添加你的 Starter 依赖: ```xml <dependencies> <dependency> <groupId>com.example</groupId> <artifactId>my-starter</artifactId> <version>1.0.0</version> </dependency> </dependencies> ``` 然后,在你的应用程序中使用 `MyStarterService`: ```java @SpringBootApplication public class MyApplication implements CommandLineRunner { private final MyStarterService myStarterService; public MyApplication(MyStarterService myStarterService) { this.myStarterService = myStarterService; } public static void main(String[] args) { SpringApplication.run(MyApplication.class, args); } @Override public void run(String... args) throws Exception { myStarterService.showMessage(); } } ``` 这样,你就成功地创建了一个简单Spring Boot Starter!当其他项目引入你的 Starter 并运行时,将会输出预定义的消息。 当然,这只是一个简单的示例,真实的 Starter 可能包含更多的配置和功能。你可以根据自己的需求进行扩展和定制。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值