在看过编程语言的许多隐藏功能后,我想知道Spring“事实”框架的隐藏功能。正如您所知道的,Spring文档隐藏了许多特性,并且很乐意与您共享这些特性。
还有你:你知道Spring框架的什么隐藏功能吗?
发布于 2009-09-14 03:02:12
Spring有一个强大的内置StringUtils类
请注意以下数组,其中包含一组id
String [] idArray = new String [] {"0", "1", "2", "0", "5", "2"} 并且您想要删除重复的引用。就这么做
idArray = StringUtils.removeDuplicateStrings(idArray);现在idArray将包含{"0","1","2","5"}。
还有更多。
可以在不在web应用程序上下文文件中声明的情况下使用Controller类吗?
可以,只需将@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();
}
}
}web表单中的继承?
将ServlerRequestDataBinder与隐藏表单标志一起使用
public class Command {
private Parent parent;
}
public class Child extends Parent { ... }
public class AnotherChild extends Parent { ... }并在您的控制器中执行以下操作
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"));如果您使用基于注释的控制器(Spring )或普通的MVC Spring控制器,则可以使用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中声明的任何属性编辑器。
以此类推。
发布于 2009-09-14 05:20:20
由于ApplicationContext也是ApplicationEventPublisher,Spring可以用作事件总线的替代品
引用Spring实现可以在SimpleApplicationEventMulticaster中找到,它可以选择使用线程池来异步分发事件。
发布于 2009-09-13 18:39:06
一个是对Spring使用基于CGLib的类代理。它也可以通过Java的动态代理来完成,但默认是CGLib。因此,如果在堆栈跟踪中看到CGLib,请不要惊讶。
另一个是对DI使用AOP。是的,在你不知道的情况下,到处都是AOP。知道您的代码是基于AOP的是很酷的,即使您只将Spring用于DI目的。
https://stackoverflow.com/questions/1416423
复制相似问题