使用Spring注解完成Bean的定义

2010-04-21 16:48:54|  分类: spring|举报|字号 订阅

 
 
通过@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-Context之四:Spring容器及bean的定义

    Spring框架的核心功能之一就是控制反转(Inversion of Control, IoC),也叫做依赖注入(dependency injection, DI).关于依赖注入的具体内容可以参见Ma ...

  2. 在Servlet中获取Spring注解的bean

    最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...

  3. Spring 注解配置Bean

    一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...

  4. 单元测试使用spring注解获取bean

    在实际项目开发中经常会有单元测试,单元测试中经常会用类似这样的代码片段获取spring管理的bean @Test public void testSendEmail(){ MessageService ...

  5. 学习 Spring (八) 注解之 Bean 的定义及作用域

    Spring入门篇 学习笔记 Classpath 扫描与组件管理 从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 be ...

  6. 5.spring:注解配置 Bean

    在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...

  7. 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式

    bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...

  8. spring 注解注入bean

    通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...

  9. [转]Spring 注解总结

    原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...

随机推荐

  1. MyEclipse解决SVN同步冲突问题conflict in the working copy obstructs the current operation

    服务端版本控制软件subversion,客户端是eclipse的插件subclipse.当删除一个东西的时候老是提示错误,说冲突 commit -m "" C:/Users/Adm ...

  2. 用PHP和Ajax进行前后台数据交互——以用户登录为例

    很多网站中都有用户登录系统,要完成用户的注册和登陆,就一定要用到前后台的数据交互.在这里以简单的用户注册和登陆为例介绍一下前后台交互的大致流程. 首先,我们来做一个简单的登陆界面. 这里为了方便我使用 ...

  3. NetCore WebSocket 即时通讯示例

    1.新建Netcore Web项目 2.创建简易通讯协议 public class MsgTemplate { public string SenderID { get; set; } public ...

  4. springmvc 之 DispatcherServlet

    DispatcherServlet说明 使用Spring MVC,配置DispatcherServlet是第一步. DispatcherServlet是一个Servlet,所以可以配置多个Dispat ...

  5. JavaScript练习笔记整理·2 - 6.24

      Codewars地址:https://www.codewars.com/ 欢迎和大家一起来讨论~   基础练习(1):   我的解答为: function isIsogram(str){ if(s ...

  6. Sql_Case_When用法

    http://wenku.baidu.com/link?url=XBnkUzGtiJFhTnQk5HbmdgndhVEYJdcfDEhSEIFeTRn9-41KMLf_49wKiydNCF-4g3Qi ...

  7. Linux: 安装NVIDIA显卡驱动

    Linux(Fedora25, 64bit)台式机配备了NVIDIA显卡GTX950,但是仅仅使用开源驱动nouveau,无法发挥NVIDIA显卡的性能,所以可以考虑使用官方提供的显卡驱动. # 先安 ...

  8. java对mysql的增删改查

    -----连接数据库 package connectdb;import java.sql.*;class Dbcon { // 此处连接数据库,独立开一个类,以后操作数据库的每次连接就不用写这么多 p ...

  9. JAVAEE——SSH三大框架整合(spring+struts2+hibernate)

    一.整合原理 二.导包(41个) 1.hibernate (1)hibernate/lib/required (2)hibernate/lib/jpa | java persist api java的 ...

  10. C#调用TSC条码打印机打印二维码

    #region 调用TSC打印机打印 /// <summary> /// 调用TSC打印机打印 /// </summary> /// <param name=" ...