<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip> <!-- 这样没有main主程序类也可以打包 -->
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
删除RobotSpringBootStarterApplication主程序类,删除src/test目录
RobotProperties.java:绑定application.properties中robot开头的配置到属性name和age
package com.hh.robot.properties;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "robot")
@Component
@Data
public class RobotProperties {
private String name;
private String age;
}
package com.hh.robot.service;
import com.hh.robot.properties.RobotProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class RobotService {
@Autowired
RobotProperties robotProperties;
public String sayHello() {
return "你好:" +
"名字:【" + robotProperties.getName() + "】, " +
"年龄:【" + robotProperties.getAge() + "】";
}
}
package com.hh.robot.controller;
import com.hh.robot.service.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class RobotController {
@Autowired
RobotService robotService;
@GetMapping("/robot")
public String sayHello() {
String hello = robotService.sayHello();
return hello;
}
}
实现RobotAutoConfiguration配置类,给IOC容器导入Robot功能要用的所有组件。如下所示
package com.hh.robot.config;
import com.hh.robot.controller.RobotController;
import com.hh.robot.properties.RobotProperties;
import com.hh.robot.service.RobotService;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@Import({RobotController.class, RobotProperties.class, RobotService.class})
@Configuration
public class RobotAutoConfiguration {
}
参照添加方式一实现RobotAutoConfiguration配置类。再实现@EnableRobot功能开关,添加RobotAutoConfiguration配置类。如下所示:
package com.hh.robot.annotation;
import com.hh.robot.config.RobotAutoConfiguration;
import org.springframework.context.annotation.Import;
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Import(RobotAutoConfiguration.class)
public @interface EnableRobot {
}
参照添加方式一实现RobotAutoConfiguration配置类。再在src\main\resources\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports文件中,添加RobotAutoConfiguration配置类的全类名。文件内容如下:
com.hh.robot.config.RobotAutoConfiguration
使用maven命令mvn clean install
进行打包到本地仓库
<dependency>
<groupId>com.hh</groupId>
<artifactId>robot-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
application.properties添加配置参数如下:
robot.name=jim
robot.age=18
使用@Import注解,将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器。如下所示:
......省略部分......
@SpringBootApplication
@Import({RobotAutoConfiguration.class})
public class SpringBootTestApplication {
......省略部分......
}
使用@EnableRobot注解,自动将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器。如下所示:
......省略部分......
@SpringBootApplication
@EnableRobot
public class SpringBootTestApplication {
......省略部分......
}
基本原理:springboot启动的时候,会自动从robot-spring-boot-starter-0.0.1-SNAPSHOT.jar的src\main\resources\META-INF\spring\org.springframework.boot.autoconfigure.AutoConfiguration.imports,找到RobotAutoConfiguration配置类,将RobotAutoConfiguration注入到IOC容器,就会自动将robot项目的所有组件注册到IOC容器
访问http://localhost:8080/robot,页面效果如下: