不同的Spring注释XML声明 [英] Different Spring Annotation XML Declarations

查看:119
本文介绍了不同的Spring注释XML声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有多个XML标记可告诉Spring使用注释:

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

<context:annotation-config/>

<context:component-scan base-package="org.example" />

<mvc:annotation-driven />

<tx:annotation-driven transaction-manager="transactionManager" />

我认为第一个标签说要扫描注释,第二个标签说要扫描哪个程序包(并且可以选择排除/包括要搜索的内容).然后,也许第三个说扫描控制器类.最后一个扫描数据访问注释.

I think the first tag says to scan for annotations, the second says which package to scan (and optionally, can exclude/include what to search for). Then maybe the third says to scan for controller classes. And the last one to scan for data access annotations.

我对问题的理解是:

  1. 我认为标签1和2可以合并为一个.所以我不知道他们为什么分开.
  2. 标签3和4似乎对于1和2是多余的.

谁能给我每个标签的用途的细目分类,为什么它们不是多余的?如果还有其他注释XML标签,请告诉我.

Could anyone give me a breakdown of what each tag does, and why they're not redundant? And if there are any other annotation XML tags, let me know.

经过进一步调查,我相信我发现了一些其他信息:

After further investigation, I believe I found some additional information:

  • <context:annotation-config/>标记允许您在变量,构造函数和方法(例如@ Autowired,@ Resource等)上使用注释.
  • 通过<context:component-scan base-package="org.example" />标记,您可以在类(例如@ Repository,@ Controller等)上使用注释.
  • <tx:annotation-driven transaction-manager="transactionManager" />标签实际上启用 @Transactional标签(<context:annotation-config/>标签允许@Transactional标签,但没有执行任何操作).
  • The <context:annotation-config/> tag allows you to use annotations on variables, constructors, and methods (e.g. @Autowired, @Resource, etc).
  • The <context:component-scan base-package="org.example" /> tag allows you to use annotations on classes (e.g. @Repository, @Controller, etc).
  • The <tx:annotation-driven transaction-manager="transactionManager" /> tag actually enables the @Transactional tag (the <context:annotation-config/> tag allowed the @Transactional tag, but it didn't do anything).

仍然不能完全确定<mvc:annotation-driven />.我认为可能为JSON等添加了更多支持.

Still not completely sure on the <mvc:annotation-driven />. I think maybe it adds further support for JSON, etc.

推荐答案

  1. annotation-configcomponent-scan

a)<context:annotation-config/>仅在定义它的相同应用程序上下文中在bean上查找注释.这意味着,如果将<context:annotation-config/>放入DispatcherServlet的WebApplicationContext中,它将仅检查控制器中的@Autowired bean,而不检查服务.请参见 15.2,"DispatcherServlet " 以获取更多信息.

a) <context:annotation-config/> only looks for annotations on beans in the same application context in which it is defined. This means that, if you put <context:annotation-config/> 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提供了自动检测定型"类并向ApplicationContext注册相应BeanDefinition的功能.为了自动检测这些类并注册相应的bean,需要在XML中包含component-scan元素,其中"basePackage"将是一个公共的父包(或者可以指定一个逗号分隔的列表,其中包括每个类的父包) .

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).

tx:annotation-driven

您确实直接在element中提供了事务管理器实例. annotation-configcomponent-scan不会.

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

mvc:annotation-driven

此标记注册Spring MVC将请求分派到@Controllers所需的DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter Bean.标签根据您的类路径中的内容为这两个bean配置合理的默认值.在 Spring文档中了解更多信息.

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.

这篇关于不同的Spring注释XML声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