转帖地址:http://blog.sina.com.cn/s/blog_7ffb8dd501014ai0.html
一、依赖注入--手工装配—注解方式
在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 //指定该类为Component组件
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;
....
}
相关推荐
技术分享:详解Spring基于Annotation的依赖注入实现
Spring Ioc 注解 依赖注入
Spring与IoC系列四:基于注解的依赖注入。对于DI使用注解,将不再需要在Spring配置文件中声明Bean实例。Spring中使用注解,需要在原有Spring运行环境基础上再做一些改变
IOC与DI的理解及使用 控制反转IOC(Inversion of Control)是一种设计思想,DI(依赖注入)是实现IOC的一种方法 。... 在Spring中实现控制反转的是IOC容器 ,其 实现方法是依赖注入 (Dependency Injection,DI)
Spring IOC Annotation 注入 学习实例
控制反转 依赖注入的c#实现,很好的教程。
Spring通过注解实现IOC,Spring通过注解实现IOC,Spring通过注解实现IOC
springIOC 小例子 附带源码 适合新手学习
本文介绍了Spring框架中的控制反转IOC和依赖注入DI,欢迎阅读,共同学习,一起进步。 Spring框架基础参考:深入学习Spring基础 文章目录一.入门程序和IOC简介二.IOC-控制反转三.DI-依赖注入四. IOC和DI五.Spring...
Spring IOC 控制反转简单过程 1: 导入需要报 2:构建spring的配合文件 3: 将我们创建的类交给spring容器管理 4: 实例化容器 5: 从容器中获取实例
Java仿Spring框架IOC控制反转利用反射简单实现(源码) Java仿Spring框架IOC控制反转利用反射简单实现(源码)
spring IOC控制反转 spring IOC控制反转 spring IOC控制反转
spring ioc指的是控制反转,IOC容器负责实例化、定位、配置应用程序中的对象及建立这些对象间的依赖。交由Spring容器统一进行管理,从而实现松耦合
源代码 博文链接:https://danielkwo.iteye.com/blog/40945
基于注解的IOC配置,Spring整合Junit_特别适合新手学习。
1.2 IoC(控制反转)和DI(依赖注入)入门 1 传统的Java类 在工程下创建包ioc,在ioc包中创建类Test。(为了看起来清楚,将三个类写在了同一个Java文件中,其中唯一的public类为Test)。类的含义和关系如下: Car类...