Spring框架的隐藏功能? [英] Hidden features of Spring framework?
问题描述
在看到许多关于编程语言的隐藏特性之后,我想知道Spring事实上框架的隐藏特性。如您所知,Spring文档隐藏了许多功能,并且很乐意分享它。
After seeing many hidden features about programming language, i would like to know hidden features of Spring "de facto" framework. As you know, Spring documentation hides many features and would be nice to share it.
您:您知道Spring框架的哪些隐藏功能?
And you: what hidden feature of Spring framework do you know ?
推荐答案
Spring有一个强大的内置 StringUtils class
Spring has a powerful built-in StringUtils class
请注意以下数组包含一组id
Notice the following array containing a set of id's
String [] idArray = new String [] {"0", "1", "2", "0", "5", "2"}
并且您想要删除重复的引用。只需这样做
And you want to remove duplicate references. Just do it
idArray = StringUtils.removeDuplicateStrings(idArray);
现在idArray将包含{0,1,2,5} 。
Now idArray will contain {"0", "1", "2", "5"}.
还有更多。
是否可以使用一个Controller类没有在Web应用程序上下文文件中声明它们?
Is it possible to use a Controller class without declare them in web application context file?
是的,只需将@Component放入其声明中(@Controller无法按预期工作)
Yes, just put @Component in its declaration (@Controller does not work as expected)
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应用程序上下文文件中输入以下内容
So in web application context file put the following
<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;
}
所以在Team验证器中放
So in Team validator put
@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();
}
}
}
< hr>
Web表单中的继承?
Inheritance in web forms ?
使用ServlerRequestDataBinder和隐藏表单标志
Use ServlerRequestDataBinder along with hidden form flag
public class Command {
private Parent parent;
}
public class Child extends Parent { ... }
public class AnotherChild extends Parent { ... }
在您的控制器中执行以下操作
And in your controller do the following
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 一>。例如,您可以使用它来查找注释。
Spring has a built-in scanner class ClassPathScanningCandidateComponentProvider. You can use it to find annotations, for instance.
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"));
如果使用基于注释的控制器(Spring 2.5) +)或者是普通的MVC Spring控制器,你可以使用WebBindingInitializer来注册全局属性编辑器。类似
If you use a annotation based controller (Spring 2.5+) OR plain MVC Spring controller, you can use a WebBindingInitializer to register global property editors. Something like
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应用程序上下文文件中,声明
So in your web application context file, declare
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
<bean class="GlobalBindingInitializer"/>
</property>
</bean>
这样任何基于注释的控制器都可以使用在GlobalBindingInitializer中声明的任何属性编辑器。
This way any annotation based controller can use any property editor declared in GlobalBindingInitializer.
依此类推......
And so on...
这篇关于Spring框架的隐藏功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!