Spring入门到精通,3.Spring 4.0修改内容

Spting 4.0 修改内容

1. 支持Java8的新特性

 1 package com.helios.change;
 2 
 3 import java.time.Instant;
 4 import java.time.ZoneId;
 5 import java.time.format.DateTimeFormatter;
 6 import java.util.Locale;
 7 
 8 import org.junit.Test;
 9 
10 import com.helios.TestConfig;
11 
12 public class Spring4_0_JDK8 extends TestConfig {
13 
14     @Test
15     public void testTime() {
16         Instant instant = Instant.now();
17         String dataStr = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
18                                           .withLocale(Locale.CHINA)
19                                           .withZone(ZoneId.systemDefault())
20                                           .format(instant);
21         System.out.println("now is:" + dataStr);
22     }
23     
24     @Test
25     public void testLambda() {
26         new Thread(() -> {
27             int i = 0;
28             while(i++ <= 100) {
29                 System.out.println("i:" + i);
30             }
31         }).start();
32         int j = 0;
33         while(j ++ <= 100) {
34             System.out.println("j:" + j);
35         }
36     }
37 }
测试代码

2. 最低支持JDK 6 update 18

3. 自动注入支持泛型

如果存在子类的泛型实现,则自动注入.

如果存在多个相同类型的bean,则会报 org.springframework.beans.factory.NoUniqueBeanDefinitionException 

 1 package com.helios.change;
 2 
 3 import java.util.List;
 4 
 5 import org.junit.Test;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.context.annotation.Bean;
 8 import org.springframework.context.annotation.Configuration;
 9 
10 import com.helios.TestConfig;
11 
12 public class Spring4_0_Autowired extends TestConfig {
13 
14     /*
15      *  因为存在多个相同的 StringList实例,所以会报
16      *  No qualifying bean of type [com.helios.change.ComputeList] is defined: expected single matching bean but found 2: getString,getString2
17      *  @Autowired
18      *  private ComputeList<String> list1;
19      */
20     
21     @Autowired
22     private ComputeList<Integer> list2;
23     
24     @Autowired
25     private List<ComputeList<String>> lists;
26     
27     @Test
28     public void test() {
29         System.out.println(String.format("%s is null? %b", "list2", list2 == null));
30         System.out.println(String.format("%s is null? %b", "lists", lists == null));
31         if(list2 != null) System.out.println("list2:" + list2.output());
32         if(lists != null) lists.forEach((list) -> System.out.println("lists 中的:" + list.output()));
33     }
34 }
35 
36 interface ComputeList<T> {
37     @SuppressWarnings("unchecked")
38     public ComputeList<T> input(T...ts);
39     public T output();
40 }
41 
42 class StringList implements ComputeList<String> {
43 
44     private String result = null;
45     
46     @Override
47     public StringList input(String...strs) {
48         StringBuffer buffer = new StringBuffer();
49         for(String str : strs) {
50             buffer.append(str);
51         }
52         result = buffer.toString();
53         return this;
54     }
55     
56     @Override
57     public String output() {
58         return result;
59     }
60 }
61 
62 class IntegerList implements ComputeList<Integer> {
63 
64     private Integer result = null;
65     
66     @Override
67     public IntegerList input(Integer... numbers) {
68         int sum = 0;
69         for(Integer number : numbers) {
70             sum += number;
71         }
72         result = sum;
73         return this;
74     }
75     
76     @Override
77     public Integer output() {
78         return result;
79     }
80 }
81 
82 @Configuration
83 class ListsBeans {
84     
85     @Bean
86     public StringList getString() {
87         return new StringList().input("I'm", " ", "now", " ", "learn Spring");
88     }
89     
90     @Bean
91     public StringList getString2() {
92         return new StringList().input("This", " is second", " String List");
93     }
94     
95     @Bean
96     public IntegerList getInt() {
97         return new IntegerList().input(15, 98, -5);
98     }
99 }
测试代码

测试结果如下:

官方参考文档 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-generics-as-qualifiers

4. 支持定义自定义的注解

官方参考文档 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-meta-annotations

5. 可以使用@Order注解对bean加载进行排序

 1 package com.helios.change;
 2 
 3 
 4 import org.junit.Test;
 5 import org.springframework.context.annotation.Bean;
 6 import org.springframework.context.annotation.Configuration;
 7 import org.springframework.core.annotation.Order;
 8 
 9 import com.helios.TestConfig;
10 
11 public class Spring4_0_Order extends TestConfig {
12     
13     @Test
14     public void order() {
15         System.out.println("order");
16     }
17 }
18 
19 @Order(2)
20 @Configuration
21 class OrderConfig1 {
22     
23     @Bean
24     public Object load1() {
25         System.out.println("config1 load object");
26         return null;
27     }
28 }
29 
30 @Order(3)
31 @Configuration
32 class OrderConfig2 {
33     
34     @Bean
35     public Object load2() {
36         System.out.println("config22 load object");
37         return null;
38     }
39 }
40 
41 @Order(1)
42 @Configuration
43 class OrderConfig3 {
44     
45     @Bean
46     public Object load3() {
47         System.out.println("config333 load object");
48         return null;
49     }
50 }
测试代码

测试结果如下:

6. 可以使用@Lazy注解对bean进行延迟加载

 1 package com.helios.change;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.annotation.Bean;
 5 import org.springframework.context.annotation.Configuration;
 6 import org.springframework.context.annotation.Lazy;
 7 
 8 import com.helios.TestConfig;
 9 
10 public class Spring4_0_Lazy extends TestConfig {
11     
12     @Test
13     public void order() {
14         System.out.println("lazy");
15     }
16 }
17 
18 @Lazy
19 @Configuration
20 class LazyConfig1 {
21     
22     @Bean
23     public Object load1() {
24         System.out.println("config1 load object");
25         return null;
26     }
27 }
28 
29 @Configuration
30 class LazyConfig2 {
31     
32     @Bean
33     public Object load2() {
34         System.out.println("config22 load object");
35         return null;
36     }
37 }
38 
39 @Lazy
40 @Configuration
41 class LazyConfig3 {
42     
43     @Bean
44     public Object load3() {
45         System.out.println("config333 load object");
46         return null;
47     }
48 }
测试代码

测试结果如下:

7.可以使用@Description注解进行添加描述

官方参考文档 http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-java-bean-description 

posted @ 2017-02-07 11:31  晓的幸福  阅读(828)  评论(0编辑  收藏  举报