第四讲 依赖注入(Dependency Injection,简称DI)

一、依赖注入-Dependency Injection

      依赖:指bean对象的创建依赖于容器;bean对象的依赖资源。
      注入:指bean对象依赖的资源由容器来设置和装配。

二、Spring的注入

  1. 构造器注入

   见“第二讲 IoC创建对象的三种方式”。

  1. setter注入(重点

   要求被注入的属性 必须有setter方法。setter方法的方法名由:set+属性名首字母大写。如果属性是boolean类型,返回没有getter方法是
   is。

          常量注入:

public class Student {
     
      private String name ;
     
      public void setName(String name ) {
            this . name = name ;
     }
     
      public void show() {
           System. out .println( "name=" + name );
     }
     
}

      < bean id = "student" class = "com.liujie.model.Student" >
            <property name="name" value="张三"></property>
      </ bean >

          bean注入(对象注入):

      public void setAddress(Address address ) {
            this . address = address ;
     }

      < bean id = "address" class = "com.liujie.model.Address" >
            < property name = "address" value = "北京" ></ property >
      </ bean >
     
      < bean id = "student" class = "com.liujie.model.Student" >
            < property name = "name" value = "张三" ></ property >
            < property name = "address" ref="address" ></ property >
      </ bean >

          数组注入:

      public void setBooks(String[] books ) {
            this . books = books ;
     }

      < bean id = "student" class = "com.liujie.model.Student" >
            < property name = "name" value = "张三" ></ property >
            < property name = "address" ref = "address" ></ property >
            < property name = "books" >
                 <array>
                     <value>语文</value>
                     <value>英语</value>
                     <value>数学</value>
                </array>
            </ property >
      </ bean >

          List注入:

      public void setHobbies(List<String> hobbies ) {
            this . hobbies = hobbies ;
     }

            < property name = "hobbies" >
                 <list>
                     <value>羽毛球</value>
                     <value>乒乓球</value>
                     <value>玻璃球</value>
                </list>
            </ property >

          Map注入:

      public void setCards(Map<String, String> cards ) {
            this . cards = cards ;
     }

            < property name = "cards" >
                <map>
                     <entry key="中国银行" value="15602822222222222"></entry>
                     <entry>
                           <key><value>建设银行</value></key>
                           <value>6789990209833833</value>
                     </entry>
                </map>
            </ property >

          Set注入:

      public void setGames(Set<String> games ) {
            this . games = games ;
     }

            < property name = "games" >
                <set>
                     <value>lol</value>
                     <value>dota</value>
                     <value>cs</value>
                </set>
            </ property >

          Null注入:

      public void setWife(String wife ) {
            this . wife = wife ;
     }

            < property name = "wife" >
                 <null></null>
            </ property >

          Properties注入:

      public void setInfo(Properties info ) {
            this . info = info ;
     }

            < property name = "info" >
                 <props>
                     <prop key="学号">2015052601</prop>
                     <prop key="sex"></prop>
                     <prop key="name">小明</prop>
                </props>
            </ property >

          p命名空间注入:

public class User {
     
      private String name ;
      private int age ;
     
      public String getName() {
            return name ;
     }
      public void setName(String name ) {
            this . name = name ;
     }
      public int getAge() {
            return age ;
     }
      public void setAge( int age ) {
            this . age = age ;
     }
     
      @Override
      public String toString() {
            return "User [name=" + name + ", age=" + age + "]" ;
     }
     
}

< beans xmlns = "http://www.springframework.org/schema/beans"
      xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
      xmlns:p= "http://www.springframework.org/schema/p"
      xsi:schemaLocation = "http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd" >

      <!-- p命名空间注入,属性依然要设置setter方法 -->
      < bean id = "user" class = "com.liujie.model.User" p:name="李白" p:age="230" ></ bean >

          c命名空间注入:

public class User {
     
      private String name ;
      private int age ;
     
      public User() {
            super ();
     }
      public User(String name, int age ) {
            super ();
            this . name = name ;
            this . age = age ;
     }
     
      @Override
      public String toString() {
            return "User [name=" + name + ", age=" + age + "]" ;
     }
     
}

< beans xmlns = "http://www.springframework.org/schema/beans"
      xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
      xmlns:c= "http://www.springframework.org/schema/c"
      xsi:schemaLocation = "http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans.xsd" >

      <!-- c命名空间注入要求有对应参数的构造方法 -->
      < bean id = "user" class = "com.liujie.model.User" c:name = "杜甫" c:age = "280" ></ bean >
Editorial Reviews Product Description Dependency Injection is an in-depth guide to the current best practices for using the Dependency Injection pattern-the key concept in Spring and the rapidly-growing Google Guice. It explores Dependency Injection, sometimes called Inversion of Control, in fine detail with numerous practical examples. Developers will learn to apply important techniques, focusing on their strengths and limitations, with a particular emphasis on pitfalls, corner-cases, and best practices. This book is written for developers and architects who want to understand Dependency Injection and successfully leverage popular DI technologies such as Spring, Google Guice, PicoContainer, and many others. The book explores many small examples of anchor concepts and unfolds a larger example to show the big picture. Written primarily from a Java point-of-view, this book is appropriate for any developer with a working knowledge of object-oriented programming in Java, Ruby, or C#. About the Author Dhanji R. Prasanna is an Enterprise Java consultant for technologies such as EJB3, JBI, JSF, Guice, Spring, HiveMind, and PicoContainer. He is a co-author of the Bean Validation (JSR-303), JAX-RS (JSR-311), Servlet 3.0 (JSR-315), and JavaServerFaces 2.0 (JSR-314) specifications. He is also co-author of the Java EE 6.0 (JSR-316) platform specification, which is the next edition of J2EE. Product Details * Paperback: 352 pages * Publisher: Manning Publications; 1 edition (August 28, 2009) * Language: English * ISBN-10: 193398855X * ISBN-13: 978-1933988559 * Product Dimensions: 9.1 x 7.4 x 0.8 inches
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值