`
leiwuluan
  • 浏览: 695154 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类

spring 2.5 注释驱动的IOC功能

阅读更多

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;
	}
}

 

 

当候选 Bean 数目不为1个时个时的应对方法

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 和 @PreDestroy

 

这两个注释定义在方法上,

被@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 命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。我们知道注释本身是不会做任何事情的,它仅提供元数据信息。要使元数据信息真正起作用,必须让负责处理这些元数据的处理器工作起来。

而我们前面所介绍的 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor 就是处理这些注释元数据的处理器。但是直接在 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 容器注册 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessorPersistenceAnnotationBeanPostProcessor 以及 equiredAnnotationBeanPostProcessor 这 4 个 BeanPostProcessor。

在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

 

 

 

使用 @Component

虽然我们可以通过 @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 种类型的过滤方式,通过下表说明:


表 1. 扫描过滤方式

过滤器类型 说明
注释 假如 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 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor),因此当使用 <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 对那些比较中立的类进行注释。

 

 

 

注释配置和 XML 配置的适用场合

是否有了这些 IOC 注释,我们就可以完全摒除原来 XML 配置的方式呢?答案是否定的。有以下几点原因:

  • 注释配置不一定在先天上优于 XML 配置。如果 Bean 的依赖关系是固定的,(如 Service 使用了哪几个 DAO 类),这种配置信息不会在部署时发生调整,那么注释配置优于 XML 配置;反之如果这种依赖关系会在部署时发生调整,XML 配置显然又优于注释配置,因为注释是对 Java 源代码的调整,您需要重新改写源代码并重新编译才可以实施调整。
  • 如果 Bean 不是自己编写的类(如 JdbcTemplateSessionFactoryBean 等),注释配置将无法实施,此时 XML 配置是唯一可用的方式。
  • 注释配置往往是类级别的,而 XML 配置则可以表现得更加灵活。比如相比于 @Transaction 事务注释,使用 aop/tx 命名空间的事务配置更加灵活和简单。

所以在实现应用中,我们往往需要同时使用注释配置和 XML 配置,对于类级别且不会发生变动的配置可以优先考虑注释配置;而对于那些第三方类以及容易发生调整的配置则应优先考虑使用 XML 配置。Spring 会在具体实施 Bean 创建和 Bean 注入之前将这两种配置方式的元信息融合在一起。

 

 原文:http://www.ibm.com/developerworks/cn/java/j-lo-spring25-ioc/

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

0
0
分享到:
评论

相关推荐

    spring2.5 api 离线版

    Spring2.5-Reference_zh_CN.chm Spring2.5-中文参考手册.chm spring——AOP,IOC.doc Spring框架快速入门之简介.doc spring配置全书.doc Spring中的IOC与AOP详解.ppt

    使用Spring2.5的Autowired实现注释型的IOC

    java 使用Spring2.5的Autowired实现注释型的IOC

    Spring2.5和Hibernate3集成--学习spring aop ioc

    Spring2.5和Hibernate3集成 采用声明式事务 1.声明式事务的配置 * 配置sessionFactory * 配置事务管理器 * 配置事务的传播特性 * 配置哪些类哪些方法使用事务 2.编写业务逻辑方法 * 继承...

    使用 Spring 2_5 注释驱动的 IoC 功能.mht

    使用 Spring 2_5 注释驱动的 IoC 功能.mht 使用 Spring 2_5 注释驱动的 IoC 功能.mht

    Spring 2.5 jar 所有开发包及完整文档及项目开发实例

    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 注解

    使用Spring2.5的Autowired实现注释型的IOC , 使用Spring2.5的新特性——Autowired可以实现快速的自动注入,而无需在xml文档里面添加bean的声明,大大减少了xml文档的维护

    spring 2.5 IOC 自动扫描,自动注入

    自己练习简单例子SPING 2.5类自动扫描,自动注入功能,加一些自己说明需要的可以下载.

    spring2.5.chm帮助文档(中文版)

    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中自动搜索组件...

    Spring2.5实例

    本实例是一个Spring与Hibernate结合的例子,很好地展示了用Spring的Ioc容器来灵活地定义Bean实现DAO和使用Spring的AOP实现事务管理。

    Spring2.5 IOC的简单实现

    NULL 博文链接:https://gddzmr.iteye.com/blog/694726

    Spring通过注解实现IOC

    Spring通过注解实现IOC,Spring通过注解实现IOC,Spring通过注解实现IOC

    springMVC详解以及注解说明 中文WORD版.rar

    基于注释(Annotation)的配置有越来越流行的趋势,Spring 2.5 顺应这种趋势,提供了完全基于注释配置 Bean、装配 Bean 的功能,您可以使用基于注释的 Spring IoC 替换原来基于 XML 的配置。本文通过实例详细讲述了 ...

    Spring框架中的ioc的幽默解释

    Spring框架中的ioc的幽默解释

    spring_iocspring_ioc

    spring_iocspring_iocspring_iocspring_iocspring_iocspring_iocspring_iocspring_iocspring_ioc

    Spring的IoC容器(《Spring揭秘》的精选版)

    针对Spring框架的主要功能以及开发者们遇到最多的问 题,首先介绍问题的相关背景,然后逐条进行深度剖析,最后通过分析来引入Spring框架可以提供的最佳解决方案。虽言Spring,却不局限于 Spring! 本书目录 目录 ...

    spring_ioc

    这是spring_ioc部分的内容。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。...

    spring2.5 学习笔记

    第三课:模拟Spring功能 5 第四课:搭建sping的运行环境 8 一、 建立一个新的项目 8 二、 建立spring的配置文件 8 三、 引入spring的jar包 8 四、 测试代码: 8 五、 注意接口的使用: 8 第五课:IOC(DI)配置及应用 ...

    Spring的Aop和Ioc示例

    Spring的Aop和Ioc示例代码,代码通过了调试的,没得问题.对于初学者理解和使用Spring的Aop和Ioc是够了.

    关于spring框架中的ioc的幽默解释.

    关于spring框架中的ioc的幽默解释.

Global site tag (gtag.js) - Google Analytics