`
zhangdefeng2008
  • 浏览: 12329 次
社区版块
存档分类
最新评论

<context:component-scan/>和<mvc:annotation-driven/>的区别

mvc 
阅读更多

mvc:annotation-driven 注解
<mvc:annotation-driven/>
相当于注册了DefaultAnnotationHandlerMapping和AnnotationMethodHandlerAdapter两个bean,配置一些messageconverter。即解决了@Controller注解的使用前提配置

 

<context:annotation-config/>
1)隐式地向Spring容器中注册AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor 及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。
在配置文件中使用<context:annotationconfig/>之前,必须在 <beans> 元素中声明 context 命名空间<context:component-scan/>。
2)是对包进行扫描,实现注释驱动Bean定义,同时将bean自动注入容器中使用。即解决了@Controller标识的类的bean的注入和使用。 

 

<context:component-scan/> 
配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此当使用 <context:component-scan/> 后,除非需要使用PersistenceAnnotationBeanPostProcessor和equiredAnnotationBeanPostProcessor两个Processor的功能(例如JPA等)否则就可以将 <context:annotation-config/> 移除了。

 

spring mvc <mvc:annotation-driven />会自动启动Spring MVC的注解功能,但实际它做了哪些工作呢?

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="webBindingInitializer">
  <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer">
   <property name="conversionService" ref="conversionService" />
   <property name="validator" ref="validator" />
  </bean>
</property>
</bean>
<bean id="conversionService" class="org.springframework.samples.petclinic.util.PetclinicConversionServiceFactory" />
<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> 

分享到:
评论

相关推荐

    使用Maven构建Spring MVC项目的简单示例

    &lt;mvc:annotation-driven /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".jsp" ...

    Spring MVC--2.入门程序

    &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; ...

    Spring mvc 环境搭建(maven构建)

    &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; ...

    spring-mvc 注解方式xml配置

    `&lt;mvc:annotation-driven&gt;`则启用了Spring MVC的注解支持。 最后,`web.xml`是应用的部署描述符,配置`DispatcherServlet`以便处理所有HTTP请求: ```xml &lt;web-app&gt; &lt;servlet&gt; &lt;servlet-name&gt;dispatcher&lt;/...

    使用maven简单搭建Spring mvc + redis缓存

    &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; ...

    框架ssm整合

    &lt;tx:annotation-driven transaction-manager="transactionManager"/&gt; &lt;!-- 扫描Service层 --&gt; &lt;context:component-scan base-package="com.example.service"/&gt; ``` #### 五、实现业务逻辑 根据需求,需要实现用户...

    java8+tomcat8+struts2.5+spring4.3+hibernate5.2框架搭建详细过程

    &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;property name="suffix" value=".jsp"/&gt; ...

    SpringMVC入门

    - **启用注解驱动**:使用`&lt;mvc:annotation-driven/&gt;`来启用Spring MVC的注解功能。 - **配置视图解析器**:定义视图解析器`InternalResourceViewResolver`来处理视图名与实际视图文件之间的映射关系。 #### 三、...

    spring mvc架构搭建,实现简单的查询用户查询功能

    &lt;mvc:annotation-driven/&gt; &lt;!-- 视图解析器配置 --&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;property name...

    Spring MVC入门小例子

    &lt;mvc:annotation-driven /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".jsp" ...

    集成springmvc spring hibernate的配置

    &lt;/context:component-scan&gt; ``` 2. **数据库连接配置**:我们需要从`jdbc.properties`文件中读取数据库连接信息。使用`context:property-placeholder`标签将属性文件加载到Spring上下文中。然后,配置数据源`...

    基于xml配置的spring mvc Helloworld实例

    &lt;mvc:annotation-driven /&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".jsp" ...

    springmvc搭建

    &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".jsp" /...

    SpringMVC+Hibernate实例

    &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="viewClass" value="org.springframework....

    SpringMVC知识点.doc

    &lt;mvc:annotation-driven/&gt; &lt;!-- 配置视图解析器 --&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/"/&gt; &lt;property name...

    基于注解的 Spring MVC 简单入门

    而`&lt;mvc:annotation-driven&gt;`元素则启用了Spring MVC对注解的支持,如@RequestMapping等,让我们的控制器可以使用注解进行方法映射。 现在让我们了解一下注解在Spring MVC中的作用。`@Controller`注解标记一个类为...

    springmvc搭建demo

    &lt;mvc:annotation-driven/&gt; &lt;!-- 数据源配置 --&gt; &lt;bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; &lt;property name="driverClassName" value=...

    SpringMVC+Hibernate全注解整合

    &lt;mvc:annotation-driven/&gt; &lt;!-- 扫描包 --&gt; &lt;context:annotation-config/&gt; &lt;context:component-scan base-package="com.org.*" /&gt; &lt;bean id="jspViewResolver" class="org.springframework.web.servlet.view...

    springMVC框架搭建及详解

    &lt;mvc:annotation-driven/&gt; &lt;!-- 资源处理 --&gt; &lt;mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/&gt; &lt;!-- 其他自定义配置... --&gt; &lt;/beans&gt; ``` 通过以上步骤,可以成功搭建一个基于...

    spring framework入门(11):springmvc简易环境搭建

    &lt;mvc:annotation-driven/&gt; &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; &lt;property name="prefix" value="/WEB-INF/views/" /&gt; &lt;property name="suffix" value=".jsp" /...

Global site tag (gtag.js) - Google Analytics