ITeye博客

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

wbj0110 · 2013-12-29 12:39 · Spring · 编程语言 · Spring · Markdown

一、注解方面的改进

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. Scheduled[] value();
  6. }

 

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

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. Map<String, Object> beans = ctx.getBeansWithAnnotation(org.springframework.stereotype.Service.class);
  7. System.out.println(beans);
  8. }

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

 

另外和提供了一个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. //ResourceScriptSource 外部的
  5. ScriptSource source = new StaticScriptSource("i+j");
  6. Map<String, Object> args = new HashMap<>();
  7. args.put("i", 1);
  8. args.put("j", 2);
  9. System.out.println(scriptEvaluator.evaluate(source, args));
  10. }

没什么很特别的地方。另外还提供了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. task.addCallback(new ListenableFutureCallback<String>() {
  12. @Override
  13. public void onSuccess(String result) {
  14. System.out.println("===success callback 1");
  15. }
  16. @Override
  17. public void onFailure(Throwable t) {
  18. }
  19. });
  20. task.addCallback(new ListenableFutureCallback<String>() {
  21. @Override
  22. public void onSuccess(String result) {
  23. System.out.println("===success callback 2");
  24. }
  25. @Override
  26. public void onFailure(Throwable t) {
  27. }
  28. });
  29. ExecutorService executorService = Executors.newSingleThreadExecutor();
  30. executorService.submit(task);
  31. String result = task.get();
  32. System.out.println(result);
  33. }

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

 

四、MvcUriComponentsBuilder

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

假设我们的控制器是:

Java代码   收藏代码

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

注:如果在真实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. //MvcUriComponentsBuilder类似于ServletUriComponentsBuilder,但是直接从控制器获取
  7. //类级别的
  8. System.out.println(
  9. fromController(UserController.class).build().toString()
  10. );
  11. //方法级别的
  12. System.out.println(
  13. fromMethodName(UserController.class, "view", 1L).build().toString()
  14. );
  15. //通过Mock方法调用得到
  16. System.out.println(
  17. fromMethodCall(on(UserController.class).getUser(2L)).build()
  18. );
  19. }

注意:当前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,其他方面并没有特别吸引人的功能。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。