首页 > 编程语言 > 详细

Spring Boot Starter开发

时间:2020-08-06 15:06:15      阅读:77      评论:0      收藏:0      [点我收藏+]

项目结构:
技术分享图片

pom.xml

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.18</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.2.7.RELEASE</version>
            <optional>true</optional>
        </dependency>

UserProperties.java

@Data
@ConfigurationProperties(prefix = "com.run.user")
public class UserProperties {
    private int id = 1;
    private String name = "ych";
    private String sex = "男";
}

UserService.java

public class UserService {
    private UserProperties userProperties;

    public UserService(UserProperties prop) {
        this.userProperties = prop;
    }

    public void say() {
        System.out.println(userProperties.toString());
    }
}

UserServiceAutoConfiguration.java

@Configuration
@EnableConfigurationProperties(UserProperties.class)
public class UserServiceAutoConfiguration {
    @Autowired
    private UserProperties userProperties;

    @Bean
    @ConditionalOnMissingBean(UserService.class)
    public UserService userService() {
        UserService userService = new UserService(userProperties);
        return userService;
    }
}

spring.factories

org.springframework.boot.autoconfigure.EnableAutoConfiguration=    com.run.UserServiceAutoConfiguration

Spring Boot Starter开发

原文:https://www.cnblogs.com/yinchh/p/13446312.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!