Spring框架的隐藏function?
在看到许多关于编程语言的隐藏特性之后,我想知道Spring“事实上”框架的隐藏特性。 如您所知,Spring文档隐藏了许多function,并且很乐意分享它。
而你:你知道Spring框架的隐藏function是什么?
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应用程序上下文文件中放入以下内容
没有其他的
以动态formsvalidation?
使用以下内容
public class Team { private List playerList; }
所以在Teamvalidation器中
@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表单中的inheritance?
使用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(); scanner.addIncludeFilter(new AnnotationTypeFilter(.class)); for (BeanDefinition bd : scanner.findCandidateComponents()) 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注册全局属性编辑器。 就像是
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应用程序上下文文件中,声明
这样,任何基于注释的控制器都可以使用GlobalBindingInitializer中声明的任何属性编辑器。
等等…
Spring可以用作事件总线替换,因为ApplicationContext
也是ApplicationEventPublisher
参考Spring实现可以在SimpleApplicationEventMulticaster
找到,它可以选择使用线程池异步分发事件。
一个是使用基于CGLib的类代理来实现Spring的AOP。 它也可以通过Java的动态代理来完成,但默认是CGLib。 因此,如果您在堆栈跟踪中看到CGLib,请不要感到惊讶。
其他是使用AOP进行DI。 是的,在你不知道的情况下,它到处都是AOP。 很高兴知道你的代码是基于AOP的,即使你只是使用Spring进行DI目的。
热交换春豆在运行时。
这对测试很有用。
看到这里
在Spring中查找任何隐藏function的最佳方法是查看源代码。
很难说什么是隐藏的function,因为Spring对注释,AspectJ和DI的使用非常灵活。
与典型的专有软件不同,Spring的源代码可供任何关心下载的人免费使用。
因此Spring没有隐藏的function。 它只是你还没见过的特色。
- Java中是否有与C#的’AS’关键字类似的关键字
- OOP:ArrayList al = new ArrayList()和List al = new ArrayList()之间的区别?
- Java算法制作直金字塔
- 一个简单的javamultithreading
- RestTemplate uriVariables未展开
- 泽西/jackson@JsonIgnore在二传手
- 无法执行目标org.codehaus.mojo:tomcat-maven-plugin :: run
- 使用MessageDigest SHA-256的POI XSSF / XLSX散列不确定性
- 接受05/05/1999和5/5/1999等的日期时间解析