在 classpath 中扫描组件:

当在组件类上使用了特定的注解之后 , 还需要在 Spring 的配置文件中声明 <context:component-scan>:

  • base-package 属性指定一个需要扫描的基类包 , Spring 容器将会扫描这个基类包里及其子包中的所有类。
  • 当需要扫描多个包时 , 可以使用逗号分隔。
  • 如果仅希望扫描特定的类而非基包下的所有类 , 可以使用 resource-pattern 属性过滤特定的类。
  • <context:include-filter> 子节点表示要包含的目标类。
  • <context:exclude-filter> 子节点表示要排除在外的目标类。
  • <context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点。

<context:include-filter> 和 <context:exclude-filter> 子节点支持多种类型的过滤表达式:

类别

示例

说明

annotation

com.xxx.XxxAnnotation

所有标注 XxxAnnotation 的类。该类型采用目标类是否标注某个注解进行过滤

assinable

com.xxx.XxxService

所有继承或扩展 XxxService 的类。该类型采用目标类是否继承或扩展某个特定类进行过滤

aspectj

com.xxx..*Service+

所有类名以 Service 结束的类及继承或者扩展他们的类。该类型采用 AspectJ 表达式进行过滤

regex

com.\xxx\.anno\..*

所有 com.xxx.anno 包下的类。该类型采用正则表达式根据类的类名进行过滤

custom

com.xxx.XxxTypeFilter

采用 XxxTypeFilter 通过代码的方式定义过滤规则。该类必须实现 org.springframework.core.type.TypeFilter 接口

resource-pattern 属性过滤:

 <?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"
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"> <!-- 指定 Spring IOC 容器扫描的包-->
<!--
resource-pattern="repository/*.class"
只扫描repository下的所有类
-->
<context:component-scan base-package="com.itdjx.spring.annotation" resource-pattern="repository/*.class"> </context:component-scan> </beans>
 package com.itdjx.spring.annotation;

 import com.itdjx.spring.annotation.controller.UserController;
import com.itdjx.spring.annotation.repository.UserRepository;
import com.itdjx.spring.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:34
*/
public class Main { public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject testObject = (TestObject) cxt.getBean("testObject");
System.out.println(testObject); UserController userController = (UserController) cxt.getBean("userController");
System.out.println(userController); UserService userService = (UserService) cxt.getBean("userService");
System.out.println(userService); UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
System.out.println(userRepository); }
}

控制台输出:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testObject' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at com.itdjx.spring.annotation.Main.main(Main.java:17)

原因:配置文件中添加 resource-pattern="repository/*.class" 属性 , IOC 容器只扫描 repository 包下的类 , IOC 容器中只有 id 为 userRepository 的 bean 节点。

 package com.itdjx.spring.annotation;

 import com.itdjx.spring.annotation.controller.UserController;
import com.itdjx.spring.annotation.repository.UserRepository;
import com.itdjx.spring.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:34
*/
public class Main { public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml"); UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
System.out.println(userRepository); }
}

控制台输出:

com.itdjx.spring.annotation.repository.UserRepositoryImpl@1e153c5

<context:execlude-filter> 子节点:

  • annotation 属性:
 <?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"
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"> <!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.itdjx.spring.annotation">
<!-- 排除包含(不包含) org.springframework.stereotype.Repository 的组件 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan> </beans>
 package com.itdjx.spring.annotation;

 import com.itdjx.spring.annotation.controller.UserController;
import com.itdjx.spring.annotation.repository.UserRepository;
import com.itdjx.spring.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:34
*/
public class Main { public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml");
TestObject testObject = (TestObject) cxt.getBean("testObject");
System.out.println(testObject); UserController userController = (UserController) cxt.getBean("userController");
System.out.println(userController); UserService userService = (UserService) cxt.getBean("userService");
System.out.println(userService); UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
System.out.println(userRepository); }
}

