从Spring2.0以后的版本中,Spring也引入了基于注解(Annotation)方式的配置,注解(Annotation)是JDK1.5中引入的一个新特性,用于简化Bean的配置,某些场合可以取代XML配置文件。开发人员对注解(Annotation)的态度也是萝卜青菜各有所爱,个人认为注解可以大大简化配置,提高开发速度,同时也不能完全取代XML配置方式,XML 方式更加灵活,并且发展的相对成熟,这种配置方式为大多数 Spring 开发者熟悉;注解方式使用起来非常简洁,但是尚处于发展阶段,XML配置文件和注解(Annotation)可以相互配合使用。
基于注解的Spring配置准备条件:
(1).使用spring注解方式,必须加入spring注解方式所依赖的jar包:common-annotation.jar。
xmlns:context=”http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context/spring-context-2.5.xsd
xmlns:context=”http://www.springframework.org/schema/context”
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd在spring配置文件中元素之前添加:
context:annotation-config>
Spring2.5中使用四个注解按功能来进行对bean的配置:
(2).@Service:用于标注配置业务层(service层)组件。
(4).@Repository:用于标注配置数据访问层组件,即一般的DAO层Bean对象。
11. Spring的自动装配:
(1).@Autowire:默认是按照对象的数据类型进行自动装配的,如
@Autowire
- private UserDao userDao;
@Autowire
private UserDao userDao;(2).@Resource:默认是按照名称或者Id进行自动装配的,只有当找不到匹配的名称或者Id时才按类型进行装配,如:
@Resource(name=”userDao”)
- private UsreDao userDao;
@Resource(name=”userDao”)
private UsreDao userDao;注意:@Autowire和@Resource都既可以写在字段上,也可以写在set方法上。
Spring自动扫描机制是指,我们使用基于注解的spring配置方式后,spring的配置文件中只需要注册注解处理器,不用显式地配置Bean,当spring在运行时会自动扫描根据给定的条件下类路径中的所有bean,根据注解将它们注入和装配起来并进行初始化的过程。
context:component-scan base-package=“要自动扫描的包名“>
Spring在运行时就可以对指定的包中所有添加了Spring注解的bean自动扫描,注入,装配和初始化。
========================================
使用Spring注解完成Bean的定义
2010-04-21 16:48:54| 分类: spring | 标签: |字号大中小 订阅
1. @Component(不推荐使用)、@Repository、@Service、@Controller
只需要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了:
- @Component
- public class UserDaoImpl extends HibernateDaoSupport implements UserDao {
- ...
- }
@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>
<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 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 {
- ...
- }
@Scope("session") @Component() public class UserSessionBean implements Serializable { ... }
相关推荐
spring的Annotation注解.
spring annotation注解 使用Spring注解来注入属性
Dwr3+Spring3 全注解 annotation 方式实现
配置整合DWR3.0和Spring2.5使用annotation注解
Spring对注解(Annotation)处理源码分析 解析和注入注解配置的资源 源码级别的分析
spring中自定义注解(annotation)与AOP中获取注解.通过实例演示自定义注解。
主要为大家详细介绍了JavaWeb Spring注解Annotation,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
csh框架+cxf+spring+hibernate+mysql 注解 annotation xml json, 参考了菠萝大象的文章,感谢。
@Resource和@Autowired都是做bean的注入时使用,其实@Resource并不是Spring的注解,它的包是javax.annotation.Resource,需要导入,但是Spring支持该注解的注入。 1、共同点 两者都可以写在字段和setter方法上。两者...
JPA、EJB、Spring零配置等等怎么使用相信各位读者都能掌握,这里主要是说如何自定义自己的注解,自己使用自定义的注解。
SSH2 annotation 实现struts2.1.6 spring2.5.6 hibernate3.3 全注解开发 hibernate延迟加载_懒加载 具体应用
基于注解Annotation的最新版SSH(Struts2.3.7+Hibernate4.1.9+Spring3.2.0)整合开发,真正实现零配置。 最新版本Struts、Spring、Hibernate框架整合: struts-2.3.7 spring-framework-3.2.0.RELEASE hibernate-...
注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...
spring annotation 注解 maven 配套的源码
Spring注解所依赖的包。com.springSource.javax.annotation
NULL 博文链接:https://ysj5125094.iteye.com/blog/2054421
Spring注解驱动开发实战 组件注册,生命周期,自动装配
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
NULL 博文链接:https://ysj5125094.iteye.com/blog/2054257