Spring4新特性——注解、脚本、任务、MVC等其他特性改进

一、注解方面的改进

spring4对注解API和ApplicationContext获取注解Bean做了一点改进。

获取注解的注解,如@Service是被@Compent注解的注解,可以通过如下方式获取@Componet注解实例:

Java代码   收藏代码
  1. Annotation service = AnnotationUtils.findAnnotation(ABService.class, org.springframework.stereotype.Service.class);  
  2. Annotation component = AnnotationUtils.getAnnotation(service, org.springframework.stereotype.Component.class);  

获取重复注解:

比如在使用hibernate validation时,我们想在一个方法上加相同的注解多个,需要使用如下方式:

Java代码   收藏代码
  1. @Length.List(  
  2.         value = {  
  3.                 @Length(min = 1, max = 2, groups = A.class),  
  4.                 @Length(min = 3, max = 4, groups = B.class)  
  5.         }  
  6. )  
  7. public void test() {  

可以通过如下方式获取@Length:

Java代码   收藏代码
  1. Method method = ClassUtils.getMethod(AnnotationUtilsTest.class"test");  
  2. Set<Length> set = AnnotationUtils.getRepeatableAnnotation(method, Length.List.class, Length.class);  

当然,如果你使用Java8,那么本身就支持重复注解,比如spring的任务调度注解,

Java代码   收藏代码
  1. @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. @Repeatable(Schedules.class)  
  5. public @interface Scheduled {   
Java代码   收藏代码
  1. @Target({ElementType.METHOD, ElementType.ANNOTATION_TYPE})  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. @Documented  
  4. public @interface Schedules {  
  5.   
  6.     Scheduled[] value();  
  7.   
  8. }  

这样的话,我们可以直接同时注解相同的多个注解:

Java代码   收藏代码
  1. @Scheduled(cron = "123")  
  2. @Scheduled(cron = "234")  
  3. public void test     

但是获取的时候还是需要使用如下方式:

Java代码   收藏代码
  1. AnnotationUtils.getRepeatableAnnotation(ClassUtils.getMethod(TimeTest.class"test"), Schedules.class, Scheduled.class)  

ApplicationContext和BeanFactory提供了直接通过注解获取Bean的方法:

Java代码   收藏代码
  1. @Test  
  2. public void test() {  
  3.     AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();  
  4.     ctx.register(GenericConfig.class);  
  5.     ctx.refresh();  
  6.   
  7.     Map<String, Object> beans = ctx.getBeansWithAnnotation(org.springframework.stereotype.Service.class);  
  8.     System.out.println(beans);  
  9. }  

这样可以实现一些特殊功能。

另外和提供了一个AnnotatedElementUtils用于简化java.lang.reflect.AnnotatedElement的操作,具体请参考其javadoc。   

二、脚本的支持 

spring4也提供了类似于javax.script的简单封装,用于支持一些脚本语言,核心接口是:

Java代码   收藏代码
  1. public interface ScriptEvaluator {  
  2.     Object evaluate(ScriptSource script) throws ScriptCompilationException;  
  3.     Object evaluate(ScriptSource script, Map<String, Object> arguments) throws ScriptCompilationException;  
  4. }  

比如我们使用groovy脚本的话,可以这样:

Java代码   收藏代码
  1. @Test  
  2. public void test() throws ExecutionException, InterruptedException {  
  3.     ScriptEvaluator scriptEvaluator = new GroovyScriptEvaluator();  
  4.   
  5.     //ResourceScriptSource 外部的  
  6.     ScriptSource source = new StaticScriptSource("i+j");  
  7.     Map<String, Object> args = new HashMap<>();  
  8.     args.put("i"1);  
  9.     args.put("j"2);  
  10.     System.out.println(scriptEvaluator.evaluate(source, args));  
  11. }  

没什么很特别的地方。另外还提供了BeanShell(BshScriptEvaluator)和javax.script(StandardScriptEvaluator)的简单封装。

三、Future增强

提供了一个ListenableFuture,其是jdk的Future的封装,用来支持回调(成功/失败),其借鉴了com.google.common.util.concurrent.ListenableFuture。

Java代码   收藏代码
  1. @Test  
  2. public void test() throws Exception {  
  3.     ListenableFutureTask<String> task = new ListenableFutureTask<String>(new Callable() {  
  4.         @Override  
  5.         public Object call() throws Exception {  
  6.             Thread.sleep(10 * 1000L);  
  7.             System.out.println("=======task execute");  
  8.             return "hello";  
  9.         }  
  10.     });  
  11.   
  12.     task.addCallback(new ListenableFutureCallback<String>() {  
  13.         @Override  
  14.         public void onSuccess(String result) {  
  15.             System.out.println("===success callback 1");  
  16.         }  
  17.   
  18.         @Override  
  19.         public void onFailure(Throwable t) {  
  20.         }  
  21.     });  
  22.   
  23.     task.addCallback(new ListenableFutureCallback<String>() {  
  24.         @Override  
  25.         public void onSuccess(String result) {  
  26.             System.out.println("===success callback 2");  
  27.         }  
  28.   
  29.         @Override  
  30.         public void onFailure(Throwable t) {  
  31.         }  
  32.     });  
  33.   
  34.     ExecutorService executorService = Executors.newSingleThreadExecutor();  
  35.     executorService.submit(task);  
  36.     String result = task.get();  
  37.     System.out.println(result);  
  38.   
  39. }  

可以通过addCallback添加一些回调,当执行成功/失败时会自动调用。

四、MvcUriComponentsBuilder

MvcUriComponentsBuilder类似于ServletUriComponentsBuilder,但是可以直接从控制器获取URI信息,如下所示:

假设我们的控制器是:

Java代码   收藏代码
  1. @Controller  
  2. @RequestMapping("/user")  
  3. public class UserController {  
  4.   
  5.     @RequestMapping("/{id}")  
  6.     public String view(@PathVariable("id") Long id) {  
  7.         return "view";  
  8.     }  
  9.   
  10.     @RequestMapping("/{id}")  
  11.     public A getUser(@PathVariable("id") Long id) {  
  12.         return new A();  
  13.     }  
  14.   
  15. }  

注:如果在真实mvc环境,存在两个@RequestMapping("/{id}")是错误的。当前只是为了测试。

我们可以通过如下方式得到

Java代码   收藏代码
  1. //需要静态导入 import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.*;  
  2. @Test  
  3. public void test() {  
  4.     MockHttpServletRequest req = new MockHttpServletRequest();  
  5.     RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(req));  
  6.   
  7.     //MvcUriComponentsBuilder类似于ServletUriComponentsBuilder,但是直接从控制器获取  
  8.     //类级别的  
  9.     System.out.println(  
  10.             fromController(UserController.class).build().toString()  
  11.     );  
  12.   
  13.     //方法级别的  
  14.     System.out.println(  
  15.             fromMethodName(UserController.class"view", 1L).build().toString()  
  16.     );  
  17.   
  18.     //通过Mock方法调用得到  
  19.     System.out.println(  
  20.             fromMethodCall(on(UserController.class).getUser(2L)).build()  
  21.     );  
  22. }  

注意:当前MvcUriComponentsBuilder实现有问题,只有JDK环境支持,大家可以复制一份,然后修改:

method.getParameterCount() (Java 8才支持)

method.getParameterTypes().length

五、Socket支持

提供了获取Socket TCP/UDP可用端口的工具,如

SocketUtils.findAvailableTcpPort()

SocketUtils.findAvailableTcpPort(min, max) 

SocketUtils.findAvailableUdpPort()

非常简单,就不用特别说明了。

示例代码请参考:spring4-others

到此,spring4新特性就介绍完了,此处没有介绍websocket,后续有时间考虑写一个websocket完整系列,对于spring4除了websocket,其他方面并没有特别吸引人的功能。

猜你喜欢

转载自zzc1684.iteye.com/blog/2118878