Spring依赖注入方式:
第一种
public class ServiceImpl implements Service(){
private Dao dao;
public vois setDao(Dao dao){
this.dao = dao;
}
}
对应的applicationContext.xml为
<beans>
<bean id = "service" class="xxx.xxx.serviceImpl">
<property name="dao"/>
<ref local = "dao"/>
</property>
</bean>
<bean is ="dao" class="xxx.xxx.daoImpl">
</bean>
</beans>
第二种:
public class ServiceImpl implements Service{
private final Dao dao;
public ServiceImpl(Dao dao){
this.dao = dao;
}
}
对应的applicationContext.xml为 <beans>
<bean id ="serviceImpl" class="xxx.xxx.ServiceImpl">
<constructor-arg>
<ref local = "dao"/>
</constructor-arg>
</bean>
<bean is ="dao" calss="xxx.xxx.Dao">
</bean>
</beans>
第三种
public abstract class ServiceImpl implements Service{
protected abstract Dao getDao();
}
}
对应的applicationContext.xml为
<beans>
<bean id ="serviceImpl" class="xxx.xxx.ServiceImpl">
<lookup-method name="getDao" bean="dao">
</bean>
<bean is ="dao" singleton="false" class="xxx.xxx.StatefulDataDaoImpl">
</bean>
</beans>
Spring依赖注入方式:
最新推荐文章于 2024-11-03 09:30:00 发布