Spring学习笔记(8)——依赖注入

spring依赖注入
使用构造器注入
使用属性setter方法注入
使用Field注入(用于注解方式)
 
注入依赖对象可以采用手工装配或自动装配,在实际应用中建议使用手工装配,因为自动装配会产生未知情况,开发人员无法预见最终的装配结果。
 
1.手工装配依赖对象
 
 手工装配依赖对象,在这种方式中又有两种编程方式
  *  在xml配置文件中,通过在bean节点下配置
  *  在java代码中使用@Autowired或@Resource注解方式进行装配
              
 依赖注入--手工装配--XML方式
  通过setter方法注入依赖
  <bean>元素的< property >子元素指明了使用它们的set方法来注入。可以注入任何东西,从基本类型到集合类,甚至是应用系统的bean。
  通过setter方法注入依赖
         *简单bean配置
          配置bean的简单属性,基本数据类型和String。

<bean id="personService" class="com.test.bean.impl.PersonServiceImpl">
    <!-- 基本类型,string类型 -->
    <property name="age"value="20"></property>
    <property name="name" value="张无忌"></property>                      
</bean>

 

通过setter方法注入依赖
    *引用其它bean

<bean id="person" class="com.test.bean.Person" />
<bean id="personService" class="com.test.bean.impl.PersonServiceImpl">
    <!-- 引用类型 -->
    <property name="person" ref="person" />
</bean>

 
     *内部bean

<bean id="personService"class="com.test.bean.impl.PersonServiceImpl">
        <!-- 内部bean注入 -->
        <property name="personClass">
              <bean class="com.test.bean.PersonClass" />
        </property>
</bean>

 这种方式的缺点是你无法在其它地方重用这个personClass实例,原因是它是专门为personService而用。
 
   *装配集合
 
      若bean的属性是集合类型,按如下处理:
        A、装配List和数组:
          <!-- 装配list -->
          <property name="lists">
               <list>
                   <value>list1</value>
                   <value>list2</value>
                   <ref bean="person"/>
               </list>
          </property>
         <!--装配数组 -->
        <property name="obj">
             <list>
                 <value>obj1</value>
                 <value>obj2</value>
                 <ref bean="person"/>
             </list>
        </property>
 
  B、装配set:
    <!--装配set -->
        <property name="sets">
             <set>
                <value>set1</value>
                <value>set2</value>
                <ref bean="person"/>
             </set>
        </property>
  set使用方法和list一样,不同的是对象被装配到set中,而list是装配到List或数组中装配。
 
  *装配键值对
  C、装配map:
   <!-- 装配map-->
    <propert yname="maps">
         <map>
             <entry key="01">
                  <value>map01</value>
             </entry>
             <entry key="02">
                 <value>map02</value>
             </entry>
        </map>
    </property>
      map中的<entry>的数值和<list>以及<set>的一样,可以使任何有效的属性元素,需要注意的是key值必须是String的。
  D、装配Properties:
  <!--装配Properties  -->
  <property name="props">
   <props>
     <prop key="01">prop1</prop>
     <prop key="02">prop2</prop>
   </props>
  </property>
       E、设置null:
            <!--装配null -->
        <property name="listnull">
              <null/>
        </property>
     
      通过参数的顺序:
            <constructor-arg index="0">
                   <value>张三</value>
            </constructor-arg>
            <constructor-arg index="1">
                   <value>56</value>
            </constructor-arg>
 
      通过构造函数注入依赖
 
          <!--通过参数的类型 -->
          <constructor-arg type="java.lang.Integer">
                   <value>56</value>
          </constructor-arg>
          <constructor-arg type="java.lang.String">
                   <value>张三</value>
          </constructor-arg>
 
  依赖注入--手工装配—注解方式
 
      在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-2.5.xsd
        http://www.springframework.org/schema/context
          http://www.springframework.org/schema/context/spring-context-2.5.xsd">
     <context:annotation-config/>
      </beans>
  2、在配置文件中添加context:annotation-config标签<context:annotation-config/>这个配置隐式注册了多个对注释进行解析处理的处理器AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor,RequiredAnnotationBeanPostProcessor
     注: @Resource注解在spring安装目录的lib\j2ee\common-annotations.jar在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是:@Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。

@Autowired
private PersonDao  personDao;//用于字段上
@Autowired
public void setPersonDao(PersonDao personDao) { //用于属性的set方法上
      this.personDao = personDao;
}

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

@Autowired(required=false)
private PersonDao  personDao;//用于字段上
 
@Autowired(request=false)
public void setPersonDao(PersonDao personDao) {  //用于属性的set方法上
     this.personDao = personDao;
}

 

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

