自动组件扫描

启用Spring组件扫描功能。
使用@Component注释来表示这是类是一个自动扫描组件。 
package com.tanlei.dao;

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository; @Component或者是@Repository
public class CustomerDao { @Override
public String toString() {
// TODO Auto-generated method stub
return "hello ,this is CustomerDao";
}
}
package com.tanlei.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import com.tanlei.dao.CustomerDao; @Component或是@Service
public class CustomerService { @Autowired
CustomerDao customerDao; @Override
public String toString() {
// TODO Auto-generated method stub
return "CustomerService [customerDAO=" + customerDao + "]";
}
}

将这个“context:component”在bean配置文件,这意味着,在 Spring 中启用自动扫描功能。base-package 是指明存储组件,Spring将扫描该文件夹,并找出Bean(注解为@Component)并注册到 Spring 容器。

扫描指定的资源:

resource-pattern="service/*.class"

<?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-4.0.xsd"> <context:component-scan base-package="com.tanlei.service"></context:component-scan>
<context:component-scan base-package="com.tanlei.dao"></context:component-scan> </beans>

执行

package com.tanlei.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tanlei.service.CustomerService; public class App { public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "beans-spring.xml" }); CustomerService cust = (CustomerService) context.getBean("customerService");
System.out.println(cust);
} }

结果

自定义自动扫描组件名称

要创建组件的自定义名称,你可以这样自定义名称:

@Service("AAA")
public class CustomerService
...

现在,可以用'AAA'这个名称进行检索。

CustomerService cust = (CustomerService)context.getBean("AAA");

自动组件扫描注释类型

在Spring2.5中,有4种类型的组件自动扫描注释类型
  • @Component – 指示自动扫描组件。
  • @Repository – 表示在持久层DAO组件。
  • @Service – 表示在业务层服务组件。
  • @Controller – 表示在表示层控制器组件。

因此,使用哪一个?其实并不那么重要。参见 @Repository,@Service 或 @Controller 源代码。

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Repository { String value() default ""; }

你可能会发现,所有的 @Repository, @Service 或 @Controller 被注解为 @Component。因此,我们可以只使用 @Component 对所有组件进行自动扫描?是的,Spring会自动扫描所有组件的 @Component 注解。

它工作正常,但不是一个好的做法,为便于阅读,应该始终声明@Repository,@ Service 或 @Controller 在指定的层,使你的代码更易于阅读,
 
 
 
 
<

context:exclude-filter

>  子节点指定排除哪些指定表达式的组件


<

context:include-filter
子节点指定包含哪些表达式的组件,该子节点需要use-default-filters="true"配合使用

>

1.过滤组件 - 包含

<context:component-scan base-package="com.tanlei">
<context:include-filter type="regex"
expression="com.tanlei.dao.*DAO.*" /> <context:include-filter type="regex"
expression="com.tanlei.service.*Service.*" />
</context:component-scan>

2.过滤组件 - 不包含

另外,您还可以排除指定组件,以避免 Spring 检测和 Spring 容器注册。不包括在这些文件中标注有 @Service 。
 
<context:component-scan base-package="com.yiibai.customer"   >
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Service" />
</context:component-scan>

不包括那些包含DAO这个词组文件名。

<context:component-scan base-package="com.yiibai" >
<context:exclude-filter type="regex"
expression="com.yiibai.customer.dao.*DAO.*" />
</context:component-scan>

注意事项

找不到bean可以使用   @Autowired(required=false)

IOC有相同类型的bean   @Repository("bean的名字")

            @Qualifier("指定一个名字")

            setter方法

Spring_自动组件扫描和 基于注解配置bean的更多相关文章

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

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

  2. 13Spring通过注解配置Bean(1)

    配置Bean的形式:基于XML文件的方式:基于注解的方式(基于注解配置Bean:基于注解来装配Bean的属性) 下面介绍基于注解的方式来配置Bean. ——组件扫描(component scannin ...

  3. Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式

     本节主要内容:    1. 给MessageBean注入参数值    2. 测试Spring自动组件扫描方式    3. 如何控制ExampleBean实例化方式    4. 使用注解方式重构Jdb ...

  4. 基于注解配置spring

    1 对 bean 的标注基于注解方式有3个注解 @Component @Repository 对DAO类进行标注 @Service 对Service类进行标注 @Controller  对Contro ...

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

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

  6. 基于注解的bean配置

    基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...

  7. spring(读取外部数据库配置信息、基于注解管理bean、DI)

    ###解析外部配置文件在resources文件夹下,新建db.properties(和数据库连接相关的信息) driverClassName=com.mysql.jdbc.Driverurl=jdbc ...

  8. Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用

    Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...

  9. IDEA02 利用Maven创建Web项目、为Web应用添加Spring框架支持、bean的创建于获取、利用注解配置Bean、自动装配Bean、MVC配置

    1 环境版本说明 Jdk : 1.8 Maven : 3.5 IDEA : 专业版 2017.2 2 环境准备 2.1 Maven安装及其配置 2.2 Tomcat安装及其配置 3 详细步骤 3.1 ...

随机推荐

  1. Python快速搭建HTTP服务器

        <wiz_tmp_tag id="wiz-table-range-border" contenteditable="false" style=&q ...

  2. HZOI2019熟练剖分(tree)

    题目大意:https://www.cnblogs.com/Juve/articles/11186805.html 题解: 先给出官方题解: 其实这题跟期望没什么关系,因为E=$\sum_\limits ...

  3. 双系统可以进入Windows但进入Ubuntu时无法进入系统引导,只有左上角光标闪

    双系统可以进入Windows但进入Ubuntu时无法进入系统引导,只有左上角光标闪 这时候可以进入windows下的easyBCD重新创建ubuntu引导项即可

  4. JavaSE_04_JDK1.8新特性Lambda表达式

    1.1体验Lambda的更优写法 借助Java 8的全新语法,上述Runnable接口的匿名内部类写法可以通过更简单的Lambda表达式达到等效: 1.2 Lambda标准格式 Lambda省去面向对 ...

  5. 在ubuntu中安装phpstorm

    安装参考网址 https://blog.csdn.net/mrgong_/article/details/77200225 注意:不需要安装java相关 激活phpstorm 2018 参考网址:ht ...

  6. jeecms框架单点登录功能的实现

    单点登录的功能实现主要原理: 1: 在点击登录按钮的时候使用reponse.addCookie()方法向浏览器发送cookie: 2: 在前段拦截器中的request.getCookie()在接收到设 ...

  7. JasperReports报表组15

    组在JasperReports的协助组织对报告的数据以逻辑方式.报告组代表连续记录的数据源中有一些共同点,比如某个报表字段的值的序列.报告组由<group>元素定义.一个报表可以有任意数量 ...

  8. c/c++输出保留小数

    c语言中,用print可以有格式符号,例如想让a保留两位小数 float a; print( "%.2f", a); 注意这里如果a是0.1, 那么打印出来会自动补0,也就是结果显 ...

  9. 读取复杂结构的yml配置项

    1.yml配置项示例:(List的集合在第一项前面加 “-”) rabbitmqsetting: exchangeList: - name: e1 type: topic bindingList: - ...

  10. 威胁快报|首爆新型ibus蠕虫,利用热门漏洞疯狂挖矿牟利

    一.背景 近日阿里云安全团队发现了一起利用多个流行漏洞传播的蠕虫事件.黑客首先利用ThinkPHP远程命令执行等多个热门漏洞控制大量主机,并将其中一台“肉鸡”作为蠕虫脚本的下载源.其余受控主机下载并运 ...