(转)Spring注解完成Bean的定义
1. @Component(不推荐使用)、@Repository、@Service、@Controller
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
- @Component
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- ...
- }
使用@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定义注解工作起来
- <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-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <context:component-scan base-package="com.kedacom.ksoa" />
- </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表达式过滤一些类
以正则表达式为例,我列举一个应用实例:
- <context:component-scan base-package="com.casheen.spring.annotation">
- <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />
- </context:component-scan>
值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor), 因此当使用<context:component-scan />后,就可以将<context:annotation-config />移除了。
3. 使用@Scope来定义Bean的作用范围
在使用XML定义Bean时,我们可能还需要通过bean的scope属性来定义一个Bean的作用范围,我们同样可以通过@Scope注解来完 成这项工作:
- @Scope("session")
- @Component()
- public class UserSessionBean implements Serializable {
- ...
- }
(转)Spring注解完成Bean的定义的更多相关文章
- Spring-Context之四:Spring容器及bean的定义
Spring框架的核心功能之一就是控制反转(Inversion of Control, IoC),也叫做依赖注入(dependency injection, DI).关于依赖注入的具体内容可以参见Ma ...
- 在Servlet中获取Spring注解的bean
最近由于项目中出现了Servlet调用Spring的bean,由于整个项目中所有的bean均是注解方式完成,如@Service,@Repository,@Resource等,但是Spring的容器管理 ...
- Spring 注解配置Bean
一.使用注解配置Bean 1.注解 在类定义.方法定义.成员变量定义前使用.其简化<bean>标签,功能同<bean>标签.格式为: @注解标记名. 2.组件扫描 Spring ...
- 单元测试使用spring注解获取bean
在实际项目开发中经常会有单元测试,单元测试中经常会用类似这样的代码片段获取spring管理的bean @Test public void testSendEmail(){ MessageService ...
- 学习 Spring (八) 注解之 Bean 的定义及作用域
Spring入门篇 学习笔记 Classpath 扫描与组件管理 从 Spring 3.0 开始,Spring JavaConfig 项目提供了很多特性,包括使用 java 而不是 XML 定义 be ...
- 5.spring:注解配置 Bean
在classpath中扫描组件 组键扫描:能够从classpath下自动扫描,侦测和实例化具有特定注解的组件 特定的组件包括: ->@Componment:基于注解,标识一个受Spring管理的 ...
- 【Spring Framework】Spring注解设置Bean的初始化、销毁方法的方式
bean的生命周期:创建---初始化---销毁. Spring中声明的Bean的初始化和销毁方法有3种方式: @Bean的注解的initMethod.DestroyMethod属性 bean实现Ini ...
- spring 注解注入bean
通过注解方式注入bean,需要在配置类下注入bean 第一步,配置扫描文件夹 首先要在spring.xml中配置需要扫描的配置类 <context:componenet-scan base-pa ...
- [转]Spring 注解总结
原文地址:http://blog.csdn.net/wangshfa/article/details/9712379 一 注解优点?注解解决了什么问题,为什么要使用注解? 二 注解的来龙去脉(历史) ...
随机推荐
- MyEclipse解决SVN同步冲突问题conflict in the working copy obstructs the current operation
服务端版本控制软件subversion,客户端是eclipse的插件subclipse.当删除一个东西的时候老是提示错误,说冲突 commit -m "" C:/Users/Adm ...
- 用PHP和Ajax进行前后台数据交互——以用户登录为例
很多网站中都有用户登录系统,要完成用户的注册和登陆,就一定要用到前后台的数据交互.在这里以简单的用户注册和登陆为例介绍一下前后台交互的大致流程. 首先,我们来做一个简单的登陆界面. 这里为了方便我使用 ...
- NetCore WebSocket 即时通讯示例
1.新建Netcore Web项目 2.创建简易通讯协议 public class MsgTemplate { public string SenderID { get; set; } public ...
- springmvc 之 DispatcherServlet
DispatcherServlet说明 使用Spring MVC,配置DispatcherServlet是第一步. DispatcherServlet是一个Servlet,所以可以配置多个Dispat ...
- JavaScript练习笔记整理·2 - 6.24
Codewars地址:https://www.codewars.com/ 欢迎和大家一起来讨论~ 基础练习(1): 我的解答为: function isIsogram(str){ if(s ...
- Sql_Case_When用法
http://wenku.baidu.com/link?url=XBnkUzGtiJFhTnQk5HbmdgndhVEYJdcfDEhSEIFeTRn9-41KMLf_49wKiydNCF-4g3Qi ...
- Linux: 安装NVIDIA显卡驱动
Linux(Fedora25, 64bit)台式机配备了NVIDIA显卡GTX950,但是仅仅使用开源驱动nouveau,无法发挥NVIDIA显卡的性能,所以可以考虑使用官方提供的显卡驱动. # 先安 ...
- java对mysql的增删改查
-----连接数据库 package connectdb;import java.sql.*;class Dbcon { // 此处连接数据库,独立开一个类,以后操作数据库的每次连接就不用写这么多 p ...
- JAVAEE——SSH三大框架整合(spring+struts2+hibernate)
一.整合原理 二.导包(41个) 1.hibernate (1)hibernate/lib/required (2)hibernate/lib/jpa | java persist api java的 ...
- C#调用TSC条码打印机打印二维码
#region 调用TSC打印机打印 /// <summary> /// 调用TSC打印机打印 /// </summary> /// <param name=" ...