2009-09-12 62 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

不同於典型的專有軟件,春源代碼是免費提供給任何人誰在乎下載。

因此春天沒有隱藏的功能。它的特點是你還沒有看到......但。