Spring配置项<context:annotation-config>的解释说明

我们一般在含有Spring的项目中,可能会看到配置项中包含这个配置节点context:annotation-config,这是一条向Spring容器中注册

AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor

这4个BeanPostProcessor.注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

那么那些注释依赖这些Bean呢。

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

同样,传统的声明方式如下:

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> 

但是,就一般而言,这些注解我们是经常使用,比如Antowired,Resuource等注解,如果总是按照传统的方式一条一条的配置,感觉比较繁琐和机械。于是Spring给我们提供了context:annotation-config/的简化的配置方式,自动帮助你完成声明,并且还自动搜索@Component , @Controller , @Service , @Repository等标注的类。

<context:component-scan base-package="com.**.impl"/>

因此当使用 context:component-scan/ 后,就可以将 context:annotation-config/ 移除了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Spring 6中使用Spring MVC的初始化案例。 1. 首先,确保你的目中已经包含了Spring MVC和Jakarta Servlet API的依赖。你可以在Maven或Gradle中添加以下依赖: ```xml <!-- Maven 依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.0.0-M1</version> </dependency> <dependency> <groupId>jakarta.servlet</groupId> <artifactId>jakarta.servlet-api</artifactId> <version>4.0.3</version> <scope>provided</scope> </dependency> ``` ```groovy // Gradle 依赖 implementation 'org.springframework:spring-webmvc:6.0.0-M1' compileOnly 'jakarta.servlet:jakarta.servlet-api:4.0.3' ``` 2. 在Web应用程序中,需要在web.xml文件中配置Spring MVC的DispatcherServlet。以下是一个基本的web.xml文件示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <display-name>Spring MVC Application</display-name> <servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-mvc-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app> ``` 在这个示例中,我们定义了一个名为“dispatcherServlet”的Servlet,并将它映射到根路径“/”。其中,`servlet-class`是Spring MVC的`DispatcherServlet`类,`init-param`中的`contextConfigLocation`参数指定了Spring MVC配置文件的位置。 3. 创建Spring MVC配置文件`spring-mvc-config.xml`,并将其放置在WEB-INF目录下。以下是一个基本的Spring MVC配置文件示例: ```xml <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <context:component-scan base-package="com.example.controller"/> <mvc:annotation-driven/> </beans> ``` 在这个示例中,我们启用了Spring MVC的注解驱动(`<mvc:annotation-driven/>`),并使用`<context:component-scan>`扫描了一个名为`com.example.controller`的包,以寻找注解为`@Controller`的类。 4. 创建一个控制器类`HelloController`,并使用`@Controller`注解将其标记为Spring MVC控制器。以下是一个基本的控制器类示例: ```java package com.example.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @GetMapping("/hello") @ResponseBody public String hello() { return "Hello, Spring MVC!"; } } ``` 在这个示例中,我们使用`@Controller`注解将`HelloController`标记为一个Spring MVC控制器,并使用`@GetMapping`注解将`hello()`方法映射到路径`/hello`上。同时,使用`@ResponseBody`注解告诉Spring MVC将方法的返回值作为响应正文返回给客户端。 5. 启动你的Web应用程序,并在浏览器中访问`http://localhost:8080/hello`,你应该可以看到“Hello, Spring MVC!”的响应。 希望这个示例可以帮助你了解如何在Spring 6中使用Spring MVC。如果你还有其他问题,请随时提出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值