Spring_自动组件扫描和 基于注解配置bean
自动组件扫描
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");
自动组件扫描注释类型
- @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 注解。
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.过滤组件 - 不包含
<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的更多相关文章
- [原创]java WEB学习笔记103:Spring学习---Spring Bean配置:基于注解的方式(基于注解配置bean,基于注解来装配bean的属性)
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- 13Spring通过注解配置Bean(1)
配置Bean的形式:基于XML文件的方式:基于注解的方式(基于注解配置Bean:基于注解来装配Bean的属性) 下面介绍基于注解的方式来配置Bean. ——组件扫描(component scannin ...
- Spring学习笔记之 Spring IOC容器(二) 之注入参数值,自动组件扫描方式,控制Bean实例化方式,使用注解方式
本节主要内容: 1. 给MessageBean注入参数值 2. 测试Spring自动组件扫描方式 3. 如何控制ExampleBean实例化方式 4. 使用注解方式重构Jdb ...
- 基于注解配置spring
1 对 bean 的标注基于注解方式有3个注解 @Component @Repository 对DAO类进行标注 @Service 对Service类进行标注 @Controller 对Contro ...
- Spring框架入门之基于Java注解配置bean
Spring框架入门之基于Java注解配置bean 一.Spring bean配置常用的注解 常用的有四个注解 Controller: 用于控制器的注解 Service : 用于service的注解 ...
- 基于注解的bean配置
基于注解的bean配置,主要是进行applicationContext.xml配置.DAO层类注解.Service层类注解. 1.在applicationContext.xml文件中配置信息如下 &l ...
- spring(读取外部数据库配置信息、基于注解管理bean、DI)
###解析外部配置文件在resources文件夹下,新建db.properties(和数据库连接相关的信息) driverClassName=com.mysql.jdbc.Driverurl=jdbc ...
- Unit03: Spring Web MVC简介 、 基于XML配置的MVC应用 、 基于注解配置的MVC应用
Unit03: Spring Web MVC简介 . 基于XML配置的MVC应用 . 基于注解配置的MVC应用 springmvc (1)springmvc是什么? 是一个mvc框架,用来简化基于mv ...
- 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 ...
随机推荐
- 程序员总数3W+,阿里巴巴首次公开2018代码数据报告
- js的深复制与浅复制
什么是深复制和浅复制? 深复制和浅复制的概念只存在于对象array和数组obj上. 浅复制是:模糊复制,就是不管对方是字符串类型还是引用类型都通通复制过来.结果两个变量的内容会同时变化. 深复制是:有 ...
- IIS发布web应用程序之再折腾
最近几个月发布程序比较多,遇到了各种IIS发布web程序后无法访问的问题.原以为对各种问题都已经摸的差不多了,但今天又为一问题折腾了大半天.具体过程祥记如下: 在server2008 R2 64位系统 ...
- pycharm professional 2019版长效激活
PyCharm是一种Python IDE,带有一整套可以帮助用户在使用Python语言开发时提高其效率的工具,比如调试.语法高亮.Project管理.代码跳转.智能提示.自动完成.单元测试.版本控制. ...
- python学习之路-day1
1 变量 赋值:变量可以是字符串.序列.元组. # author:hams.ali # 界面 line = '-*'*20 # 数字直接可以计算 _var1 = ' # 字符变量拼接 _var_2 = ...
- Python爬虫笔记【一】模拟用户访问之提交表单登入—第二次(7)
在第一次登入时遇到这个问题,页面验证码与下载下来需要识别的验证码不同的问题,从网上查寻说是叫验证码同步问题.发现是用cookie解决的,那次cookie介绍到通过cookie就可以实现时间戳同步问题, ...
- NGINX模块开发 之 验证URL參数
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/RoyalApex/article/details/26404379 作者:邹祁峰 邮箱:Qifeng ...
- 第15章 RMAN备份
第15章 RMAN备份 oracle推荐的备份工具是rman(恢复管理器:recovery manager),用操作系统命令执行的备份被称为用户管理的备份.使用rman执行的备份被称为服务器管理备份. ...
- 全景还原报错现场 | 应用实时监控 ARMS 上线用户行为回溯功能
随着前端技术日新月异迅猛发展,为了实现更好的前端性能,最大程度提高用户体验,支持单页应用的框架逐渐占领市场,如众所周知的React,Vue等等.但是在单页应用的趋势下,快速定位并解决JS错误却成为一大 ...
- 【html、CSS、javascript-2】CSS基础
CSS CSS是Cascading Style Sheets的简称,中文称为层叠样式表,用来控制网页数据的表现,可以使网页的表现与数据内容分离. 一 css的四种引入方式 1.行内式 ...