Skip to content

Commit 52295ff

Browse files
authored
Merge pull request doocs#80 from huifer/master
Spring source code
2 parents ba0fc40 + 261a9c2 commit 52295ff

10 files changed

Lines changed: 248 additions & 23 deletions

README.md

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,29 +60,34 @@
6060

6161
### Spring 类解析
6262

63-
- [Spring 自定义标签解析](/docs/Spring/clazz/Spring-Custom-label-resolution.md)
64-
- [Spring Scan 包扫描](/docs/Spring/clazz/Spring-scan.md)
65-
- [Spring 注解工具类](/docs/Spring/clazz/Spring-AnnotationUtils.md)
66-
- [Spring 别名注册](/docs/Spring/clazz/Spring-SimpleAliasRegistry.md)
67-
- [Spring 标签解析类](/docs/Spring/clazz/Spring-BeanDefinitionParserDelegate.md)
68-
- [Spring ApplicationListener](/docs/Spring/clazz/Spring-ApplicationListener.md)
69-
- [Spring messageSource](/docs/Spring/clazz/Spring-MessageSource.md)
70-
- [Spring 自定义属性解析器](/docs/Spring/clazz/Spring-Custom-attribute-resolver.md)
71-
- [Spring 排序工具](/docs/Spring/clazz/Spring-OrderUtils.md)
72-
73-
- [Spring-import 注解](/docs/Spring/clazz/Spring-Import.md)
74-
- [Spring-定时任务](/docs/Spring/clazz/Spring-Scheduling.md)
75-
- [Spring StopWatch](/docs/Spring/clazz/Spring-StopWatch.md)
76-
- [Spring 元数据](/docs/Spring/clazz/Spring-Metadata.md)
77-
- [Spring 条件接口](/docs/Spring/clazz/Spring-Conditional.md)
78-
79-
- [Spring MultiValueMap](/docs/Spring/clazz/Spring-MultiValueMap.md)
80-
- [Spring MethodOverride](/docs/Spring/clazz/Spring-MethodOverride.md)
81-
- [Spring BeanDefinitionReaderUtils](/docs/Spring/clazz/Spring-BeanDefinitionReaderUtils.md)
82-
- [Spring PropertyPlaceholderHelper](/docs/Spring/clazz/Spring-PropertyPlaceholderHelper.md)
83-
84-
- [Spring PropertySource](/docs/Spring/clazz/PropertySource)
85-
- [Spring PlaceholderResolver](/docs/Spring/clazz/PlaceholderResolver)
63+
* [Spring 自定义标签解析](/docs/Spring/clazz/Spring-Custom-label-resolution.md)
64+
* [Spring Scan 包扫描](/docs/Spring/clazz/Spring-scan.md)
65+
* [Spring 注解工具类](/docs/Spring/clazz/Spring-AnnotationUtils.md)
66+
* [Spring 别名注册](/docs/Spring/clazz/Spring-SimpleAliasRegistry.md)
67+
* [Spring 标签解析类](/docs/Spring/clazz/Spring-BeanDefinitionParserDelegate.md)
68+
* [Spring ApplicationListener](/docs/Spring/clazz/Spring-ApplicationListener.md)
69+
* [Spring messageSource](/docs/Spring/clazz/Spring-MessageSource.md)
70+
* [Spring 自定义属性解析器](/docs/Spring/clazz/Spring-Custom-attribute-resolver.md)
71+
* [Spring 排序工具](/docs/Spring/clazz/Spring-OrderUtils.md)
72+
73+
* [Spring-import注解](/docs/Spring/clazz/Spring-Import.md)
74+
* [Spring-定时任务](/docs/Spring/clazz/Spring-Scheduling.md)
75+
* [Spring StopWatch](/docs/Spring/clazz/Spring-StopWatch.md)
76+
* [Spring 元数据](/docs/Spring/clazz/Spring-Metadata.md)
77+
* [Spring 条件接口](/docs/Spring/clazz/Spring-Conditional.md)
78+
79+
* [Spring MultiValueMap](/docs/Spring/clazz/Spring-MultiValueMap.md)
80+
* [Spring MethodOverride](/docs/Spring/clazz/Spring-MethodOverride.md)
81+
* [Spring BeanDefinitionReaderUtils](/docs/Spring/clazz/Spring-BeanDefinitionReaderUtils.md)
82+
* [Spring PropertyPlaceholderHelper](/docs/Spring/clazz/Spring-PropertyPlaceholderHelper.md)
83+
84+
* [Spring PropertySource](/docs/Spring/clazz/PropertySource)
85+
* [Spring PlaceholderResolver](/docs/Spring/clazz/PlaceholderResolver)
86+
87+
* [Spring-AnnotationFormatterFactory](/docs/Spring/clazz/format/Spring-AnnotationFormatterFactory.md)
88+
* [Spring-Formatter](/docs/Spring/clazz/format/Spring-Formatter.md)
89+
* [Spring-Parser](/docs/Spring/clazz/format/Spring-Parser.md)
90+
* [Spring-Printer](/docs/Spring/clazz/format/Spring-Printer.md)
8691

