- 浏览: 695154 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (260)
- web 开发 (44)
- struts (14)
- java (39)
- 我的感受 (2)
- ajax (2)
- spring (22)
- Hibernate (4)
- struts2 (1)
- java 面试题 (1)
- 英语 学习类 (2)
- jdk1.6特性 (1)
- extjs (13)
- oracle (3)
- JQuery (3)
- ┠ 『Spring 3.0』 ┨ (1)
- 『LOG4J』 (2)
- 『常用数据』 (1)
- JavaMail (0)
- 〓『 EJB3.0 』〓 (0)
- HTML5 (5)
- Canvas (0)
- spring2.5 (0)
- HPH 5 (1)
- C# .net (0)
- UML (1)
- REST (2)
- JS_validate (1)
- 项目管理文章 (1)
- 闲聊闲记 (12)
- SQL-SQL (3)
- javascript (18)
- js (4)
- linux (8)
- 电子政务 (1)
- 项目对象模型 (1)
- 系统集成项目管理 (1)
- shell (2)
- mysql (1)
- solr (5)
- cxf;services (2)
- 11 (0)
- 缓存 (1)
- cached (3)
- 单片机通信 (1)
- cached;oscache (1)
- android (15)
- android;蓝牙 (1)
- Lucene (5)
- TTS (1)
- 网络爬虫 (1)
- HtmlCleaner (1)
- iPhone (2)
- 有感而发 (1)
- Python (0)
- jetty (1)
- Redis (1)
- hadoop (1)
- socket (1)
- 色彩顾问 (1)
- javaapns (1)
- bat (1)
- object-c (4)
- ios (4)
- C-学习 (1)
- 搜索 (1)
- c# (1)
最新评论
-
annie211:
Angularjs构建一个CRUD应用可能用到的全部内容包括:数据绑定、基本模板标识符、表单验证、路由、深度链接、组件重用、依赖注入 -
潇寒听竹:
你好,请问您上传的那个APK为什么每次只能识别一次,不能连续识 ...
pocketsphinxAdroid离线语音识别---入门 -
ichenwenjin:
Angularjs构建一个CRUD应用可能用到的全部内容包括:数据绑定、基本模板标识符、表单验证、路由、深度链接、组件重用、依赖注入 -
NN_iaoiao:
问一下 你的这个People 类 是自定义的还是android ...
android 获取电话本中的联系人列表 -
wangzhe_real:
对于刚接触的我是一个很详细的教程,太感谢了
JAX-RS cxf web服务 rest简单增删改查 集成spring webService
spring 注释功相当于
以前在spring的配置文件的bean头中加入了:
default-autowire="byName"
default-autowire="byType"
Spring 通过一个 BeanPostProcessor
对 @Autowired
进行解析,所以要让 @Autowired
起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor
Bean。
@Autowired 默让是byType
它可以注释到方法,也可以注释到成员域:
如下两种:
1、
package com.web.userManagser.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.web.userManagser.services.UserManagerService;
public class UserManagerAction {
@Autowired
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
}
2、
package com.web.userManagser.action;
import org.springframework.beans.factory.annotation.Autowired;
import com.web.userManagser.services.UserManagerService;
public class UserManagerAction {
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
public UserManagerService getUserManagerService() {
return userManagerService;
}
@Autowired
public void setUserManagerService(UserManagerService userManagerService) {
this.userManagerService = userManagerService;
}
}
1、bean 数目为0个时:
也就是spring 找不到一个匹配的bean, spring 就会抛出BeanCreationException 异常
如上述的情况怎么解决呢。如果想让spring 找不到bean 不报错那么你在注释的时候可以如下的注释方法:
@Autowired(required = false)
当然,一般情况下,使用 @Autowired
的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false)
会很少用到。
2、bean 数目不只一个,而是多个时呢?
如果我们在spring 中配置了两个相同的类型时,而我们的注释又同时是通过byType时
那么spring 就会出错,它不知道哪个bean 是它要的。
那么我们就可以将@Qualifier 和@Autowired 一起用
用的同时就是把自动配置的方式从byType 转向 byName,
如下使用:
配置文件中有:
<bean id="userManagerService" class="com.web.userManagser.services.UserManagerService"/> <bean id="userManagerAction" class="com.web.userManagser.action.UserManagerAction"/> <bean id="userManagerAction1" class="com.web.userManagser.action.UserManagerAction"/>
此时我们就可以:
public class UserManagerAction {
@Autowired
@Qualifier("userManagerService")
private UserManagerService userManagerService;
public void sayHello(){
this.userManagerService.sayHello();
}
}
@Qualifier
只能和 @Autowired
结合使用,是对 @Autowired
有益的补充。一般来讲,@Qualifier
对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。
我们还可以用使用 JSR-250 的注释
要让 JSR-250 的注释生效,除了在 Bean 类中标注这些注释外,还需要在 Spring 容器中注册一个负责处理这些注释的 BeanPostProcessor
:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<!-- 该 BeanPostProcessor 将自动起作用,对标注的 @Resource、@PostConstruct 以及 @PreDestroy-->
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/>
<!-- 移除 boss Bean 的属性注入配置的信息 -->
<bean id="userManagerService" class="com.web.userManagser.services.UserManagerService"/>
<bean id="userManagerService1" class="com.web.userManagser.services.UserManagerService"/>
<bean id="userManagerAction" class="com.web.userManagser.action.UserManagerAction"/>
</beans>
@Resource、@PostConstruct
以及 @PreDestroy
。
1、@Resource 有两个属性。
name spring 将它解释成bean的名字
type spring 将它解释成bean 的类型
如果我们定义了name那么spring 就使用byName 方式注入
如果我们定义了type 那么spring 就用byTyte方式注入
如果两者都没有定义那么就默为byName
这两个注释定义在方法上,
被@PostConstruct 注释的方法,bean 被实例化后调用的。
而被@PreDestroy 注释的方法,bean 被销毁时调用的。
public class UserManagerAction { @Resource(name="userManagerService") private UserManagerService userManagerService; public void sayHello(){ this.userManagerService.sayHello(); } @PostConstruct public void ct(){ System.out.println("ct"); } @PreDestroy public void pro(){ System.out.println("ct"); } }
我们知道,不管是通过实现 InitializingBean
/DisposableBean
接口,还是通过 <bean> 元素的 init-method/destroy-method
属性进行配置,都只能为 Bean 指定一个初始化 / 销毁的方法。但是使用 @PostConstruct
和 @PreDestroy
注释却可以指定多个初始化 / 销毁方法,那些被标注 @PostConstruct
或 @PreDestroy
注释的方法都会在初始化 / 销毁时被执行。
使用 <context:annotation-config/> 简化配置
Spring 2.1 添加了一个新的 context 的 Schema 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。
而我们前面所介绍的 AutowiredAnnotationBeanPostProcessor
和 CommonAnnotationBeanPostProcessor
就是处理这些注释元数据的处理器。但是直接在 Spring 配置文件中定义这些 Bean 显得比较笨拙。Spring 为我们提供了一种方便的注册这些 BeanPostProcessor
的方式,这就是 <context:annotation-config/>。
<?xml version="1.0" encoding="UTF-8" ?> <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:annotation-config/> <bean id="userManagerService" class="com.web.userManagser.services.UserManagerService"/> <bean id="userManagerAction" class="com.web.userManagser.action.UserManagerAction"/> </beans>
<context:annotationconfig/> 将隐式地向 Spring 容器注册 AutowiredAnnotationBeanPostProcessor
、CommonAnnotationBeanPostProcessor
、PersistenceAnnotationBeanPostProcessor
以及 equiredAnnotationBeanPostProcessor
这 4 个 BeanPostProcessor。
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。
虽然我们可以通过 @Autowired
或 @Resource
在 Bean 类中使用自动注入功能,但是 Bean 还是在 XML 文件中通过 <bean> 进行定义 —— 也就是说,在 XML 配置文件中定义 Bean,通过 @Autowired
或 @Resource
为 Bean 的成员变量、方法入参或构造函数入参提供自动注入的功能。能否也通过注释定义 Bean,从 XML 配置文件中完全移除 Bean 定义的配置呢?答案是肯定的,我们通过 Spring 2.5 提供的 @Component
注释就可以达到这个目标了。
下面,我们完全使用注释定义 Bean 并完成 Bean 之间装配:
package com.web.userManagser.services; import org.springframework.stereotype.Component; @Component public class UserManagerService { public void sayHello(){ System.out.println("fuck you!"); } }
@Component("userManagerAction") public class UserManagerAction { @Resource(name="userManagerService") private UserManagerService userManagerService; public void sayHello(){ this.userManagerService.sayHello(); } }
@Component
有一个可选的入参,用于指定 Bean 的名称,在 Boss 中,我们就将 Bean 名称定义为“boss
”。一般情况下,Bean 都是 singleton 的,需要注入 Bean 的地方仅需要通过 byType 策略就可以自动注入了,所以大可不必指定 Bean 的名称。
在使用 @Component
注释后,Spring 容器必须启用类扫描机制以启用注释驱动 Bean 定义和注释驱动 Bean 自动注入的策略。Spring 2.5 对 context 命名空间进行了扩展,提供了这一功能,请看下面的配置:
<?xml version="1.0" encoding="UTF-8" ?>
<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.web"/>
</beans>
这里,所有通过 <bean> 元素定义 Bean 的配置内容已经被移除,仅需要添加一行 <context:component-scan/> 配置就解决所有问题了——Spring XML 配置文件得到了极致的简化(当然配置元数据还是需要的,只不过以注释形式存在罢了)。<context:component-scan/> 的 base-package 属性指定了需要扫描的类包,类包及其递归子包中所有的类都会被处理。
<context:component-scan/> 还允许定义过滤器将基包下的某些类纳入或排除。Spring 支持以下 4 种类型的过滤方式,通过下表说明:
注释 | 假如 com.baobaotao.SomeAnnotation 是一个注释类,我们可以将使用该注释的类过滤出来。 |
类名指定 | 通过全限定类名进行过滤,如您可以指定将 com.baobaotao.Boss 纳入扫描,而将 com.baobaotao.Car 排除在外。 |
正则表达式 | 通过正则表达式定义过滤的类,如下所示: com\.baobaotao\.Default.* |
AspectJ 表达式 | 通过 AspectJ 表达式定义过滤的类,如下所示: com. baobaotao..*Service+ |
下面是一个简单的例子:
<context:component-scan base-package="com.web"> <context:include-filter type="aspectj" expression="com.web..action..*"/> </context:component-scan> |
值得注意的是 <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor
和 CommonAnnotationBeanPostProcessor
),因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。
通过 @Scope 指定 Bean 的作用范围
package com.baobaotao;
import org.springframework.context.annotation.Scope;
…
@Scope("prototype")
@Component("boss")
public class Boss {
…
}
这样,当从 Spring 容器中获取 boss
Bean 时,每次返回的都是新的实例了。
Spring 2.5 中除了提供 @Component
注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository
、@Service
和 @Controller
。在目前的 Spring 版本中,这 3 个注释和 @Component
是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。虽然目前这 3 个注释和 @Component
相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用 @Repository
、@Service
和 @Controller
对分层中的类进行注释,而用 @Component
对那些比较中立的类进行注释。
是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因:
- 注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。
- 如果 Bean 不是自己编写的类(如
JdbcTemplate
、SessionFactoryBean
等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。 - 注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于
@Transaction
事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。
所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。
原文:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/
发表评论
-
Java 数据验证
2017-05-12 10:27 1061在web开发当前就会遇到一些数据有效性验证的相关代码编 ... -
spring mvc rest 小例子
2012-05-24 15:35 1796http://localhost:8080/mainrest/ ... -
JAX-RS cxf web服务 rest简单增删改查 集成spring webService
2011-09-14 14:35 5716没时间什么也不多说了。所有的话都在代码里。 ... -
aop svn
2010-11-30 08:31 967aop http://student.csdn.net/sp ... -
spring 2.5 mvc 注解 ajax 实现
2010-03-07 19:30 4215spring 2.5 的mvc 大量的使用了注解,减少了xml ... -
spring 2.0_MVC+Ibatis web 实例
2010-03-04 01:50 2559这个便子是我在面试之后回来做的, 前段时间不久,我去面 ... -
spring 2.0 配置文 件头
2010-02-28 00:02 129<?xml version="1.0" ... -
Spring Jar包详解
2010-01-28 13:35 1267摘自:http://vc88.iteye.com/ ... -
spring配置事务
2010-01-27 17:38 1939<?xml version="1.0" ... -
SSH(Spring+Struts+Hibernate)综合应用
2009-08-04 23:56 1461(1)Struts与Spring整合 <1> 在 ... -
spring 事务管理
2009-08-04 23:31 1622对于J2EE 应用程序而言,事务的处理一般有两种模式: ... -
在Spring应用中使用Hibernate
2009-08-04 11:56 1003<1>Spring in Hibernate示例 ... -
JDBC操作的Java对象化
2009-08-03 23:41 133org.springframework.jdbc.object ... -
基于Spring的数据访问
2009-08-03 22:52 1054(1)在Spring应用中使用JDBC Spring对JDB ... -
Spring中常用三种通知
2009-08-03 17:14 1204(1)前置通知 接口:org.springframework ... -
Spring的体系结构
2009-08-03 16:42 3825Spring 框架是一个分层架 ... -
Spring bean的封装机制
2009-08-03 16:31 1675Spring 从核心而言,是一个DI 容器,其设计哲学是提供一 ... -
spring 中 Bean继承
2009-08-03 16:29 1994某些情况下,你也许需要定义多个实现了共用接口的相同类型的bea ... -
spring 中自动装配你的Bean
2009-08-03 16:27 1846如果你不喜欢自己将你的程序装配起来,你可以尝试让Spring自 ... -
spring 注入参数(Injection Parameters)
2009-08-03 16:25 2995(1)简单的值注入 将简单的值注入到beans里面是很简单的 ...
相关推荐
Spring2.5-Reference_zh_CN.chm Spring2.5-中文参考手册.chm spring——AOP,IOC.doc Spring框架快速入门之简介.doc spring配置全书.doc Spring中的IOC与AOP详解.ppt
java 使用Spring2.5的Autowired实现注释型的IOC
Spring2.5和Hibernate3集成 采用声明式事务 1.声明式事务的配置 * 配置sessionFactory * 配置事务管理器 * 配置事务的传播特性 * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承...
使用 Spring 2_5 注释驱动的 IoC 功能.mht 使用 Spring 2_5 注释驱动的 IoC 功能.mht
13) spring-mock.jar需spring-core.jar,spring-beans.jar,spring-dao.jar,spring-context.jar,spring-jdbc.jarspring2.0和spring2.5及以上版本的jar包区别Spring 2.5的Jar打包 在Spring 2.5中, Spring Web MVC...
使用Spring2.5的Autowired实现注释型的IOC , 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护
自己练习简单例子SPING 2.5类自动扫描,自动注入功能,加一些自己说明需要的可以下载.
2. Spring 2.0和 2.5的新特性 2.1. 简介 2.2. 控制反转(IoC)容器 2.2.1. 新的bean作用域 2.2.2. 更简单的XML配置 2.2.3. 可扩展的XML编写 2.2.4. Annotation(注解)驱动配置 2.2.5. 在classpath中自动搜索组件...
本实例是一个Spring与Hibernate结合的例子,很好地展示了用Spring的Ioc容器来灵活地定义Bean实现DAO和使用Spring的AOP实现事务管理。
NULL 博文链接:https://gddzmr.iteye.com/blog/694726
Spring通过注解实现IOC,Spring通过注解实现IOC,Spring通过注解实现IOC
基于注释(Annotation)的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,提供了完全基于注释配置 Bean、装配 Bean 的功能,您可以使用基于注释的 Spring IoC 替换原来基于 XML 的配置。本文通过实例详细讲述了 ...
Spring框架中的ioc的幽默解释
spring_iocspring_iocspring_iocspring_iocspring_iocspring_iocspring_iocspring_iocspring_ioc
针对Spring框架的主要功能以及开发者们遇到最多的问 题,首先介绍问题的相关背景,然后逐条进行深度剖析,最后通过分析来引入Spring框架可以提供的最佳解决方案。虽言Spring,却不局限于 Spring! 本书目录 目录 ...
这是spring_ioc部分的内容。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...
第三课:模拟Spring功能 5 第四课:搭建sping的运行环境 8 一、 建立一个新的项目 8 二、 建立spring的配置文件 8 三、 引入spring的jar包 8 四、 测试代码: 8 五、 注意接口的使用: 8 第五课:IOC(DI)配置及应用 ...
Spring的Aop和Ioc示例代码,代码通过了调试的,没得问题.对于初学者理解和使用Spring的Aop和Ioc是够了.
关于spring框架中的ioc的幽默解释.