@Autowired@Qualifier("personDao")
private PersonDao  personDao;//用于字段上
 
@Autowired
public void setPersonDao(@Qualifier("personDao") PersonDao personDao) {
     //用于属性的set方法上
      this.personDao= personDao;
}

 
       @Qualifier注解也能够被指定为构造器的参数或者方法的参数:
       @Resource注解和@Autowired一样,也可以标注在字段或属性的setter方法上.
       @Resource注解默认按名称装配。名称可以通过@Resource的name属性指定,如果没有指定name属性,
       当注解标注在字段上,即默认取字段的名称作为bean名称寻找依赖对象
       当注解标注在属性的setter方法上,即默认取属性名作为bean名称寻找依赖对象。
       @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属性,就只能按名称装配了。
 
2.自动装配依赖对象
     对于自动装配,大家了解一下就可以了,实在不推荐大家使用。例子:
     <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-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="cn.itcast"/>
              </beans>
              2、在配置文件中添加context:component-scan标签
                    <context:component-scan base-package="cn.itcast"/>
                其中base-package为需要扫描的包(含子包)。
               注:
                    1、在使用组件扫描元素时,AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor会隐式地被包括进来。 也就是说,连个组件都会被自动检测并织入 - 所有这一切都不需要在XML中提供任何bean配置元数据。
 
                     2、功能介绍
                          @Service用于标注业务层组件、
                          @Controller用于标注控制层组件(如struts中的action)、
                          @Repository用于标注数据访问组件,即DAO组件。
                          而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
                          

//Dao层
import org.springframework.stereotype.Repository;
import com.test.dao.PersonDao;
@Repository("personDao")
public class PersonDaoBean implements PersonDao {
}
 
//业务层
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.test.dao.PersonDao;
import com.test.service.PersonService;
@Service("personService")
public class PersonServiceBean implements PersonService {
    @Resource(name="personDao")
    private PersonDao personDao;
}

 
转自http://www.cnblogs.com/jqyp/archive/2010/08/21/1805418.html
 
 
       平常的java开发中,程序员在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种开发存在的问题是new的类实例不好统一管理,spring提出了依赖注入的思想,即依赖类不由程序员实例化,而是通过spring容器帮我们new指定实例并且将实例注入到需要该对象的类中。依赖注入的另一种说法是“控制反转”,通俗的理解是:平常我们new一个实例,这个实例的控制权是我们程序员,而控制反转是指new实例工作不由我们程序员来做而是交给spring容器来做。

       spring有多种依赖注入的形式,下面仅介绍spring通过xml进行IOC配置的方式:
set注入
这是最简单的注入方式,假设有一个SpringAction,类中需要实例化一个SpringDao对象,那么就可以定义一个private的SpringDao成员变量,然后创建SpringDao的set方法(这是ioc的注入入口):

package com.bless.springdemo.action;  
public class SpringAction {  
        //注入对象springDao  
    private SpringDao springDao;  
        //一定要写被注入对象的set方法  
        public void setSpringDao(SpringDao springDao) {  
        this.springDao = springDao;  
    }  
  
        public void ok(){  
        springDao.ok();  
    }  
}  

 

随后编写spring的xml文件,<bean>中的name属性是class属性的一个别名,class属性指类的全名,因为在SpringAction中有一个公共属性Springdao,所以要在<bean>标签中创建一个<property>标签指定SpringDao。<property>标签中的name就是SpringAction类中的SpringDao属性名,ref指下面<bean name="springDao"...>,这样其实是spring将SpringDaoImpl对象实例化并且调用SpringAction的setSpringDao方法将SpringDao注入:

<!--配置bean,配置后该类由spring管理-->  
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">  
     <!--(1)依赖注入,配置当前类中相应的属性-->  
     <property name="springDao" ref="springDao"></property>  
</bean>  
<bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>  

 

构造器注入
这种方式的注入是指带有参数的构造函数注入,看下面的例子,我创建了两个成员变量SpringDao和User,但是并未设置对象的set方法,所以就不能支持第一种注入方式,这里的注入方式是在SpringAction的构造函数中注入,也就是说在创建SpringAction对象时要将SpringDao和User两个参数值传进来:

public class SpringAction {  
    //注入对象springDao  
    private SpringDao springDao;  
    private User user;  
      
    public SpringAction(SpringDao springDao,User user){  
        this.springDao = springDao;  
        this.user = user;  
        System.out.println("构造方法调用springDao和user");  
    }  
          
