Spring bean注解配置(1)
Spring自带的@Component注解及扩展@Repository、@Service、@Controller,如图

在使用注解方式配置bean时,需要引进一个包:

使用方法:
1、为需要使用注解方式的类添加注解标记
@Component("标识符")
POJO类
在类上使用@Component注解,表示该类定义为Spring管理Bean,使用默认value(可选)属性表示Bean标识符。如果不指定标识符,默认为首字母小写类名。例如类UserController的标识符为userController
2、在xml中配置自动扫描策略
<context:component-scan
base-package=""
resource-pattern="**/*.class"
name-generator="org.springframework.context.annotation.AnnotationBeanNameGenerator"
use-default-filters="true"
annotation-config="true">
<context:include-filter type="aspectj" expression=""/>
<context:exclude-filter type="regex" expression=""/>
</context:component-scan>
base-package:表示扫描注解类的开始位置,即将在指定的包中扫描,其他包中的注解类将不被扫描,默认将扫描所有类路径;
resource-pattern:表示扫描注解类的后缀匹配模式,即“base-package+resource-pattern”将组成匹配模式用于匹配类路径中的组件,默认后缀为“**/*.class”,即指定包下的所有以.class结尾的类文件;
name-generator:默认情况下的Bean标识符生成策略,默认是AnnotationBeanNameGenerator,其将生成以小写开头的类名(不包括包名);可以自定义自己的标识符生成策略;
use-default-filters:默认为true表示过滤@Component、@ManagedBean、@Named注解的类,如果改为false默认将不过滤这些默认的注解来定义Bean,即这些注解类不能被过滤到,即不能通过这些注解进行Bean定义;
annotation-config:表示是否自动支持注解实现Bean依赖注入,默认支持,如果设置为false,将关闭支持注解的依赖注入,需要通过<context:annotation-config/>开启。
<context:include-filter>:表示过滤到的类将被注册为Spring管理Bean。需要配合use-default-filters使用
<context:exclude-filter>:表示过滤到的类将不被注册为Spring管理Bean,它比<context:include-filter>具有更高优先级;
type:表示过滤器类型,目前支持注解类型、类类型、正则表达式、aspectj表达式过滤器,当然也可以自定义自己的过滤器,实现org.springframework.core.type.filter.TypeFilter即可;
expression:表示过滤器表达式。
下面为案例分析:
package com.proc.bean; import org.springframework.stereotype.Component; @Component
public class TestObject { }
TestObject
package com.proc.bean.Controller; import org.springframework.stereotype.Controller; @Controller
public class UserController { }
UserController
package com.proc.bean.repository;
public interface UserRepository {
void save();
}
UserRepository
package com.proc.bean.repository;
import org.springframework.stereotype.Repository;
@Repository("userRepository")
public class UserRepositoryImps implements UserRepository{
@Override
public void save() {
System.out.println("UserRepository save");
}
}
UserRepositoryImps
package com.proc.bean.service; import org.springframework.stereotype.Service; @Service
public class UserService { }
UserService
1、在xml中配置,通过base-package指定扫描指定包及其子包下所有类
<context:component-scan base-package="com.proc.bean"></context:component-scan>
测试输出
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
TestObject testObject=(TestObject) ctx.getBean("testObject");
System.out.println(testObject);
UserController userController=(UserController) ctx.getBean("userController");
System.out.println(userController);
UserService userService=(UserService) ctx.getBean("userService");
System.out.println(userService);
UserRepository userRepository=(UserRepository)ctx.getBean("userRepository");
System.out.println(userRepository);
输出结果:
com.proc.bean.TestObject@50d156
com.proc.bean.Controller.UserController@61c7e3
com.proc.bean.service.UserService@11d0846
com.proc.bean.repository.UserRepositoryImps@1e4c80f
2、指定resource-pattern:资源匹配,只扫描controller包下面的所有类
<context:component-scan base-package="com.proc.bean" resource-pattern="controller/*.class">
</context:component-scan>
这里是能够正确获取到com.proc.bean.Controller.UserController@61c7e3
3、 排除使用指定注解标签的类
<context:component-scan base-package="com.proc.bean">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
type:选择类型 annotation:注解标签、assignable:类名方式
这里能够正确获取到
com.proc.bean.TestObject@191f517
com.proc.bean.controller.UserController@5965f2
com.proc.bean.service.UserService@9bd883
4、排除指定标识符的类
<context:component-scan base-package="com.proc.bean">
<context:exclude-filter type="assignable" expression="com.proc.bean.controller.UserController"/>
</context:component-scan>
这里排除了com.proc.bean.controller.UserController类型的,所以只能够正确得到
com.proc.bean.TestObject@134517
com.proc.bean.service.UserService@50d156
com.proc.bean.repository.UserRepositoryImps@61c7e3
5、包含指定注解标记的类
<context:component-scan base-package="com.proc.bean" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
这里只能够正确得到
com.proc.bean.repository.UserRepositoryImps@1200884
在使用include-filter是需要配合use-default-filters="false",让自动扫描注解不使用默认的filter,而是使用我们指定的filter,否则将无效
6、包含指定类型的类
<context:component-scan base-package="com.proc.bean" use-default-filters="false">
<context:include-filter type="assignable" expression="com.proc.bean.TestObject"/>
</context:component-scan>
这里只能够正确得到
com.proc.bean.TestObject@17f23d9
最后提醒:
<context:include-filter/>和<context-exclude-filter/>标签可以使用多个
Spring bean注解配置(1)的更多相关文章
- JavaWeb_(Spring框架)注解配置
系列博文 JavaWeb_(Spring框架)xml配置文件 传送门 JavaWeb_(Spring框架)注解配置 传送门 Spring注解配置 a)导包和约束:基本包.aop包+context约束 ...
- spring aop注解配置
spring aop是面向切面编程,使用了动态代理的技术,这样可以使业务逻辑的代码不掺入其他乱七八糟的代码 可以在切面上实现合法性校验.权限检验.日志记录... spring aop 用的多的有两种配 ...
- java框架之Spring(2)-注解配置IOC&AOP配置
注解配置IoC 准备 1.要使用注解方式配置 IoC,除了之前引入的基础 jar 包,还需要引入 spring-aop 支持包,如下: 2.在 applicationContext.xml 中引入 c ...
- Spring自定义注解配置切面实现日志记录
一: spring-mvc.xml: <!--配置日志切面 start,必须与mvc配置在同一个配置文件,否则无法切入Controller层--><!-- 声明自动为spring容器 ...
- 学妹问的Spring Bean常用配置,我用最通俗易懂的讲解让她学会了
你好呀,我是沉默王二,一枚有趣的程序员,写的文章一直充满灵气,力求清新脱俗.昨天跑去王府井的小米店订购了一台小米 10,说是一周之内能到货,但我还是忍不住今天就想见到她.见我茶不思饭不想的,老婆就劝我 ...
- Spring纯注解配置
待改造的问题 我们发现,之所以我们现在离不开 xml 配置文件,是因为我们有一句很关键的配置: <!-- 告知spring框架在,读取配置文件,创建容器时,扫描注解,依据注解创建对象,并存入容器 ...
- springmvc学习指南 之---第25篇 Spring Bean有三种配置方式
writed by不要张艳涛, 从tomcat转到了springmvc 现在开始有点不知道该看什么书了,看完了springmvc 学习指南之后 又查了一些书,好多都是内容相近,在找书的过程之中,发现s ...
- Spring @Bean 注解的使用
使用说明 这个注解主要用在方法上,声明当前方法体中包含了最终产生 bean 实例的逻辑,方法的返回值是一个 Bean.这个 bean 会被 Spring 加入到容器中进行管理,默认情况下 bean 的 ...
- spring @bean注解
1.@bean注解用于注册一个bean到 到ioc容器中.类似于@component注解 2.@configure注解,相当于指明这个类是配置文件 3.@bean还可以指定initMethod,des ...
随机推荐
- ThinkPad 复刻计划 ThinkPad Time Machine
在快节奏的高科技市场中,针对性的进化 ThinkPad 的设计几乎是闻所未闻的.在汽车行业,保时捷无疑干的不错,但我不认为有任何其他的电脑公司可以顶住压力,坚持自己的初心这么久.没有任何一个竞争对手可 ...
- python使用opencv驱动摄像头
#coding:utf-8 import cv2 import sys from PIL import Image def CatchUsbVideo(window_name, camera_idx) ...
- OC基础:OC 基本数据类型与对象之间的转换方法 分类: ios学习 OC 2015-06-18 20:01 11人阅读 评论(0) 收藏
1.Foundation框架中提供了很多的集合类如:NSArray,NSMutableArray,NSSet,NSMutableSet,NSDictionary,NSMutableDictionary ...
- Windows下C++删除清除map
清除单map(非嵌套map) #include<map> #include<string> #include<iostream> using namespace s ...
- 代理模式及Spring AOP (二)
一.Spring AOP 1.1 Spring AOP 底层还是用的动态代理.如果目标对象所对应的类有接口,spring就用jdk生成代理对象: 如果目标对象所对应的类没有接口,spring就用C ...
- .NET 之 垃圾回收机制GC
一.GC的必要性 1.应用程序对资源操作,通常简单分为以下几个步骤:为对应的资源分配内存 → 初始化内存 → 使用资源 → 清理资源 → 释放内存. 2.应用程序对资源(内存使用)管理的方式,常见的一 ...
- SQLite数据库学习小结——native层实现
1. SQlite概述 SQLite是一款轻量.快速.跨平台的嵌入式数据库,是遵守ACID(注:ACID指数据库事务正确执行的四个基本要素的缩写.包含:原子性(Atomicity).一致性(Consi ...
- ballerina 学习 三十 扩展开发(一)
ballerina 主要是分为两大类 基于ballerina 语言开发的,一般是客户端的connector 使用java语言开发的(类似的基于jvm的都可以),一般是注解以及进行构件生成 baller ...
- dbt 包管理
dbt 可以方便的支持基于git 的包管理 依赖申明 位置 dbt_project.yml 中的repositories 或者使用packages.yaml 格式 dbt_project.yml: r ...
- js 验证代码部分的简单实现
接上面的文章. 我们已经简单的设计了关于如何进行处理了,但是如何进行校验呢,代码也是比较简单的因为我们使用的是asp.net 简单并且功能强大. 我们同样使用的是HttpResponse,简单的模拟代 ...