Spring依赖注入(基于XML配置文件和Annotation的方式完成属性装配)

依赖注入的方式(手工装配):
1.使用bean的构造器注入—基于XML方式
2.使用属性setter方法注入—基于XML方式
3.使用field注入—基于Annotation方式
注入依赖对象可采用手工装配和自动装配,在实际应用中建议使用手工装配,自动装配会产生未知情况,且无法预知装配结果

使用构造器方式注入:

// 提供构造方法
    public PersonServiceBean(PersonDao personDao, String name) {
        this.personDao = personDao;
        this.name = name;
    }


    @Override
    public void save() {

        System.out.println(name);
        personDao.add();
    }

    public void destory() {
        System.out.println("关闭打开的资源");
    }
<!--在bean中子元素的配置如下-->
 <constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao"></constructor-arg>
    <constructor-arg index="1" value="jxlgdx"></constructor-arg>

手工装配依赖对象:两种方式
1.在xml配置文件中,通过在bean节点下的配置,代码如下

    <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
        <!-- id属性中不能有特殊字符,name属性可以有 -->   
    <!-- 构造器注入 -->
    <constructor-arg index="0" type="cn.itcast.dao.PersonDao" ref="personDao"/>
    <constructor-arg index="1" type="String" value="jxlgdx"/>    
    <!-- 属性setter方法注入 -->
    <property name="name" value = "zhangsan"></property>
    </bean> 

2.在java code中使用@Autowired或@Resource注解方式进行装配(推荐使用Resource),但使用注解方式需要在xml文件中配置如下信息,
*
@Resource是JDK中自带的注解,而@Autowired是Spring Framework提供的,为了方便解耦,推荐使用Resource*

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

    <context:annotation-config/>

在java代码上使用注解时,可以写在字段上也可以写在set方法上

@Resource
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }//写在set方法上

public class PersonServiceBean implements PersonService {
    @Resource private PersonDao personDao;
    private String name;//写在字段上

PS:在用junit测试时,出现一个bug,最后在网上才找到解决办法,如下:
关于 AnnotationConfigBeanDefinitionParser are only available on JDK 1.5 and higher
修改IDE使用的JDK版本可百度找到,这个使用annotation完成bean属性装配也由于bug弄了一早上,汗颜。。。

Annotation:配置的作用,注解代表某一种业务意义,注解背后的处理器才有实际作用:先解析所有属性,根据搜索规则,如果能match上,则取得bean,通过反射技术注入

另一种注解@Autowired的使用:
1.@Autowired默认按照类型装配,@Resource默认按照名称装配,当找不到与名称匹配的bean才会按照类型装配(@Resource的名称可以通过name属性指定,如果没有name属性则默认取字段的名称作为bean的名称寻找依赖对象,当注解使用在setter方法上面时,默认区属性名作为bean的名称寻找依赖对象)

PS:@Reource,若没有指定name属性,并且按照默认名称也找不到依赖对象时,注解会自动根据类型装配,但一旦指定了name属性后,则在只能按照名称装配

自动装配(不推荐使用,会产生不可预知的结果)
例子如下,aotuwire属性取值有多种,详细见API文档

<bean id="xxx" class="yyy.yyy" autowire="byType"/>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值