`

spring 依赖注入的几种类型<一>

 
阅读更多
  spring 依赖注入:
关于ioc依赖注入方式,解决了在java程序中使用new 方式来产生对象,实现了解耦。
下面将结合代码 实现类对应属性与配置文件的映射,对于list,map,等属性映射做了测试。

下面bean 代码只下了属性,构造函数以及set,get 方法省略
public class StudentBean {

	private int id;
	private String name; 
	private BoxBean box; // 对象类型映射
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}

 // 类 pencilBean 


通过属性依赖注入方式:

 <bean id ="stud1" class="com.xxg.bean.StudentBean">
  <property name="id" value ="1"></property>
  <property name="name" value ="xxg"></property>
  [color=red]<property name="box" ref ="box1"></property>[/color]
</bean>

其中划红线的属性为一个对象,这个对象在对应生成的配置文件为:
<bean id ="box1" class ="com.xxg.bean.BoxBean">
   <property name ="color" value="红色"></property>
   <property name ="name" value ="飞哥书包"></property>
   <property name ="pencils"> 
       <list>   
         <ref bean="pencil1"></ref>   <!--list里面装的是引用类型用   <ref bean="pencil1"> </ref>  pencil1 为一个对象 形式 -->
         <ref bean="pencil2"></ref>
         <ref bean="pencil3"></ref>
       </list>
     <!-- 
     
               如果list 里面装的是基本数据类型 
               则 用value设置   
     -->
      <list>
 			<value>aaaaaaaaa</value>
 			<value>bbbbbbbbb</value>
 		</list> 
      
       
   </property>
  
</bean>


public class PencilBean {
	
  private String type;
  private String name;
  private Map<String, String> map;
public Map<String, String> getMap() {
	return map;
}

配置文件如下:
注意关于map的配置

<bean id ="pencil1" class ="com.xxg.bean.PencilBean">   <!-- 相当于  PencilBean penclil1 = new PencilBeran()  -->
   <property name ="type" value="a1"></property>         <!--penclil1.setType("a1")  -->
   <property name ="name" value="美丽牌"></property>      <!--penclil1.setName("美丽牌")  -->

<property name ="map">
       <map >
         <entry>
            <key >
             <value>no1</value>  <!--Map key 的值  -->
            </key>  
           <value>声声道铅笔</value>  <!-- 该key 对应的值 -->
         </entry>
  
          <entry>
            <key >
             <value>no2</value>
            </key>  
           <value>二位keguan</value>
         </entry>
       </map> 
   </property>
 </bean>



通过构造函数依赖注入 SutentBean对象

<bean id ="stud2" class ="com.xxg.bean.StudentBean">
<constructor-arg type="int" value="2"></constructor-arg> <!--是int 类型但不能写成java.lang.Integer  -->
<constructor-arg type="java.lang.String" value ="yml"></constructor-arg>
<constructor-arg type ="com.xxg.bean.BoxBean" ref="box1"></constructor-arg>
</bean>


总结:关于依赖注入的配置文件 注意2点就Ok:
1.里面括号ref总共 在2个地方用到,一个是在映射属性为list类型且里面装的东西为对象或者引用类型 此时,用法为<ref bean ="指向对象的引用(也可以理解为该类产生的对象)简单理解为bean 的id值"></ref>

2.在当映射属性为对象类型时,此时ref用法为<property name="属性名" ref ="该属性对象类型的id值"></property


0
1
分享到:
评论

相关推荐

    开源框架 Spring Gossip

    &lt;br&gt;从代理机制初探 AOP &lt;br&gt;动态代理&lt;br&gt;&lt;br&gt;AOP 观念与术语 &lt;br&gt;Spring AOP &lt;br&gt;Advices &lt;br&gt;Advices 包括了Aspect 的真正逻辑,由于缝合至Targets的时机不同,Spring 提供了几种不同的 Advices。&lt;br&gt;Before ...

    spring学习:依赖注入的几种方式讨论

    NULL 博文链接:https://shmilyaw-hotmail-com.iteye.com/blog/2169569

    spring依赖注入的几种方式

    spring依赖注入的几种方式

    DBKING使用指南

    在dbking中,所有的数据库数据只有五种数据类型,String、Number(BigDecimal)、Timestamp、Clob(String)、Blob(byte[]),经过反复测试后,我们会例出各种数据库数据类型到这五种类型的映射表,当然我们也有...

    Spring 依赖注入的几种方式详解

    本篇文章主要介绍了Spring 依赖注入的几种方式详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring中依赖注入与容器

     依赖注入的几种形式 - 接口注入 - 构造器注入 - 属性(setter)注入  容器 容器是管理 组件的生命周期,注入组件(声明)所需的资源 例如: 容器 : 国家 义务教育、纳税、社保、医疗、养老 组件 : 公民 - ...

    Spring依赖注入的三种方式实例详解

    Spring依赖注入(DI)的三种方式,分别为: 1. 接口注入 2. Setter方法注入 3. 构造方法注入 下面介绍一下这三种依赖注入在Spring中是怎么样实现的。 首先我们需要以下几个类: 接口 Logic.java 接口实现类 ...

    Spring.net框架

    本部分代码仅仅提供一种功能演示,如果实际应用仍需进一步完善(建议使用一些成型的Ioc框架,例如Spring.net或Castle等)。经过改造后 的系统,组件间依赖关系如下图: 可以看出这次实现了真正的“针对接口编程”...

    Spring开发指南

    依赖注入的几种实现类型 Type1 接口注入 Type2 设值注入 Type3 构造子注入 几种依赖注入模式的对比总结 Spring Bean封装机制 Bean Wrapper Bean Factory ApplicationContext Web Context Spring 高级...

    springboot之yml配置文件信息加密.docx

    在 Spring Boot 项目中配置 Jasypt 需要进行以下几步:1)在 pom 文件中增加依赖:&lt;dependency&gt; &lt;groupId&gt;com.github.ulisesbocchio&lt;/groupId&gt; &lt;artifactId&gt;jasypt-spring-boot-starter&lt;/artifactId&gt; &lt;version&gt;1.8&lt;/...

    详析Spring中依赖注入的三种方式

    在开发的过程中突然对Spring的依赖注入几种方式出现混交,打算做个简单的小结,方便大家和自己以后参考借鉴,如有总结不对的地方,请大家不吝指教!下面来一起看看吧。

    Spring面试题含答案.pdf

    19. 有哪些不同类型的 IOC(依赖注入)方式? 20. 哪种依赖注入方式你建议使用,构造器注入,还是 Setter 方法注入? 21.什么是 Spring beans? 22. 一个 Spring Bean 定义 包含什么? 23. 如何给 Spring 容器提供...

    SpringFramework常见知识点.md

    - Spring依赖注入的方式有几种? - 一个bean的定义包含了什么?(BeanDefinition) - bean的作用域有哪些? - Spring 的扩展点主要有哪些? - Spring如何解决循环依赖? - 事务的传播行为是什么?有哪些? - 什么是AOP...

    25个经典的Spring面试问答

    Spring有几种配置方式 如何用基于XML配置的方式配置Spring 如何用基于Java配置的方式配置Spring 怎样用注解的方式配置Spring 请解释Spring Bean的生命周期 Spring Bean的作用域之间有什么区别 什么是Spring inner ...

    java面试Spring.pdf

    1. Spring 介绍 1.1 Spring 的优点 ...说一下Spring基于xml注入bean的几种方式? Spring如何解决循环依赖问题? Spring的自动装配 Spring框架中都用到了哪些设计模式? Spring框架中有哪些不同类型的事件?

    Spring面试专题.pdf

    6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、请解释 Spring Bean 的生命周期? 11、Spring Bean 的...

    Spring面试题.zip

    6、Spring 有几种配置方式? 7、如何用基于 XML 配置的方式配置 Spring? 8、如何用基于 Java 配置的方式配置 Spring? 9、怎样用注解的方式配置 Spring? 10、请解释 Spring Bean 的生命周期? 11、Spring Bean 的...

    Spring面试题大全

    3. 依赖注入和IoC:Spring框架使用依赖注入和IoC使得JDBC操作简单化。 4. 开源和免费:Spring框架是开源的免费的,使得对象管理集中化合简单化。 二、Spring中实现DI(Dependency Injection)的几种方式 在Spring...

    springIOC案例

    包含spring的和Junit的jar包,采用Junit测试,一共四个案例,主要讲述:1.怎么利用IOC容器获得实例化对象;2.IOC容器到底是如何工作 ...4.依赖和依赖注入的几种方式。以及一张&lt;&lt;IOC应用&gt;&gt;的ppt。

Global site tag (gtag.js) - Google Analytics