`

五 依赖注入

 
阅读更多
五 依赖注入
1>set注入方式
1 对象注入 
先看接口 PersonDao.java
package cn.itcast.service;

public interface PersonDao {

	public void add();

}

PersonService
package cn.itcast.service;

public interface PersonService {

	public void save();

}


实现类 
PersonDaoBean.java

package cn.itcast.service.impl;

import cn.itcast.service.PersonDao;

public class PersonDaoBean implements PersonDao {
    /* (non-Javadoc)
	 * @see cn.itcast.service.impl.PersonDao#add()
	 */
    public void add(){
    	System.out.println("我是PersonDaoBean里的add方法");
    }
}

PersonServiceBean.java
package cn.itcast.service.impl;

import cn.itcast.service.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {
    private PersonDao persondao;
	public PersonDao getPersondao() {
		return persondao;
	}
	public void setPersondao(PersonDao persondao) {
		this.persondao = persondao;
	}
	public void save(){
		System.out.println("我是在PersonServiceBean中的save()方法");
		persondao.add();
	}
	public void init(){
		System.out.println("我是在PersonServiceBean中的初始化的方法");
	}
	public void destroy(){
		System.out.println("我是在PersinServiceBean中的释放资源的方法");
	}
}




配置文件 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
      <!-- 依赖对象 -->
      <!-- 方式一 set方法注入  -->
      <!--  
      <bean id="persondao" class="cn.itcast.service.impl.PersonDaoBean"></bean>    
      <bean id="personservice" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destroy">
      <property name="persondao" ref="persondao"></property>
      </bean>
      -->
      
        <!-- 方式二(使用内部bean,但该bean不能被其他bean使用) -->
        <bean id="personservice" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destroy">
        <property name="persondao">
        <bean class="cn.itcast.service.impl.PersonDaoBean"></bean>
        </property>
        </bean>
</beans>


执行代码
AbstractApplicationContext tx=new ClassPathXmlApplicationContext("beans.xml");
PersonService personservie=(PersonService)tx.getBean("personservice");
personservie.save();
tx.close();

2 基本类型数据注入


<bean id="personservice" class="cn.itcast.service.impl.PersonServiceBean" init-method="init" destroy-method="destroy">
 <property name="persondao">
 <bean class="cn.itcast.service.impl.PersonDaoBean"></bean>   
 </property>
  <!-- 基本类型数据注入 -->
  <property name="name" value="itcast"></property>   
 </bean>


 实现类修改
 package cn.itcast.service.impl;

import cn.itcast.service.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {
    private PersonDao persondao;
    private String name;
	public PersonDao getPersondao() {
		return persondao;
	}
	public void setPersondao(PersonDao persondao) {
		this.persondao = persondao;
	}
	public void save(){
		System.out.println("我是在PersonServiceBean中的save()方法");
		System.out.println("name="+name);
		persondao.add();
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public void init(){
		System.out.println("我是在PersonServiceBean中的初始化的方法");
	}
	public void destroy(){
		System.out.println("我是在PersinServiceBean中的释放资源的方法");
	}
}

测试 
AbstractApplicationContext tx=new ClassPathXmlApplicationContext("beans.xml");
PersonService personservie=(PersonService)tx.getBean("personservice");
personservie.save();
tx.close();


3 集合属性注入
Set集合属性注入
 <!-- 集合注入 -->
         <property name="set">
         <set>
         <value>第一个</value>
         <value>第二个</value>
         <value>第三个</value>
         </set>
         </property>
         <property name="list">
         <list>
           <value>list集合1</value>
           <value>list集合2</value>
           <value>list集合3</value>
         </list>
         </property>
         <property name="p">
         <props>
         <prop key="who is he?">我是谁</prop>
         <prop key="do you know">你懂吗</prop>
         <prop key="that's goodidear">好主意</prop>
         </props>
         </property>
         <property name="map">
         <map>
         <entry key="xuchunrong" value="许春荣"></entry>
         <entry key="huhuan" value="huhuan"></entry>
         <entry key="nihao" value="李好"></entry>
         </map>
         </property>




2>构造器注入方式
1 利用构造器注入方式注入属性和对象值
配置
  <bean id="persondao" class="cn.itcast.service.impl.PersonDaoBean"></bean>
          <bean id="personService" class="cn.itcast.service.impl.PersonServiceBean">
          <constructor-arg index="0" ref="persondao"></constructor-arg>
          <constructor-arg index="1" value="xuchunrong"></constructor-arg>
</bean>

接口
package cn.itcast.service;

public interface PersonDao {

	public void add();

}


package cn.itcast.service;

public interface PersonService {

	public void save();

}


实现类
package cn.itcast.service.impl;

import cn.itcast.service.PersonDao;

public class PersonDaoBean implements PersonDao {
    /* (non-Javadoc)
	 * @see cn.itcast.service.impl.PersonDao#add()
	 */
    public void add(){
    	System.out.println("我是PersonDaoBean里的add方法");
    }
}




package cn.itcast.service.impl;

import cn.itcast.service.PersonDao;
import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {
    private PersonDao persondao;
    private String name;
	public void save(){
		System.out.println("我是save()方法");
		this.persondao.add();
		System.out.println("name----------->"+name);
		
	}
	public PersonServiceBean(PersonDao persondao, String name) {
		this.persondao = persondao;
		this.name = name;
	}

}



测试类
AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personService");
personService.save();


3>使用Field注入(用于注解方式)

 

分享到:
评论

相关推荐

    c# 依赖注入 控制反转

    控制反转 依赖注入的c#实现,很好的教程。

    C# (.NET Core 6.0) DI依赖注入 示例

    较为框架式的演示了DI服务依赖注入 适用于熟悉C#中的继承,基本的Core命令使用等. 该示例演示了以服务器集群为背景的各种业务的依赖注入. 示例来自于B站杨中科老师的.NET Core(6.0)教学.

    一个轻量级的PHP依赖注入框架

    ✨一个轻量级的PHP依赖注入框架,让你自定义的方法也可以使用依赖注入.

    .NET Core中的一个接口多种实现的依赖注入与动态选择看这篇就够了

    最近有个需求就是一个抽象仓储层接口方法需要SqlServer以及Oracle两种实现方式,为了灵活我在依赖注入的时候把这两种实现都给注入进了依赖注入容器中,但是在服务调用的时候总是获取到最后注入的那个方法的实现,这...

    详解 Spring 3.0 基于 Annotation 的依赖注入实现

    详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。

    Spring系列之依赖注入的三种方式.docx

    对于spring配置一个bean时,如果需要给该bean提供一些初始化参数,则需要通过依赖注入方式,所谓的 依赖注入就是通过spring将bean所需要的一些参数传递到bean实例对象的过程(将依赖关系注入到对象中) ,spring的...

    java中spring依赖注入的简单例子

    javaEE 开发中 现在最成熟的框架之一应该就是spring了 spring框架最强大的地方就是实现了依赖注入 也叫控制反转 最近的一个项目中用的就是 spring框架 spring框架是工厂模式的应用结合了MVC的设计思想 大家可以...

    IoC 依赖注入 技术总结

    IoC 依赖注入 技术总结 IoC 依赖注入 技术总结

    依赖注入那些事儿

    网上下载整理的关于依赖注入的介绍。循序渐进,非常不错。

    C++ 依赖注入

    软件设计是控制依赖关系的方法和方式。其中依赖注入起很重的作用。依赖注入是简单,但非常强大的设计模式。本文介绍依赖注入的概念,并介绍简单且实用的C++依赖注入容器的一种实现方法。

    Spring Ioc 注解 依赖注入

    Spring Ioc 注解 依赖注入

    PHP依赖注入容器库

    简介:一个小的依赖注入容器

    Angular6依赖注入Demo

    Angular6依赖注入Demo,包含懒加载实现。 Angular6依赖注入Demo,包含懒加载实现。

    Entity Framework Repository(含依赖注入)

    Entity Framework Repository(含依赖注入)

    [依赖注入] 依赖注入 实战 (英文版)

    [Manning Publications] 依赖注入 实战 (英文版) [Manning Publications] Dependency Injection (E-Book) ☆ 图书概要:☆ Dependency Injection is an in-depth guide to the current best practices for using ...

    Unity依赖注入快速入门

    Unity为微软推出的一个轻量级依赖注入容器,内带了一个名为StopLight的快速入门示例,基于WindowsForm技术构建,里面有大量的冗余代码。我将全部程序用WPF技术进行了重构,保留使用Unity的全部技术和程序的全部功能...

    模仿Spring依赖注入

    模仿Spring依赖注入,代码详细,简单,明了

    .NET Autofac依赖注入

    依赖注入是做什么用的? 依赖注入又称之为控制反转(Inversion of Control,英文缩写为IoC)是一个重要的面向对象编程的法则来削减计算机程序的耦合问题,也是轻量级的Spring框架的核心。 控制反转一般分为两种类型...

    spring依赖注入底层详解

    spring依赖注入底层详解,很不错的资源,欢迎大家来下载学习。

    SSH笔记-泛型依赖注入

    SSH笔记-泛型依赖注入,当继承类完毕之后,根据泛型依赖注入的特性,被继承类会参照继承类所引用了的其他的引用关系自动建立对应引用关系,这就是泛型依赖注入

Global site tag (gtag.js) - Google Analytics