init commit

This commit is contained in:
2021-11-14 00:34:04 +08:00
parent 66207f84e8
commit b1577a60e1
9 changed files with 227 additions and 52 deletions

View File

@@ -0,0 +1,28 @@
package me.hatter.sample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = "me.hatter.sample")
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
// @Bean
// public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
// return args -> {
//
// System.out.println("Let's inspect the beans provided by Spring Boot:");
//
// String[] beanNames = ctx.getBeanDefinitionNames();
// Arrays.sort(beanNames);
// for (String beanName : beanNames) {
// System.out.println(beanName);
// }
//
// };
// }
}

View File

@@ -0,0 +1,21 @@
package me.hatter.sample.common;
import me.hatter.sample.common.resolver.SystemTimeArgumentResolver;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.*;
import java.util.List;
@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
@Autowired
SystemTimeArgumentResolver systemTimeArgumentResolver;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(systemTimeArgumentResolver);
}
}

View File

@@ -0,0 +1,9 @@
package me.hatter.sample.common.annotation;
import java.lang.annotation.*;
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemTime {
}

View File

@@ -0,0 +1,24 @@
package me.hatter.sample.common.resolver;
import me.hatter.sample.common.annotation.SystemTime;
import org.springframework.core.MethodParameter;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import java.util.Date;
@Component
public class SystemTimeArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(SystemTime.class);
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
return new Date();
}
}

View File

@@ -0,0 +1,17 @@
package me.hatter.sample.web.controller;
import me.hatter.sample.common.annotation.SystemTime;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
@RestController
public class SampleController {
@GetMapping("/")
public String home(
@SystemTime Date time) {
return "Hello, World: " + time;
}
}