63、自定义Springboot Starter 在项目中使用
1.1、创建自定义的starter项目手先添加相关依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</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>
</dependency>
</dependencies>
1.2、定义和application.yml 配置文件绑定数据的配置类
@Data @ConfigurationProperties(prefix = "hello") public class HelloProperties { private String name; private String address; }
1.3、定义自动配置的类HelloService,将自动把这个类自动配置
public class HelloService { private String name; private String address; public HelloService(String name, String address) { this.name = name; this.address = address; } public String sayHello(){ return "你好!我的名字叫 " + name + ",我来自 " + address; } }
1.4、标注开启自动配置 将helloProperties与配置文件绑定,然后注入给HelloService 完成自动配置
@Configuration @EnableConfigurationProperties(HelloProperties.class) public class HelloServiceAutoConfiguration { private HelloProperties helloProperties; //通过构造方法注入配置属性对象HelloProperties public HelloServiceAutoConfiguration(HelloProperties helloProperties) { this.helloProperties = helloProperties; } //实例化HelloService并载入Spring IoC容器 @Bean @ConditionalOnMissingBean public HelloService helloService(){ return new HelloService(helloProperties.getName(),helloProperties.getAddress()); } }
1.5、META-INF/spring.factories 创建该文件,目的是让spring启动时找到这个自动配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wcs.config.HelloServiceAutoConfiguration
2.1 使用刚才的starts 引入依赖
<dependency>
<groupId>org.wcs</groupId>
<artifactId>hello-springboot-starter</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
2.2、编写配置类
hello:
name: edwin
address: dasdsadsa
2.3、测试是否会将编写的配置类完成自动配置
@RestController @RequestMapping("/hello") public class HelloController { //HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入 @Autowired private HelloService helloService; @GetMapping("/say") public String sayHello(){ return helloService.sayHello(); } }
3.1、在前面的案例一中我们通过定义starter,自动配置了一个HelloService实例。本案例我们需要通过自动配置来创建一个拦截器对象,通过此拦截器对象来实现记录日志功能。
第一步:在hello-spring-boot-starter的pom.xml文件中追加如下maven坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
第二步:自定义MyLog注解
package cn.itcast.log; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface MyLog { /** * 方法描述 */ String desc() default ""; }
第三步:自定义日志拦截器MyLogInterceptor
package cn.itcast.log; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; /** * 日志拦截器 */ public class MyLogInterceptor extends HandlerInterceptorAdapter { private static final ThreadLocal<Long> startTimeThreadLocal = new ThreadLocal<>(); public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { HandlerMethod handlerMethod = (HandlerMethod)handler; Method method = handlerMethod.getMethod();//获得被拦截的方法对象 MyLog myLog = method.getAnnotation(MyLog.class);//获得方法上的注解 if(myLog != null){ //方法上加了MyLog注解,需要进行日志记录 long startTime = System.currentTimeMillis(); startTimeThreadLocal.set(startTime); } return true; } public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { HandlerMethod handlerMethod = (HandlerMethod)handler; Method method = handlerMethod.getMethod();//获得被拦截的方法对象 MyLog myLog = method.getAnnotation(MyLog.class);//获得方法上的注解 if(myLog != null){ //方法上加了MyLog注解,需要进行日志记录 long endTime = System.currentTimeMillis(); Long startTime = startTimeThreadLocal.get(); long optTime = endTime - startTime; String requestUri = request.getRequestURI(); String methodName = method.getDeclaringClass().getName() + "." + method.getName(); String methodDesc = myLog.desc(); System.out.println("请求uri:" + requestUri); System.out.println("请求方法名:" + methodName); System.out.println("方法描述:" + methodDesc); System.out.println("方法执行时间:" + optTime + "ms"); } } }
第四步:创建自动配置类MyLogAutoConfiguration,用于自动配置拦截器、参数解析器等web组件
package cn.itcast.config; import cn.itcast.log.MyLogInterceptor; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** * 配置类,用于自动配置拦截器、参数解析器等web组件 */ @Configuration public class MyLogAutoConfiguration implements WebMvcConfigurer{ //注册自定义日志拦截器 public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new MyLogInterceptor()); } }
第五步:在spring.factories中追加MyLogAutoConfiguration配置
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
cn.itcast.config.HelloServiceAutoConfiguration,\
cn.itcast.config.MyLogAutoConfiguration
测试
@RestController @RequestMapping("/hello") public class HelloController { //HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入 @Autowired private HelloService helloService; @MyLog(desc = "sayHello方法") //日志记录注解 @GetMapping("/say") public String sayHello(){ return helloService.sayHello(); } }
在myapp工程的Controller方法上加入@MyLog注解
@RestController @RequestMapping("/hello") public class HelloController { //HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入 @Autowired private HelloService helloService; @MyLog(desc = "我的日志然机器") @GetMapping("/say") public String sayHello(){ return helloService.sayHello(); } }

浙公网安备 33010602011771号