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 ...
随机推荐
- php冒泡算法
1.冒泡算法 网上搜了很多,但是总是对于每次循环的边界值思路讲的比较笼统. 不是很容易被新手记住,我自己平时也是硬记下来的. 但是对于算法,硬记,时间长了还是容易忘记,所以自己写了一次,把每次思路尽量 ...
- SQL Server中存储过程与函数的区别
本质上没区别.只是函数有如:只能返回一个变量的限制.而存储过程可以返回多个.而函数是可以嵌入在sql中使用的,可以在select中调用,而存储过程不行.执行的本质都一样. 函数限制比较多,比如不能用临 ...
- Java通过接口或者抽象类调用方法的时候,怎么知道调用的是哪个实现类里的方法?
用对象的 getClass() 方法获得它的类,之后就可以随意去判断这是哪个实现类了. 比如代码1-1所示的JDBC连接池的代码,我们想要知道conn调用的close方法是释放连接还是归还连接, 我们 ...
- Cesium 1.51新功能评测
前言 之前介绍Cesium1.50版本的新功能时,很多人把1.50写成1.5.这两个版本可不一样,之间差了45个小版本号,1.5版本大概是Cesium三年前的版本了. Cesium每月月初的第一个工作 ...
- Junit5的依赖添加及RunWith(SpringJUnit4ClassRunner.class)注解使用
首先Junit5依赖应该配置为 <dependency> <groupId>org.junit.jupiter</groupId> <artifactId&g ...
- jmeter参数化之配置元件CSV控件
1. 用badboby进行录制,录制完成后保存,用JMeter格式进行保存,如:登陆.jmx 2. 在jmeter中打开保存的文件登陆.jmx. 3. 对登陆账号和密码进行参数 ...
- 2.快速创建springboot项目 连pom文件里面的配置都不用配了
无论是创建项目 还是module 模块 选择这个 .然后在后面的选择中选择自己要的功能 就可以把相关的依赖都加进去 省去了依赖 其后的写法跟第一篇一样 在这个项目下面有一个配置文件 ====>a ...
- JasperReport生命周期3
JasperReports的主要目的是为了在一个简单而灵活的方式创建页面为导向,准备好打印文档.下面的流程图描述了一个典型的工作流程,同时创建报表. 如在图片的生命周期具有以下明显的阶段 设计报表在这 ...
- Sql Server实现自动增长
在学习中遇到这个问题 数据库里有编号字段 BH00001 BH00002 BH00003 BH00004 如何实现自动增长 --下面的代码生成长度为8的编号,编号以BH开头,其余6位为流水号. --得 ...
- Hackerrank--Savita And Friends(最小直径生成树MDST)
题目链接 After completing her final semester, Savita is back home. She is excited to meet all her friend ...