     public void save(){  
        user.setName("卡卡");  
        springDao.save(user);  
    }  
}  

 
在XML文件中同样不用<property>的形式,而是使用<constructor-arg>标签,ref属性同样指向其它<bean>标签的name属性:
<!--配置bean,配置后该类由spring管理--> 
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
        <!--(2)创建构造器注入,如果主类有带参的构造方法则需添加此配置--> 
        <constructor-arg ref="springDao"></constructor-arg> 
        <constructor-arg ref="user"></constructor-arg> 
    </bean>  
    <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>  
    <bean name="user" class="com.bless.springdemo.vo.User"></bean>  
解决构造方法参数的不确定性,你可能会遇到构造方法传入的两参数都是同类型的,为了分清哪个该赋对应值,则需要进行一些小处理:
下面是设置index,就是参数位置:
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction"> 
        <constructor-arg index="0" ref="springDao"></constructor-arg> 
        <constructor-arg index="1" ref="user"></constructor-arg> 
    </bean>  

  另一种是设置参数类型:
<constructor-arg type="java.lang.String" ref=""/>  

 
静态工厂的方法注入
静态工厂顾名思义,就是通过调用静态工厂的方法来获取自己需要的对象,为了让spring管理所有对象,我们不能直接通过"工程类.静态方法()"来获取对象,而是依然通过spring注入的形式获取:

package com.bless.springdemo.factory;  
  
import com.bless.springdemo.dao.FactoryDao;  
import com.bless.springdemo.dao.impl.FactoryDaoImpl;  
import com.bless.springdemo.dao.impl.StaticFacotryDaoImpl;  
  
public class DaoFactory {  
    //静态工厂  
    public static final FactoryDao getStaticFactoryDaoImpl(){  
        return new StaticFacotryDaoImpl();  
    }  
}  

 

同样看关键类,这里我需要注入一个FactoryDao对象,这里看起来跟第一种注入一模一样,但是看随后的xml会发现有很大差别:

 public class SpringAction {  
        //注入对象  
    private FactoryDao staticFactoryDao;  
      
    public void staticFactoryOk(){  
        staticFactoryDao.saveFactory();  
    }  
    //注入对象的set方法  
    public void setStaticFactoryDao(FactoryDao staticFactoryDao) {  
        this.staticFactoryDao = staticFactoryDao;  
    }  
}  

 
Spring的IOC配置文件,注意看<bean name="staticFactoryDao">指向的class并不是FactoryDao的实现类,而是指向静态工厂DaoFactory,并且配置factory-method="getStaticFactoryDaoImpl"指定调用哪个工厂方法:

<!--配置bean,配置后该类由spring管理-->  
    <bean name="springAction" class="com.bless.springdemo.action.SpringAction" >  
        <!--(3)使用静态工厂的方法注入对象,对应下面的配置文件(3)-->  
        <property name="staticFactoryDao" ref="staticFactoryDao"></property>  
    </bean>  
    <!--(3)此处获取对象的方式是从工厂类中获取静态方法-->  
    <bean name="staticFactoryDao" class="com.bless.springdemo.factory.DaoFactory" factory-method="getStaticFactoryDaoImpl"></bean>   

 
实例工厂的方法注入
实例工厂的意思是获取对象实例的方法不是静态的,所以你需要首先new工厂类,再调用普通的实例方法:

public class DaoFactory {  
    //实例工厂  
    public FactoryDao getFactoryDaoImpl(){  
        return new FactoryDaoImpl();  
    }  
}  

 

那么下面这个类没什么说的,跟前面也很相似,但是我们需要通过实例工厂类创建FactoryDao对象:

public class SpringAction {  
    //注入对象  
    private FactoryDao factoryDao;  
      
    public void factoryOk(){  
        factoryDao.saveFactory();  
    }  
  
    public void setFactoryDao(FactoryDao factoryDao) {  
        this.factoryDao = factoryDao;  
    }  
}  

  
最后看spring配置文件:

<!--配置bean,配置后该类由spring管理-->  
<bean name="springAction" class="com.bless.springdemo.action.SpringAction">  
    <!--(4)使用实例工厂的方法注入对象,对应下面的配置文件(4)-->  
    <property name="factoryDao" ref="factoryDao"></property>  
</bean>  
      
    <!--(4)此处获取对象的方式是从工厂类中获取实例方法-->  
<bean name="daoFactory" class="com.bless.springdemo.factory.DaoFactory"></bean>  
<bean name="factoryDao" factory-bean="daoFactory" factory-method="getFactoryDaoImpl"></bean>  
 

 

总结
Spring IOC注入方式用得最多的是(1)(2)种,多谢多练就会非常熟练。
        另外注意:通过Spring创建的对象默认是单例的,如果需要创建多实例对象可以在<bean>标签后面添加一个属性:
<bean name="..." class="..." scope="prototype">  
 
http://blog.csdn.net/u011225629/article/details/45459909

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值