通过@Autowired或@Resource来实现在Bean中自动注入的功能,但还要在配置文件中写Bean定义,下面我们将介绍如何注解Bean,从而从XML配置文件 中完全移除Bean定义的配置。

1. @Component(不推荐使用)、@Repository、@Service、@Controller 
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:

Java 代码
  1. @Component
  2. public   class  UserDaoImpl  extends  HibernateDaoSupport  implements  UserDao {
  3. ...
  4. }

使用@Component注解定义的Bean,默认的名称(id)是小写开头的非限定类名。如这里定义的Bean名称就是 userDaoImpl。你也可以指定Bean的名称: 
@Component("userDao") 
@Component是所有受Spring管理组件的通用形式,Spring还提供了更加细化的注解形式:@Repository、 @Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展示层Bean。目前版本(2.5)中,这些注解与 @Component的语义是一样的,完全通用,在Spring以后的版本中可能会给它们追加更多的语义。所以,我们推荐使用@Repository、 @Service、@Controller来替代@Component。

2. 使用<context:component-scan />让Bean定义注解工作起来

Java 代码
  1. <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"
  2. xsi:schemaLocation="http://www.springframework.org/schema/beans
  3. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  4. http://www.springframework.org/schema/context
  5. http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  6. <context:component-scan base-package = "com.kedacom.ksoa"  />
  7. </beans>

这里,所有通过<bean>元素定义Bean的配置内容已经被移除,仅需要添加一行<context:component- scan />配置就解决所有问题了——Spring XML配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan />的base-package属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。 
<context:component-scan />还允许定义过滤器将基包下的某些类纳入或排除。Spring支持以下4种类型的过滤方式:

  • 过滤器类型 表达式范例 说明
  • 注解 org.example.SomeAnnotation 将所有使用SomeAnnotation注解的类过滤出来
  • 类名指定 org.example.SomeClass 过滤指定的类
  • 正则表达式 com/.kedacom/.spring/.annotation/.web/..* 通过正则表达式过滤一些类
  • AspectJ表达式 org.example..*Service+ 通过AspectJ表达式过滤一些类

以正则表达式为例,我列举一个应用实例:

Java 代码
  1. <context:component-scan base- package = "com.casheen.spring.annotation" >
  2. <context:exclude-filter type="regex"  expression= "com/.casheen/.spring/.annotation/.web/..*"  />
  3. </context:component-scan>

值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor), 因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了。

3. 使用@Scope来定义Bean的作用范围 
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完 成这项工作:

Java 代码
    1. @Scope ( "session" )
    2. @Component ()
    3. public   class  UserSessionBean  implements  Serializable {
    4. ...
    5. }

Spring自动注入Bean的更多相关文章

  1. Spring 自动装配 Bean

    Spring3系列8- Spring 自动装配 Bean 1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiri ...

  2. Spring自动装配Bean详解

    1.      Auto-Wiring ‘no’ 2.      Auto-Wiring ‘byName’ 3.      Auto-Wiring ‘byType 4.      Auto-Wirin ...

  3. Spring自动装配bean

    Spring推荐面向接口编程,这样可以很好的解耦具体的实现类. CompactDisc.class 文件: public interface CompactDisc { void play(); } ...

  4. spring自动注入的三种方式

    所谓spring自动注入,是指容器中的一个组件中需要用到另一个组件(例如聚合关系)时,依靠spring容器创建对象,而不是手动创建,主要有三种方式: 1. @Autowired注解——由spring提 ...

  5. Spring自动注入properties文件

    实现spring 自动注入属性文件中的key-value. 1.在applicationContext.xml配置文件中,引入<util />命名空间. xmlns:util=" ...

  6. 解决Spring+Quartz无法自动注入bean问题

    问题 我们有时需要执行一些定时任务(如数据批处理),比较常用的技术框架有Spring + Quartz中.无奈此方式有个问题:Spring Bean无法自动注入. 环境:Spring3.2.2 + Q ...

  7. Spring 注解Autowired自动注入bean异常解决

      错误: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'xx' is defined ...

  8. Spring自动注入之@Autowired、@Resource、@Inject

    相同点: 三者都支持对spring bean的自动注入 不同点: ①Autowired按照类型进行注入( Bean bean = applicationContext.getBean(Bean.cla ...

  9. Spring自动注入有关的注解

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. 1,@Comp ...

随机推荐

  1. windows MinGW gcc 编译乱码问题

    问题描述 一般很多编辑器默认都是保存成utf-8文件,然而在输出中文的时候出现了乱码?另外试了其他方法,有的乱码,有的不乱? MinGW gcc 编译 utf-8 文件的时候乱码 MinGW gcc ...

  2. MySQL的sql语言分类DML、DQL、DDL、DCL

    SQL语言一共分为4大类:数据定义语言DDL,数据操纵语言DML,数据查询语言DQL,数据控制语言DCL 1.数据定义语言DDL(Data Definition Language) 对象: 数据库和表 ...

  3. WUSTOJ 1279: Wallace and His Pet(Java)

    1279: Wallace and His Pet 题目   给出一句话(英文),单词总数不超过1000,每个单词不超过10个字符,一句话只有一个唯一的字符"."(句点).将这句话 ...

  4. mysql查看表结构命令,如下:

    desc 表名; show columns from 表名; describe 表名; show create table 表名;

  5. 调整统计信息JOB采样时间

    一.需求说明 Oracle数据库中存在定时JOB,自动执行收集统计信息的程序.但是对于7*24小时系统来说,Oracle配置的定时收集时间不太合理,需要人为调整.本篇博客就是基于这种需求,调整JOB采 ...

  6. Mariadb/Mysql 主从复制(1)

    一.原理 mysql的主从数据同步是一个异步复制过程,需要master开启bin-log日志功能,bin-log记录了master库中的增.删.修改.更新操作的sql语句,整个过程需要开启3个线程,分 ...

  7. 本地虚拟机NAT模式下怎么设置才可以访问外网

    记:因为我要在本机虚拟机上安装Docker,结果发现虚拟机环境不能上网,是主机模式.我要调成net模式下才可以访问外网,这就需要怎么设置.下面文章记录一下. 在本机安装VMware软件后,系统中会自动 ...

  8. Uwl.Admin开源框架(二)

    Uwl.Admin开源框架基于QuartzNet定时任务模块的实现 Quartz.NET官网地址:https://www.quartz-scheduler.net/ Quartz.NET文档地址:ht ...

  9. aspnet core 全局模型验证,统一api响应

    上手就来 新建一个模型验证过滤器,其中ApiResp是自定义的统一响应类. public class VldFilter:IActionFilter { /// <summary> /// ...

  10. SVN配置使用及移植

    使用svn作为配置管理工具及其普遍的用于项目开发中,网上有很多关于svn的原理介绍及命令行管理教程.这里仅仅分享下个人配置及使用的过程,不通过命令行,可简单的上手操作.如有遗漏欢迎留言交流. 配置及使 ...