8792
### Spring5 新特性
8893

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# Spring DateTimeFormatAnnotationFormatterFactory
2+
3+
- 类全路径: `org.springframework.format.datetime.DateTimeFormatAnnotationFormatterFactory`
4+
5+
- 类图
6+
![EmbeddedValueResolutionSupport](/images/spring/DateTimeFormatAnnotationFormatterFactory.png)
7+
8+
9+
```java
10+
public class DateTimeFormatAnnotationFormatterFactory extends EmbeddedValueResolutionSupport
11+
implements AnnotationFormatterFactory<DateTimeFormat> {
12+
13+
/**
14+
* 字段类型
15+
*/
16+
private static final Set<Class<?>> FIELD_TYPES;
17+
18+
@Override
19+
public Set<Class<?>> getFieldTypes() {
20+
return FIELD_TYPES;
21+
}
22+
23+
@Override
24+
public Printer<?> getPrinter(DateTimeFormat annotation, Class<?> fieldType) {
25+
return getFormatter(annotation, fieldType);
26+
}
27+
28+
@Override
29+
public Parser<?> getParser(DateTimeFormat annotation, Class<?> fieldType) {
30+
return getFormatter(annotation, fieldType);
31+
}
32+
33+
protected Formatter<Date> getFormatter(DateTimeFormat annotation, Class<?> fieldType) {
34+
DateFormatter formatter = new DateFormatter();
35+
// style
36+
String style = resolveEmbeddedValue(annotation.style());
37+
// 判断时间格式是否村子啊
38+
if (StringUtils.hasLength(style)) {
39+
formatter.setStylePattern(style);
40+
}
41+
// iso 设置
42+
formatter.setIso(annotation.iso());
43+
// date time pattern
44+
String pattern = resolveEmbeddedValue(annotation.pattern());
45+
// 设置
46+
if (StringUtils.hasLength(pattern)) {
47+
formatter.setPattern(pattern);
48+
}
49+
return formatter;
50+
}
51+
52+
static {
53+
Set<Class<?>> fieldTypes = new HashSet<>(4);
54+
// 加入字段类型
55+
fieldTypes.add(Date.class);
56+
fieldTypes.add(Calendar.class);
57+
fieldTypes.add(Long.class);
58+
FIELD_TYPES = Collections.unmodifiableSet(fieldTypes);
59+
}
60+
61+
}
62+
63+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Spring DateTimeParser
2+
- 类全路径: `org.springframework.format.datetime.joda.DateTimeParser`
3+
4+
- 代码如下
5+
6+
```java
7+
public final class DateTimeParser implements Parser<DateTime> {
8+
9+
private final DateTimeFormatter formatter;
10+
11+
12+
/**
13+
* Create a new DateTimeParser.
14+
* @param formatter the Joda DateTimeFormatter instance
15+
*/
16+
public DateTimeParser(DateTimeFormatter formatter) {
17+
this.formatter = formatter;
18+
}
19+
20+
21+
@Override
22+
public DateTime parse(String text, Locale locale) throws ParseException {
23+
// DateTimeFormatter 转换字符串事件类型
24+
return JodaTimeContextHolder.getFormatter(this.formatter, locale).parseDateTime(text);
25+
}
26+
27+
}
28+
29+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Spring MillisecondInstantPrinter
2+
- 类全路径: `org.springframework.format.datetime.joda.MillisecondInstantPrinter`
3+
4+
5+
```java
6+
public final class MillisecondInstantPrinter implements Printer<Long> {
7+
8+
private final DateTimeFormatter formatter;
9+
10+
11+
/**
12+
* Create a new ReadableInstantPrinter.
13+
* @param formatter the Joda DateTimeFormatter instance
14+
*/
15+
public MillisecondInstantPrinter(DateTimeFormatter formatter) {
16+
this.formatter = formatter;
17+
}
18+
19+
20+
@Override
21+
public String print(Long instant, Locale locale) {
22+
// DateTimeFormatter .print
23+
return JodaTimeContextHolder.getFormatter(this.formatter, locale).print(instant);
24+
}
25+
26+
}
27+
28+
```
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Spring AnnotationFormatterFactory
2+
3+
- 类全路径: `org.springframework.format.AnnotationFormatterFactory`
4+
5+
6+
```java
7+
8+
public interface AnnotationFormatterFactory<A extends Annotation> {
9+
10+
/**
11+
* The types of fields that may be annotated with the &lt;A&gt; annotation.
12+
* 字段类型
13+
*/
14+
Set<Class<?>> getFieldTypes();
15+
16+
/**
17+
* Get the Printer to print the value of a field of {@code fieldType} annotated with
18+
* {@code annotation}.
19+
* <p>If the type T the printer accepts is not assignable to {@code fieldType}, a
20+
* coercion from {@code fieldType} to T will be attempted before the Printer is invoked.
21+
* 通过注解和字段类型获取输出接口
22+
* @param annotation the annotation instance
23+
* @param fieldType the type of field that was annotated
24+
* @return the printer
25+
*/
26+
Printer<?> getPrinter(A annotation, Class<?> fieldType);
27+
28+
/**
29+
* Get the Parser to parse a submitted value for a field of {@code fieldType}
30+
* annotated with {@code annotation}.
31+
* <p>If the object the parser returns is not assignable to {@code fieldType},
32+
* a coercion to {@code fieldType} will be attempted before the field is set.
33+
* 通过注解和字段类型获取解析接口
34+
* @param annotation the annotation instance
35+
* @param fieldType the type of field that was annotated
36+
* @return the parser
37+
*/
38+
Parser<?> getParser(A annotation, Class<?> fieldType);
39+
40+
}
41+
42+
```
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Spring Formatter
2+
- 类全路径: `org.springframework.format.Formatter`
3+
4+
```java
5+
public interface Formatter<T> extends Printer<T>, Parser<T> {
6+
7+
}
8+
```
9+
10+
- 该接口继承了 printer 和 parser 两个接口.
11+
- 比较常见的有: `DateFormatter` 就是继承这个接口.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Spring Parser
2+
3+
- 类全路径: `org.springframework.format.Parser`
4+
- 类作用: 字符串准换成java对象
5+
6+
```java
7+
8+
@FunctionalInterface
9+
public interface Parser<T> {
10+
11+
/**
12+
* Parse a text String to produce a T.
13+
* 将字符串转换成对象
14+
* @param text the text string
15+
* @param locale the current user locale
16+
* @return an instance of T
17+
* @throws ParseException when a parse exception occurs in a java.text parsing library
18+
* @throws IllegalArgumentException when a parse exception occurs
19+
*/
20+
T parse(String text, Locale locale) throws ParseException;
21+
22+
}
23+
```
24+
25+
- 类图
26+
27+
![Parser](/images/spring/Parser.png)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Spring Printer
2+
- 类全路径: `org.springframework.format.Printer`
3+
- 类作用: 对象转换成字符串
4+
5+
6+
```java
7+
@FunctionalInterface
8+
public interface Printer<T> {
9+
10+
/**
11+
* Print the object of type T for display.
12+
* 打印对象
13+
* @param object the instance to print
14+
* @param locale the current user locale
15+
* @return the printed text string
16+
*/
17+
String print(T object, Locale locale);
18+
19+
}
20+
```
21.1 KB
Loading

images/spring/Parser.png

41.4 KB
Loading

0 commit comments

Comments
 (0)