Different Spring Annotation XML Declarations

后端 未结 2 1687
孤独总比滥情好
孤独总比滥情好 2021-01-14 00:37

There seem to be multiple XML tags for telling Spring to use annotations:





        
2条回答
  •  一个人的身影
    2021-01-14 01:31

    1. Difference between annotation-config and component-scan

      a) only looks for annotations on beans in the same application context in which it is defined. This means that, if you put in a WebApplicationContext for a DispatcherServlet, it only checks for @Autowired beans in your controllers, and not your services. See Section 15.2, “The DispatcherServlet” for more information.

      b) Spring provides the capability of automatically detecting 'stereotyped' classes and registering corresponding BeanDefinitions with the ApplicationContext. To autodetect these classes and register the corresponding beans requires the inclusion of the component-scan element in XML where 'basePackage' would be a common parent package (or alternatively a comma-separated list could be specified that included the parent package of each class).

    2. tx:annotation-driven

      You do provide the transaction-manager instace directly within element. annotation-config and component-scan won't.

    3. mvc:annotation-driven

      This tag registers the DefaultAnnotationHandlerMapping and AnnotationMethodHandlerAdapter beans that are required for Spring MVC to dispatch requests to @Controllers. The tag configures those two beans with sensible defaults based on what is present in your classpath. Read more at Spring doc.

提交回复
热议问题