注入依赖对象可以采用手工装配和自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见装配结果
依赖注入――手工装配
手工装配依赖对象,有两种编程方式:基于xml和注解方式
1 在xml配置文件中,通过在bean节点下配置,如:
<bean id="personDao" class="cn.itcast.service.PersonDaoBean">
<constructor-arg index="0" type="java.lang.String"
value="xxx"/>//使用构造器注入
<property name="name" value="探索"/>//使用setter方法注入
</bean>
2 在代码里使用注解方式:
在java代码里使用@Resource和@AutoWired的注解方式进行装配。但我们需要在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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
</bean>
其中<context:annotation-config/> 这个配置隐式注册了多个对注释进行解析处理的处理器
比如:对@AuotWired :AutowireAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor
注:@Resource注解 在spring的安装目录lib/j2ee/comm-annotation.jar
在java代码中使用@Autowired或者@Resource注解方式进行装配,这两个注解的区别是:@Autowired默认按照类型装配(这个是spring提供的),@Resource默认按照名称装配(这个是jdk1.6提供的),当找不到名称匹配的bean才会按照类型装配。『通常建议使用@Resource,因为这样不会与spring框架耦合』
@Autowired
Private PersonDao personDao://用于字段上
...
@Autowired
Public void setOrderDao(OrderDao orderDao){//用于属性的setter方法上
This.orderDao = orderDao;
}
@Autowired注解是按照类型装配依赖对象的,默认情况下它要求依赖对象必须存在,如果允许null值,可以设置它required属性为false。如果我们想使用名称装配,可以结合@Qualifier注解一起使用。如下:
@Autowired @Qualifier(“personDaoBean”)
Private PersonDao personDao;
@Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上,但它默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,当注解标注在字段上,即默认取字段的名称做为bean名称寻找依赖对象,当注解标注在属性的setter方法上,即默认取属性名称做为bean名称寻找依赖对象。
@Resource(name=”personDaoBean”)
Private PersonDao personDao;//用于字段上
::注意:: 如果没有指定name属性,并且按照默认的名称仍然找不到依赖对象时,@Resource注解会回退到按照类型装配。但一旦指定了name属性,就只能按照名称装配了。
import javax.annotation.Resource;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
@Resource private PersonDao personDao;
private String name;
public PersonServiceBean() {}
public void save()
{
System.out.println("�ɹ�ִ��save����");
}
}
<bean id="personDao" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean" class="cn.itcast.service.impl.PersonServiceBean"/>
这样,@Resource private PersonDao personDao;就会根据personDao到配置文件找到id/name为personDao的对象进行注入。如果没有找到会按照PersonDao类型进行查找进行注入。(因为@Resource默认按照名称查找,如果找不到按照类型查找)
注意1:如果改为<bean id="personDaoxxx" class="cn.itcast.dao.PersonDao"/>也是可以找到的,会按照类型查找!
注意2:如果改为
@Resource(name=”personDaoxxx”) private PersonDao personDao;也是可以的,但是这样如果在容器中没有名字为personDaoxxx的就不会按照类型去找了,就会出现异常!
上面是对字段进行注入,下面根据setter进行注入
import javax.annotation.Resource;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
private String name;
@Resource
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
public PersonServiceBean() {
}
public void save()
{
System.out.println("�ɹ�ִ��save����");
}
}
<bean id="personDao" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean"/>
这样也是OK的。
@Resource是j2ee里面的应用,下面看一下spring的注解@Autowired
import org.springframework.beans.factory.annotation.Autowired;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
@Autowired
private PersonDao personDao;
private String name;
public PersonServiceBean() {
}
public void save()
{
System.out.println("�ɹ�ִ��save����");
}
}
<bean id="personDao" class="cn.itcast.dao.PersonDao"/>
<bean id="personServiceBean"
class="cn.itcast.service.impl.PersonServiceBean"/>
@Autowired默认是按照类型查找要注入的对象的。要改成按照名称查找注入可以这样:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import cn.itcast.dao.PersonDao;
import cn.itcast.service.PersonService;
public class PersonServiceBean implements PersonService {
@Autowired @Qualifier("personDao")//按照指定名字注入
private PersonDao personDao;
private String name;
public PersonServiceBean() {
}
public void save()
{
personDao.save();
}
}
当然,如果@Autowired(required=true) @Qualifier("personDao")
private PersonDao personDao;
required=true :表示如果在配置文件中找不到指定的要注入的值,会抛出异常
required=false :表示如果在配置文件中找不到指定的要注入的值,赋一个null
分享到:
相关推荐
完整英文电子版 IEC 60300-3-2:2004 Dependability management - Part 3-2:Application guide - Collection of dependability data from the field(可靠性管理 - 第3-2部分:应用指南 - 从现场收集可靠性数据)。...
Systems Analysis and Design: An Object-Oriented Approach with UML, 5th Edition by Dennis, Wixom, and Tegarden captures the dynamic aspects of the field by keeping students focused on doing SAD while ...
关于django-timezone-field 主页: : 软件包许可证:BSD-2-Clause 原料许可证: 简介:一个Django应用程序,为pytz时区对象提供数据库和表单字段。 当前构建状态 所有平台: 当前发行信息 姓名 资料下载 版本 平台...
资源来自pypi官网。 资源全名:django-json-field-0.4.1.tar.gz
多个类注解@XStreamAlias,value相同、且在集合上使用了@XStreamImplicit会出现异常: com.thoughtworks.xstream.converters.ConversionException: No field 'null' found in class '.....(注解的类路径)' ---- ...
6)....Fixed: Empty "Count" field/column is now displayed as "1" in Viewer 7)....Fixed: Generic names with "," could not be decoded in Viewer 8)....Fixed: Updated Windows 10 detection for latest builds...
案例三十二:使用DropdownlistByKey 和DropdownlistByIndex - 223 - 案例三十三:使用External Window以及Dialog Boxes - 227 - 案例三十四:实现Input field的cursor定位 - 246 - 案例三十五:从View上实现Web...
用法您应该在项目devDependencies以下配置和对等依赖项安装为devDependencies : npm install --save-dev eslint-config-infield eslint 接下来,只需从项目的.eslintrc文件扩展配置.eslintrc : "extends": "eslint...
今天在写一个简单的内核测试模块的时候出现了一个挺奇怪的问题,编译内核模块出现error: negative width in bit-field错误。网上查了一下也没人解决,自己试了好久终于解决了,所以分享出来供大家参考,先贴出源码。
stylelint-config-infield 遵循Infield DigitalCSS / SCSS编码准则的Stylelint配置。 用法 您应该在项目devDependencies以下配置和对等依赖项安装为devDependencies : npm install --save-dev stylelint-config-...
Principles of Planar Near-Field Antenna Measurements 介绍天线近场测量的基础知识 Contents Preface xi 1 Introduction 1 1.1 The phenomena of antenna coupling 1 1.2 Characterisation via the measurement ...
Analytical SAR computation in a multilayer elliptic cylinder: The near-field line-current radiation case Bioelectromagnetics 21:473^479 (2000) Analytical SARComputation in a Multilayer Elliptic ...
Abaqus UMAT subroutine code for phase-field fracture in elasto-plastic solids ABAQUS相场断裂模拟的UMAT程序,模拟弹塑性材料的断裂。附参考文献、inp文件和f文件。
Each part is presented by six experts in that field and state of the art information is shared and overviewed. This book is number 17 in this successful series of Analog Circuit Design.
python库。 资源全名:django-bitfield-1.6.0.tar.gz
python库。 资源全名:vlk-django-jsonfield-1.2.1.tar.gz
Applications of self-consistent field theory in polymer systems.pdf
关于sqlalchemy-jsonfield 主页: : 软件包许可证:Apache-2.0 原料许可证: 简介:SQLALchemy JSONField实现可独立于JSON类型支持在SQL上存储字典。 开发: : 文档: : SqlAlchemy为几种数据库类型...
资源来自pypi官网。 资源全名:django-validated-jsonfield-1.0.8.tar.gz
source Smart-Field-env / bin / activate :books: 依赖关系我们使用以下命令安装库: pip3 install -r requirements.txt :magnifying_glass_tilted_right: 测试我们使用pytest -v启动测试 :rocket: 我们启动了该...