您的位置:首页 > 编程语言 > Java开发

Spring学习3—控制反转(IOC)基于Annotation(注解)的依赖注入实现

2016-02-04 17:28 981 查看
一、依赖注入--手工装配—注解方式

在java代码中使用@Autowired或@Resource注解方式进行装配的前提条件是:

1、引入context命名空间
需要在xml配置文件中配置以下信息:


<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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:annotation-config/>

</beans>

注意到:

在配置文件中添加context:annotation-config标签<context:annotation-config/>这个配置隐式注册了多个对注释进行解析处理的处理器:

AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,

PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor

注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar


在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:


@Autowired
默认按类型装配,

@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。


例如:


a、
@Autowired

@Autowired
//从xml文件中寻找到类型为PersonDao的bean后初始化personDao

private PersonDao
personDao;//用于字段上

@Autowired

public void
setPersonDao(PersonDaopersonDao)

//用于属性的set方法上

{

this.personDao = personDao;

}

@Autowired注解是按类型装配依赖对象,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如:@Autowired(required=false)

private PersonDao personDao;//用于字段上

@Autowired(request=false)

public void setPersonDao(PersonDaopersonDao) {

//用于属性的set方法上

this.personDao =
personDao;

}

如果我们想使用按名称装配,可以结合@Qualifier注解一起使用。如下:

@Autowired@Qualifier("personDao")

private
PersonDao personDao;//用于字段上

@Autowired

public void setPersonDao(@Qualifier("personDao") PersonDao
personDao) {//用于属性的set方法上

this.personDao=
personDao;

}

@Qualifier注解也能够被指定为构造器的参数或者方法的参数:

------------------------------------------------------------

b、@Resource注解

@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上.

@Resource注解默认按名称装配。

名称可以通过@Resource的name属性指定,如果没有指定name属性,

当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象

当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。


如:

//从xml文件中寻找到名字为personDao的bean后初始化personDao
@Resource(name="personDao")

private
PersonDao personDao;//用于字段上

@Resource(name="personDao")

public void setPersonDao(PersonDao
personDao)

{//用于属性的set方法上

this.personDao =
personDao;

}

后一种相当于xml配置文件中的

<property name=“personDao"
ref="personDao" />


注意:如果没有指定name属性,并且按照默认的名称找不到依赖对象时,
@Resource注解会回退到按类型装配。但一旦指定了name属性,就只能按名称装配了。


、依赖注入--自动装配—注解方式

自动装配是指,Spring
在装配 Bean 的时候,根据指定的自动装配规则,将某个 Bean 所需要引用类型的 Bean
注入进来。<bean> 元素提供了一个指定自动装配类型的 autowire
属性,该属性有如下选项:

如:


<bean id=“foo”class=“...Foo” autowire=“autowire
type”>


autowire属性取值如下

*
byType:按类型装配,可以根据属性的类型,在容器中寻找跟该类型匹配的bean。如果发现多个,那么将会抛出异常。如果没有找到,即属性值为null。

*
byName:按名称装配,可以根据属性的名称,在容器中寻找跟该属性名相同的bean,如果没有找到,即属性值为null。

*constructor与byType的方式类似,不同之处在于它应用于构造器参数。如果在容器中没有找到与构造器参数类型一致的bean,那么将会抛出异常。

*autodetect
:首先尝试使用constructor来自动装配,然后使用byType方式。不确定性的处理与constructor方式和byType方式一致。

通过在classpath自动扫描方式把组件纳入spring容器中管理

前面的例子我们都是使用XML的bean定义来配置组件。在一个稍大的项目中,通常会有上百个组件,如果这些组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找及维护起来也不太方便。

spring2.5为我们引入了组件自动扫描机制,它可以在类路径底下寻找标注了@Component、@Service、
@Controller、@Repository注解的类,并把这些类纳入进spring容器中管理。它的作用和在xml文件中使用bean节点配置组件
是一样的。


要使用自动扫描机制,我们需要打开以下配置信息:


1、引入context命名空间 需要在xml配置文件中配置以下信息:


<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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.dao.impl,com.biz.impl,com.util,com.intercept"/>


</beans>

注意到:在配置文件中添加context:component-scan标签

<context:component-scan
base-package="com.dao.impl,com.biz.impl,com.util,com.intercept"/>

其中base-package为需要扫描的包(含子包),他会将这些包下的类自动装配成bean,并且bean的名字为该类的类名,但首字母小写。


注:

1、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor会隐式地被包括进来。
也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。

2、功能介绍

@Service用于标注业务层组件、

@Controller用于标注控制层组件(如struts中的action)、

@Repository用于标注数据访问组件,即DAO组件。

而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

//HibernateUtil
,



@Component //
[b]指定该类为Component组件[/b]

public class HibernateUtil
{

//从xml文件中寻找到名字为sessionFactory的bean后初始化sessionFactory


@Resource(name =
"sessionFactory")


private
SessionFactory factory;


//该属性可以无get/set 方法


....

}

//Dao层

import
org.springframework.stereotype.Repository;

import com.test.dao.PersonDao;


@Repository
//指定该类为Repository组件

public class PersonDaoBean implements PersonDao
{

//即默认取字段的名称作为bean名称寻找依赖对象,即hibernateUtil

@Resource

private
HibernateUtil hibernateUtil;....

}

//业务层/Service层

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.test.dao.PersonDao;

import com.test.service.PersonService;


@Service

//指定该组件为Service组件

public class PersonServiceBean implements
PersonService {

@Resource
//即默认取字段的名称作为bean名称寻找依赖对象 private PersonDao
personDao;

....

}

//控制层/Controller层

@Controller//指定该组件为Controller组件

public class AccountMngAction

{

//即默认取字段的名称作为bean名称寻找依赖对象
@Resource

private
CheckInBiz checkInBiz;

@Resource

private
RoomBiz roomBiz;

@Resource

private
AccountBiz accountBiz;

....

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: