第一步:创建starter工程hello-spring-boot-starter并配置pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.peppa hello-spring-boot-starter 1.0 org.springframework.boot spring-boot-starter-web true org.springframework.boot spring-boot-autoconfigure
第二步:创建配置属性类HelloProperties
package com.peppa.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "hello")
public class HelloProperties {
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "HelloProperties{" +
"name='" + name + ''' +
", address='" + address + ''' +
'}';
}
}
第三步:创建服务类HelloService
package com.peppa.service;
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;
}
}
第四步:创建自动配置类HelloServiceAutoConfiguration
package com.peppa.config;
import com.peppa.service.HelloService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@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());
}
}
第五步:创建Log 相关类
MyLog注解
package com.peppa.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 com.peppa.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 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
package com.peppa.config;
import com.peppa.log.MyLogInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class MyLogAutoConfiguration implements WebMvcConfigurer{
//注册自定义日志拦截器
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyLogInterceptor()).addPathPatterns("*.html","*.css","*.js");
}
}
最后一步:在resources目录下创建meta-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration= com.peppa.config.HelloServiceAutoConfiguration, com.peppa.config.MyLogAutoConfiguration
至此starter已经开发完成了,可以将当前starter安装到本地maven仓库供其他应用来使用。
使用自定义starter第一步:创建maven工程myapp并配置pom.xml文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.2.2.RELEASE com.peppa myapp 1.0 org.springframework.boot spring-boot-starter-web com.peppa hello-spring-boot-starter 1.0
第二步:创建application.yml文件
server: port: 8080 hello: name: peppa address: Wuhan
第三步:创建HelloController
package com.peppa.controller;
import com.peppa.log.MyLog;
import com.peppa.service.HelloService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class HelloController {
//HelloService在我们自定义的starter中已经完成了自动配置,所以此处可以直接注入
@Autowired
private HelloService helloService;
@GetMapping("/say")
@MyLog(desc = "sayHello方法") //日志记录注解
public String sayHello(){
return helloService.sayHello();
}
}
第四步:创建启动类HelloApplication
package com.peppa;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class);
}
}
执行启动类main方法,访问地址http://localhost:8080/hello/say



