2009-09-12 65 views
26

在看到许多关于编程语言的隐藏特性之后,我想知道Spring“事实”框架的隐藏功能。如您所知,Spring文档隐藏了很多功能,并且很高兴与您分享。Spring框架的隐藏功能?

而你:你知道Spring框架有什么隐藏功能吗?

回答

0

找到任何隐藏功能在Spring中的最佳方法只需要查看源代码。

虽然Spring对注释,AspectJ和DI的使用非常灵活,但很难说什么是隐藏功能。

1

一个是基于CGLib的类代理用于Spring的AOP。它也可以通过Java的动态代理完成,但默认为CGLib。所以,如果你在堆栈跟踪中看到CGLib,不要感到惊讶。

其他是DI的使用AOP。是的,所有这些都是AOP,没有你知道。知道你的代码是基于AOP的,即使你只使用Spring来实现DI目标也很酷。

27

Spring提供了强大的内置StringUtils

通知包含了一组ID的

String [] idArray = new String [] {"0", "1", "2", "0", "5", "2"} 

以下阵列和要删除的重复引用。只要做到这一点

idArray = StringUtils.removeDuplicateStrings(idArray); 

现在idArray将包含{“0”,“1”,“2”,“5”}。

还有更多。


是否可以使用Controller类而不在Web应用程序上下文文件中声明它们?

是的,只是把@Component在其声明(@Controller不起作用如预期)

package br.com.spring.view.controller 

@Component 
public class PlayerController extends MultiActionController { 

    private Service service; 

    @Autowired 
    public PlayerController(InternalPathMethodNameResolver ipmnr, Service service) { 
     this.service = service; 

     setMethodNameResolver(ipmnr); 
    } 

    // mapped to /player/add 
    public ModelAndView add(...) {} 

    // mapped to /player/remove 
    public ModelAndView remove(...) {} 

    // mapped to /player/list 
    public ModelAndView list(...) {} 

} 

所以在Web应用程序上下文文件把下面的

<beans ...> 
    <context:component-scan base-package="br.com.spring.view"/> 
    <context:annotation-config/> 
    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"> 
     <property name="order" value="0"/> 
     <property name="caseSensitive" value="true"/> 
    </bean> 
    <bean class="org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver"/> 
</beans> 

闲来无事


动态表单验证?

使用以下

public class Team { 

    private List<Player> playerList;  

} 

所以在团队验证把

@Component 
public class TeamValidator implements Validator { 

    private PlayerValidator playerValidator; 

    @Autowired 
    public TeamValidator(PlayerValidator playerValidator) { 
     this.playerValidator = playerValidator; 
    } 

    public boolean supports(Class clazz) { 
     return clazz.isAssignableFrom(Team.class); 
    } 

    public void validate(Object command, Errors errors) { 
     Team team = (Team) command; 

     // do Team validation 

     int index = 0; 
     for(Player player: team.getPlayerList()) { 
      // Notice code just bellow 
      errors.pushNestedPath("playerList[" + index++ + "]"); 

      ValidationUtils.invokeValidator(playerValidator, player, errors); 

      errors.popNestedPath(); 
     } 

    } 

} 

在网页表单继承?

使用以及隐藏的表单标志

public class Command { 

    private Parent parent; 

} 

public class Child extends Parent { ... } 

public class AnotherChild extends Parent { ... } 

而在你的控制器ServlerRequestDataBinder请执行下列操作

public class MyController extends MultiActionController { 

    public ModelAndView action(HttpServletRequest request, HttpServletResponse response, Object command) { 

     Command command = (Command) command; 

     // hidden form flag 
     String parentChildType = ServletRequestUtils.getRequiredStringParameter(request, "parentChildType"); 

     // getApplicationContext().getBean(parentChildType) retrieves a Parent object from a application context file 
     ServletRequestDataBinder binder = 
      new ServletRequestDataBinder(getApplicationContext().getBean(parentChildType)); 

     // populates Parent child object 
     binder.bind(request); 

     command.setParent((Parent) binder.getTarget()); 
} 

Spring有一个内置的扫描仪类ClassPathScanningCandidateComponentProvider。例如,您可以使用它来查找注释。

ClassPathScanningCandidateComponentProvider scanner = 
    new ClassPathScanningCandidateComponentProvider(<DO_YOU_WANT_TO_USE_DEFAULT_FILTER>); 

scanner.addIncludeFilter(new AnnotationTypeFilter(<TYPE_YOUR_ANNOTATION_HERE>.class)); 

for (BeanDefinition bd : scanner.findCandidateComponents(<TYPE_YOUR_BASE_PACKAGE_HERE>)) 
    System.out.println(bd.getBeanClassName()); 

Spring提供了有用的org.springframework.util.FileCopyUtils类。您可以通过使用

public ModelAndView action(...) throws Exception { 

    Command command = (Command) command; 

    MultiPartFile uploadedFile = command.getFile(); 

    FileCopyUtils.copy(uploadedFile.getBytes(), new File("destination")); 

如果使用基于注解的控制器(春季2.5+)或纯MVC春季控制器复制上传的文件,你可以使用的WebBindingInitializer注册全局属性编辑器。像

public class GlobalBindingInitializer implements WebBindingInitializer { 

    public void initBinder(WebDataBinder binder, WebRequest request) { 
     binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("dd/MM/yyyy", true); 
    } 

} 

东西,所以在你的web应用程序上下文文件,声明

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean class="GlobalBindingInitializer"/> 
    </property> 
</bean> 

这样,任何基于注解控制器可以使用任何属性编辑器中GlobalBindingInitializer声明。

等等......

11

弹簧可以用作事件总线更换,因为ApplicationContext也是ApplicationEventPublisher

参考春季实现在SimpleApplicationEventMulticaster被发现,它可以有选择地使用一个线程池来异步分配事件。

+0

有用的东西,这。做这项工作的代码是'SimpleApplicationEventMulticaster',它可以选择使用线程池来异步分配事件。我对于一个厌倦了编写代码的人来说这样做。 – skaffman 2009-09-14 07:24:11

1

在运行时热交换弹簧豆。

这对测试很有用。

看到here

0

不同于典型的专有软件,春源代码是免费提供给任何人谁在乎下载。

因此春天没有隐藏的功能。它的特点是你还没有看到......但。