示例一
依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
</dependencies>
业务实现
//HelloService.java
public class HelloService {
private String name;
private String addr;
public HelloService(String name,String addr){
this.name = name;
this.addr = addr;
}
public String sayHello(){
return String.format("我叫%s,我来自%s", this.name,this.addr);
}
}
// helloProperties
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@Data
@ConfigurationProperties(prefix = "hello")
public class HelloPorperties {
private String name;
private String addr;
}
自动配置实现
// 自动配置类
import com.snail.hello.starter.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(HelloPorperties.class) //自动注入HelloPorperties
public class HelloServeicAutoConfiguration {
private HelloPorperties helloPorperties;
public HelloServeicAutoConfiguration(HelloPorperties helloPorperties){
this.helloPorperties = helloPorperties;
}
// 使用HelloPorperties的配置属性创建出HelloService对象并加入到spring容器中。
@ConditionalOnMissingBean
@Bean
public HelloService helloService(){
return new HelloService(this.helloPorperties.getName(),this.helloPorperties.getAddr());
}
}
#在resource目录中创建 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.snail.hello.starter.config.HelloServeicAutoConfiguration
使用
-
创建springboot工程,并引入此starter
-
在配置文件中配置如下内容
hello: addr: jiangxi name: luolf
-
在需要使用HelloService的地方直接注入即可使用,如下:
import com.snail.hello.starter.service.HelloService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController public class HelloControl { @Resource private HelloService helloService; @RequestMapping("/sayHello") public String sayHell(){ return helloService.sayHello(); } } // 访问sayHello 即可调用到helloService.sayHello()方法。
示例二
依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</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>
</dependency>
</dependencies>
业务实现
// MyLog 注解
package com.snail.hello.starter.annotation;
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 "";
}
// 日志拦截器,继承HandlerInterceptorAdapter
public class MyLogInterceptor extends HandlerInterceptorAdapter {
private static final ThreadLocal<Long> startTimeThreadLocal = new ThreadLocal<>();
// 方法执行前拦截,记录开始时间.
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (isInterceptMethod(handler)) {
startTimeThreadLocal.set(System.currentTimeMillis());
}
return true;
}
private boolean isInterceptMethod(Object handler) {
if(!(handler instanceof HandlerMethod)){
return false;
}
return ((HandlerMethod) handler).getMethod().getAnnotation(MyLog.class) != null;
}
// 方法执行后拦截,记录记录时间,并打印日志信息.
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (isInterceptMethod(handler)) {
Method method = ((HandlerMethod) handler).getMethod();
MyLog myLog = method.getAnnotation(MyLog.class);
System.out.println(String.format("请求URL:%s,请求方法:%s,方法描述:%s,方法执行时间:%s"
, request.getRequestURI()
, method.getDeclaringClass().getName() + "." + method.getName()
, myLog.desc()
, (System.currentTimeMillis() - startTimeThreadLocal.get()) + "ms"));
}
}
}
自动配置实现
@Configuration
public class MyLogAutoConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new MyLogInterceptor());
}
}
#在resource目录中创建 META-INF/spring.factories
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.snail.hello.starter.config.MyLogAutoConfiguration
使用
@RestController
public class HelloControl {
@Resource
private HelloService helloService;
@RequestMapping("/sayHello")
@MyLog(desc = "测试方法") // 增加日志注解.
public String sayHell(){
return helloService.sayHello();
}
}
// 执行方法后会打印出日志:请求URL:/sayHello,请求方法:com.snail.control.HelloControl.sayHell,方法描述:测试方法,方法执行时间:32ms