控制台输出:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userRepository' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at com.itdjx.spring.annotation.Main.main(Main.java:26)
com.itdjx.spring.annotation.TestObject@13f29ff
com.itdjx.spring.annotation.controller.UserController@1e8c3cb
com.itdjx.spring.annotation.service.UserService@3f38c4

原因:IOC 容器中不包含 org.springframework.stereotype.Repository 的组件 , 也就是 IOC 容器中没有 id 为 userRepository 的 bean 节点。

  • assignable 属性:
 <?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"
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"> <!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.itdjx.spring.annotation">
<!-- 排除(不包含) com.itdjx.spring.annotation.repository.UserRepository 以及他的实现类 -->
<context:exclude-filter type="assignable" expression="com.itdjx.spring.annotation.repository.UserRepository"/>
</context:component-scan> </beans>

控制台输出:

com.itdjx.spring.annotation.TestObject@422e18
com.itdjx.spring.annotation.controller.UserController@17159c0
com.itdjx.spring.annotation.service.UserService@27ba32
Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'userRepository' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at com.itdjx.spring.annotation.Main.main(Main.java:26)

原因:IOC 容器中没有 id 为 userRepository 的 bean 节点。

<context:include-filter> 子节点:

  • annotation 属性:
 <?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"
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"> <!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.itdjx.spring.annotation">
<!-- 包含 org.springframework.stereotype.Repository 的组件 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan> </beans>

控制台输出:

com.itdjx.spring.annotation.TestObject@27ba32
com.itdjx.spring.annotation.controller.UserController@f82753
com.itdjx.spring.annotation.service.UserService@10fe47a
com.itdjx.spring.annotation.repository.UserRepositoryImpl@2b0582

<context:include-filter> 子节点没有生效:该子节点需要和 use-default-filters 属性配合使用。

 <?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"
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"> <!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.itdjx.spring.annotation" use-default-filters="false">
<!-- 包含 org.springframework.stereotype.Repository 的组件 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan> </beans>

控制台输出:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testObject' available
at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:687)
at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1207)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:284)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)
at com.itdjx.spring.annotation.Main.main(Main.java:17)

原因:IOC 容器中只存在 id 为 userRepository 的 bean 节点 。

 package com.itdjx.spring.annotation;

 import com.itdjx.spring.annotation.controller.UserController;
import com.itdjx.spring.annotation.repository.UserRepository;
import com.itdjx.spring.annotation.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author Wáng Chéng Dá
* @create 2017-03-03 9:34
*/
public class Main { public static void main(String[] args) {
ApplicationContext cxt = new ClassPathXmlApplicationContext("beans-annotation.xml"); UserRepository userRepository = (UserRepository) cxt.getBean("userRepository");
System.out.println(userRepository); }
}

控制台输出:

com.itdjx.spring.annotation.repository.UserRepositoryImpl@1406a1
  • assignable 属性:
 <?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"
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"> <!-- 指定 Spring IOC 容器扫描的包 -->
<context:component-scan base-package="com.itdjx.spring.annotation" use-default-filters="false">
<!-- 包含 com.itdjx.spring.annotation.repository.UserRepository 以及他的实现类 -->
<context:include-filter type="assignable" expression="com.itdjx.spring.annotation.repository.UserRepository"/>
</context:component-scan> </beans>

控制台输出:

com.itdjx.spring.annotation.repository.UserRepositoryImpl@af004b

