spring使用注解时配置文件的写法

简介:

在spring的配置文件中:

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="
http://www.springframework.org/schema/context"
       xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
        
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        
http://www.springframework.org/schema/context
        
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

       <context:annotation-config/> 

</beans>

 

 我们会看到这样写:

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="
http://www.springframework.org/schema/context"
       xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
        
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        
http://www.springframework.org/schema/context
        
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="cn.test">
        <context:exclude-filter type="annotation"   expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

</beans>

<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能,即还隐式地在内部注册了
AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor。

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

<context:component-scan/> 的base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。

通过exclude-filter把所有@Controller注解的控制器组件排除。我们知道@Controller一般标注的是action,不用把action注入的什么地方。

本文转自IT徐胖子的专栏博客51CTO博客,原文链接http://blog.51cto.com/woshixy/949684如需转载请自行联系原作者


woshixuye111

相关文章
|
4天前
|
Java 开发者 Spring
Spring Framework 中的 @Autowired 注解:概念与使用方法
【4月更文挑战第20天】在Spring Framework中,@Autowired 注解是实现依赖注入(Dependency Injection, DI)的一种非常强大的工具。通过使用 @Autowired,开发者可以减少代码中的引用绑定,提高模块间的解耦能力
28 6
|
1月前
|
XML Java 数据库连接
spring boot 参数的过滤注解与实战
在Spring Boot应用中,对于入参的过滤,通常会涉及到对Web层的数据验证和处理。Spring Boot借助Spring框架提供了强大的验证框架支持,主要基于JSR-303/JSR-380(Bean Validation API)规范,以及Spring自身的@Valid或@Validated注解来实现请求参数的验证。以下是一些常见的使用案例来展示如何对参数进行过滤和验证。
29 1
|
1月前
|
Java Spring 容器
【Java】Spring如何扫描自定义的注解?
【Java】Spring如何扫描自定义的注解?
35 0
|
1月前
|
Java 测试技术 数据库
SpringBoot:@Profile注解和Spring EL
SpringBoot:@Profile注解和Spring EL
|
1月前
|
Java API 开发者
Spring中@import注解终极揭秘
在Spring框架中,@Import注解可以用来引入一个或多个组件,这些组件通常是通过@Bean注解定义的,当使用@Import注解时,实际上是在告诉Spring:“除了当前配置类中的bean定义外,还想包含另一个配置类(或多个配置类)中定义的bean。”
Spring中@import注解终极揭秘
|
2月前
|
监控 Java 调度
Spring中的任务调度:探索@Scheduled和@Schedules注解的威力
Spring中的任务调度:探索@Scheduled和@Schedules注解的威力
35 0
|
1月前
|
存储 缓存 Java
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
【Spring原理高级进阶】有Redis为啥不用?深入剖析 Spring Cache:缓存的工作原理、缓存注解的使用方法与最佳实践
|
1月前
|
Java 数据库 Spring
【spring(四)】Spring事务管理和@Transactional注解
【spring(四)】Spring事务管理和@Transactional注解
|
2月前
|
安全 Java 数据库连接
《Spring Boot配置文件大揭秘:看懂 application.yaml 与 bootstrap.yaml 的不同》
《Spring Boot配置文件大揭秘:看懂 application.yaml 与 bootstrap.yaml 的不同》
79 0
|
1月前
|
Java Spring 容器
Spring中@Autowired和@Resource注解异同点
Spring中@Autowired和@Resource注解异同点
31 0