2010-07-19 72 views
6

我希望能够做到这一点:我们可以在其他注释中使用spring表达式(spel)吗?

@Controller 
@RequestMapping("/#{handlerMappingPaths.security}/*") 
public class SecurityController { 
    etc 

    //for instance, to resuse the value as a base for the folder resolution  
    @Value("#{handlerMappingPaths.security}/") 
    public String RESOURCE_FOLDER; 

    @RequestMapping(value="/signin-again", method = RequestMethod.GET) 
    public String signinAgainHandler() { 
     return RESOURCE_FOLDER + "signin_again"; 
    } 
} 

这不会出现现在的工作,我失去的东西吗?你可以找到这样的事情

+0

澄清,该SPEL表达的作品只是设置的“RESOURCE_FOLDER”价值@value注释很好,但它不会在工作@ RequestMapping注释 – chrismarx 2010-07-19 18:08:24

回答

0

@Sean回答了春天是否支持这个问题,但我也想回答一下在使用注释时如何不重复配置的问题。事实证明这是可能的使用静态进口,如:

import static com.test.util.RequestMappingConstants.SECURITY_CONTROLLER_PATH 

@Controller 
@RequestMapping("/" + SECURITY_CONTROLLER_PATH + "/*") 
public class SecurityController { 
    etc 

    //for instance, to resuse the value as a base for the folder resolution  
    public String RESOURCE_FOLDER = SECURITY_CONTROLLER_PATH + "/"; 

    @RequestMapping(value="/signin-again", method = RequestMethod.GET) 
    public String signinAgainHandler() { 
     return RESOURCE_FOLDER + "signin_again"; 
    } 
} 
2

一种方法是来看看自己。这是eclipse的一个例子,但它应该类似地用于其他IDE:

首先,确保你有你正在使用的弹簧库的来源。如果您使用maven,using the maven-eclipse-plugin或使用m2eclipse,这是最简单的。

然后,在Eclipse中,选择Navigate -> Open Type...。输入你正在寻找的类型(像RequestMa*应该为像我这样的懒惰类型)。输入/确定。现在右键单击源文件中的类名并选择References -> Project。在搜索视图中,将显示此类或注释的所有用法。

其中之一是DefaultAnnotationHandlerMapping.determineUrlsForHandlerMethods(Class, boolean),在此代码段会告诉你,语言表达不评估:

ReflectionUtils.doWithMethods(currentHandlerType, new ReflectionUtils.MethodCallback() { 
    public void doWith(Method method) { 
     RequestMapping mapping = AnnotationUtils.findAnnotation(
            method, RequestMapping.class); 
     if (mapping != null) { 
      String[] mappedPatterns = mapping.value(); 
      if (mappedPatterns.length > 0) { 
       for (String mappedPattern : mappedPatterns) { 
        // this is where Expression Language would be parsed 
        // but it isn't, as you can see 
        if (!hasTypeLevelMapping && !mappedPattern.startsWith("/")) { 
         mappedPattern = "/" + mappedPattern; 
        } 
        addUrlsForPath(urls, mappedPattern); 
       } 
      } 
      else if (hasTypeLevelMapping) { 
       urls.add(null); 
      } 
     } 
    } 
}, ReflectionUtils.USER_DECLARED_METHODS); 

请记住,这就是所谓的开源。如果您不试图了解您使用的是什么,那么使用开放源代码软件没有意义。

+6

谢谢你的答案,但请保存你的蔑视,你不知道什么海报已经尝试过或没有尝试过 – chrismarx 2010-09-27 18:31:35

+2

@chris真实的,不要采取这个人,要么。 SO答案针对的不仅仅是OP中的更多观众。我正在做一个普遍的观察,你的情况可能是不合理的 – 2010-09-27 18:41:56

+0

最后一行=纯金。 – 2013-12-10 11:14:08

相关问题