Spring学习--通过注解配置 Bean (二)的更多相关文章

  1. Spring学习--通过注解配置 Bean (三)

    组件装配: <context:component-sacan> 元素还会自动注册 AutowiredAnnotationBeanPostProcesser 实例 , 该实例可以自动装配具有 ...

  2. Spring学习--通过注解配置 Bean (一)

    在 classpath 中扫描组件: 组件扫描(component scanning): Spring 能够从 classpath 下自动扫描 , 侦测和实例化具有特定注解的组件. 特定组件包括: @ ...

  3. Spring学习之xml配置Bean总结

    学习Spring时,我用的是Maven来管理jar包,先看看maven的pom.xml: pom.xml <project xmlns="http://maven.apache.org ...

  4. [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring(二)--FactoryBean、bean的后置处理器、数据库连接池、引用外部文件、使用注解配置bean等

    实验1:配置通过静态工厂方法创建的bean  [通过静态方法提供实例对象,工厂类本身不需要实例化!] 1.创建静态工厂类 public class StaticFactory { private st ...

  6. Spring框架学习之注解配置与AOP思想

         上篇我们介绍了Spring中有关高级依赖关系配置的内容,也可以调用任意方法的返回值作为属性注入的值,它解决了Spring配置文件的动态性不足的缺点.而本篇,我们将介绍Spring的又一大核心 ...

  7. Spring IOC机制之使用注解配置bean

    一. 通过注解配置bean 1.1       概述 相对于XML方式而言,通过注解的方式配置bean更加简洁和优雅,而且和MVC组件化开发的理念十分契合,是开发中常用的使用方式. 1.2       ...

  8. Spring框架入门之基于Java注解配置bean

    Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...

  9. Spring学习(七)bean装配详解之 【通过注解装配 Bean】【自动装配的歧义解决】

    自动装配 1.歧义性 我们知道用@Autowired可以对bean进行注入(按照type注入),但如果有两个相同类型的bean在IOC容器中注册了,要怎么去区分对哪一个Bean进行注入呢? 如下情况, ...

随机推荐

  1. LEAVE TO LIST-PROCESSING

    LEAVE SCREEN 現在の画面の処理を中止し.次の画面を呼び出す - LEAVE TO SCREEN 現在の画面の処理を中止し.動的に定義された次の画面を呼び出す - LEAVE [PROGRA ...

  2. 新版IdFTP解决中文乱码问题

    用XE10后开发FTP客户端,发现有中文乱码问题.这里也主要是编码的问题,在connect链接后,需要设置编码方可. 注意:  IndyTextEncoding_OSDefault;   该代码可能需 ...

  3. ArrayMap java.lang.ArrayIndexOutOfBoundsException

    错误堆栈: java.lang.ArrayIndexOutOfBoundsException: length=0; index=1 at android.support.v4.util.SimpleA ...

  4. 美年健康股票成交量和K线关系

    看下美年健康的股票,这次主要是研究下成交量和K线的关系,以最后5天为例子,股票下跌成交量降低,说明抛压很小,在最后3天,价格突破的时候,成交量是平时的两倍,说明有机构买入, 业绩部分还可以,全民健身是 ...

  5. Android各版本代号、版本号、API/NDK级别、发布时间

    代号 版本号 API/NDK级别 发布时间 牛轧糖 Nougat 7.1.2 API level 25 2017-2 7.1.1 2016-10 7.0 API level 24 2016-05 棉花 ...

  6. swagger webapi控制器注释不显示

    swagger是webapi文档描述及调试工具,要在asp.net mvc中使用swagger,需要安装Swashbuckle.Core这个包,安装好后会在app_start中生成SwaggerCon ...

  7. CWindowWnd类源码分析

    CWindowWnd代码在UIBase.h和UIBase.cpp文件里.主要实现的是一个基本窗口的创建与消息处理. 相关代码: 头文件: class UILIB_API CWindowWnd { pu ...

  8. Leetcode. 回文字符串的分割和最少分割数

    Q1: 回文字符串的分割 Given a string s, partition s such that every substring of the partition is a palindrom ...

  9. [转]Hibernate入门:批量插入数据

    转自:http://blog.csdn.net/xiazdong/article/details/7709068 一般如果要插入100万条数据,则会写如下代码: package org.xiazdon ...

  10. Android Studio 添加模块依赖

    原文地址: http://fanjiajia.cn/2018/09/27/Android%20Studio%20%E6%B7%BB%E5%8A%A0%E6%A8%A1%E5%9D%97%E4%BE%9 ...