Java Spring框架的隐藏特性?

Java Spring框架的隐藏特性?,java,spring,Java,Spring,在看到编程语言的许多隐藏特性之后,我想了解Spring“事实上”框架的隐藏特性。正如您所知,Spring文档隐藏了许多特性,如果能够共享这些特性就好了 您:您知道Spring框架的哪些隐藏特性吗?查找Spring中任何隐藏特性的最佳方法只需查看源代码即可 很难说什么是隐藏的特性,尽管Spring在注释、AspectJ和DI的使用方面非常灵活。其中之一是在Spring的AOP中使用基于CGLib的类代理。它也可以通过Java的动态代理完成,但默认为CGLib。因此,如果在堆栈跟踪中看到CGLib,

在看到编程语言的许多隐藏特性之后,我想了解Spring“事实上”框架的隐藏特性。正如您所知,Spring文档隐藏了许多特性,如果能够共享这些特性就好了

您:您知道Spring框架的哪些隐藏特性吗?

查找Spring中任何隐藏特性的最佳方法只需查看源代码即可


很难说什么是隐藏的特性,尽管Spring在注释、AspectJ和DI的使用方面非常灵活。

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


另一个是将AOP用于DI。是的,在你不知道的情况下到处都是AOP。知道您的代码是基于AOP的很酷,即使您仅将Spring用于DI目的。

Spring有一个强大的内置类

注意下面的数组包含一组id

String [] idArray = new String [] {"0", "1", "2", "0", "5", "2"} 
并且您希望删除重复的引用。就这么做吧

idArray = StringUtils.removeDuplicateStrings(idArray);
现在idArray将包含{“0”、“1”、“2”、“5”}

还有更多


是否可以使用控制器类而不在web应用程序上下文文件中声明它们

是的,只需将@Component放入其声明中(@Controller未按预期工作)

因此,在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 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());
}

web表单中的继承

将ServlerRequestDataBinder与隐藏表单标志一起使用

public class Command {

    private Parent parent;

}

public class Child extends Parent { ... }

public class AnotherChild extends Parent { ... }
在控制器中执行以下操作

<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 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有一个内置的scanner类。例如,您可以使用它来查找注释

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 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应用程序上下文文件中,声明

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

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


等等…

Spring可以用作事件总线的替代品,因为


可以在中找到引用Spring实现,它可以选择使用线程池异步分发事件

运行时热插拔SpringBean

这对于测试很有用


参见

与典型的专有软件不同,Spring的源代码可以免费提供给任何想下载它的人


因此,Spring没有隐藏的特性。它只是你没有看到的功能。。。然而。

有用的东西,这个。执行这项工作的代码是
SimpleApplicationEventMulticast
,它可以选择使用线程池异步分发事件。就我而言,我厌倦了编写代码来完成这项工作。
public class GlobalBindingInitializer implements WebBindingInitializer {

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

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