Spring的注入类型(Injection type of Spring)

Set injection

Constructor injection

Static factory method injection

Instance factory method injection

refer:http://blessht.iteye.com/blog/1162131

In the usual java development,a method for programmer to relay on other classes in a class,
is usually a new dependent class to call the class instance method,this development model is the problem of new class instance is not a unified management,Spring put forward the idea of dependency injection.

Set Injection

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();  
    }  
}  

Configuration in XML

<!--配置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> 

Constructor injection

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);  
    }  
}  

Configuration in XML

    <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>  

****When the constructor has many parameters of the same type,bean does not known the descendants of the order of the parameters.

    <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>  
        <bean name="springDao" class="com.bless.springdemo.dao.impl.SpringDaoImpl"></bean>  
         <bean name="user" class="com.bless.springdemo.vo.User"></bean>  

Static factory method injection

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();  
    }  
}  
 public class SpringAction {  
        //注入对象  
    private FactoryDao staticFactoryDao;  

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

Configuration in XML

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

Instance factory method injection

public class DaoFactory {  
    //实例工厂  
    public FactoryDao getFactoryDaoImpl(){  
        return new FactoryDaoImpl();  
    }  
}  
public class SpringAction {  
    //注入对象  
    private FactoryDao factoryDao;  

    public void factoryOk(){  
        factoryDao.saveFactory();  
    }  

    public void setFactoryDao(FactoryDao factoryDao) {  
        this.factoryDao = factoryDao;  
    }  
}  

Configuration in XML

<!--配置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>       
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值