spring13-AspectJ

1. AspectJ

1.1. AspectJ介绍

AspectJ官网^4

AspectJ文档^1

1.1.1. pointcuts

pointcuts指的是程序中的某些链接点(某些时机),例如call(void Point.setX(int))表示:调用类PointsetX(int)方法时

pointcuts可以使用与或非表达式(||,&&,!)连接,比如 call(void Point.setX(int)) || call(void Point.setY(int))

pointcuts可以被定义为变量,如下面代码中的move()

1
2
3
4
5
6
pointcut move():
call(void FigureElement.setXY(int,int)) ||
call(void Point.setX(int)) ||
call(void Point.setY(int)) ||
call(void Line.setP1(Point)) ||
call(void Line.setP2(Point));

当然pointcuts定义的时候还可以使用通配符,比如call(void Figure.make*(..))代表Figure的以make开头且返回值为void的方法(不关心参数)调用的时候。比如call(public * Figure.* (..))代表Figure的任何方法(不关心方法名,参数,返回值)调用的时候。

阅读更多

Springfox使用

1. 对List不能很好的支持

1.1. 核心代码

代码中写的是数组

1
2
3
4
5
/**
* 用户
*/
@ApiModelProperty(value = "用户",example = "hellowang")
private List<String> user;

1.2. 问题详情

在swagger页面展示的例子是字符串

1
2
3
{
"user": "hellowang"
}

1.3. 问题讨论

Stackoverflow

Github

1.4. 解决方案

springfox-collection-example-plugin

Java异常原因及处理

Java常见异常

1
2
3
4
5
6
7
8
9
10
11
classDiagram
Object <|-- Throwable
Throwable <|-- Error
Throwable <|-- Exception
Error <|-- OutOfMemoryError
Error <|-- NoClassDefFoundError
Error <|-- StackOverflowError
Exception <|-- IOException
Exception <|-- RuntimeException
RuntimeException <|-- NullPointerException
RuntimeException <|-- IndexOutOfBoundsException

NoClassDefFoundError 异常原因处理

阅读更多

springcloud-gateway

Spring Cloud Gateway

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package com.example.demo;

import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.cloud.gateway.route.RouteDefinitionRepository;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

// 动态路由
// https://zhuanlan.zhihu.com/p/125018436
@RestController
@SpringBootApplication
public class DemoApplication implements RouteDefinitionRepository, ApplicationEventPublisherAware {

public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}

// event publisher
ApplicationEventPublisher applicationEventPublisher;

@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.applicationEventPublisher = applicationEventPublisher;
}


// router
List<RouteDefinition> memery = new ArrayList<>();

private void refreshRoute() {
applicationEventPublisher.publishEvent(new RefreshRoutesEvent(this));
}

@PutMapping
Mono<Void> putRoute(@RequestBody Mono<RouteDefinition> o) {
return o.flatMap(routeDefinition -> {
memery.add(routeDefinition);
refreshRoute();
return Mono.empty();
});
}

@PostMapping
Mono<Void> postRoute(@RequestBody Mono<RouteDefinition> o) {
return o.flatMap(routeDefinition -> {
for (int i = 0; i < memery.size(); i++) {
if (memery.get(i).getId().equals(routeDefinition.getId())) {
memery.set(i, routeDefinition);
}
}
refreshRoute();
return Mono.empty();
});
}

@DeleteMapping
Mono<Void> deleteRoute(@RequestBody Mono<String> o) {
return o.flatMap(id -> {
memery.removeIf(routeDefinition -> routeDefinition.getId().equals(id));
refreshRoute();
return Mono.empty();
});
}

@GetMapping
Mono<List<RouteDefinition>> getRoute(){
return Mono.just(memery);
}

@Override
public Flux<RouteDefinition> getRouteDefinitions() {
return Flux.fromIterable(memery);
}

@Override
public Mono<Void> save(Mono<RouteDefinition> route) {
return Mono.empty();
}

@Override
public Mono<Void> delete(Mono<String> routeId) {
return Mono.empty();
}
}
阅读更多

aware

Aware

BeanNameAware

beanNameAware可以获得容器中Bean的名称,作用于每一个Bean。当bean被创建的时候设置他的名字,在基本properties填充完成以后,init调用前执行

摘自: spring-beans:5.3.4 org.springframework.beans.factory.BeanNameAware

Set the name of the bean in the bean factory that created this bean.

Invoked after population of normal bean properties but before an init callback such as {@link InitializingBean#afterPropertiesSet()} or a custom init-method.

1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;

@Component
public class BeanNameAwareDemo implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println(name);
}
}
阅读更多

feign

Feign

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.Logger;
import feign.codec.Decoder;
import feign.codec.Encoder;
import java.util.Arrays;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

@Configuration
public class FeignConfigure {
@Bean
public Decoder feignDecoder() {
HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
return new ResponseEntityDecoder(new SpringDecoder(objectFactory));
}
@Bean
public Encoder feignEncoder(){
HttpMessageConverter jacksonConverter = new MappingJackson2HttpMessageConverter(customObjectMapper());
ObjectFactory<HttpMessageConverters> objectFactory = () -> new HttpMessageConverters(jacksonConverter);
return new SpringEncoder(objectFactory);
}

public ObjectMapper customObjectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
//Customize as much as you want
objectMapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
return objectMapper;
}

@Bean
public Logger.Level logger() {
return Logger.Level.FULL;
}
}