Notes spring - dependent manner in several injection of injection for different types of variables

Original link: http://www.cnblogs.com/fingerboy/p/5271914.html

  Inversion of Control and Dependency Injection are talking about a concept, just standing at different angles, so-called dependency injection:

  Refers to the operation of, the outer container dynamically dependent objects injected into the assembly. When the container starts spring, spring container initialization, create and manage bean object, and destroy it. So we just need to get directly from the container Bean object on the line, instead of writing code to create a bean object. This phenomenon is known as inversion of control, that is dependent on the application itself is not responsible for the maintenance and creation of objects, dependent objects created and maintained by the outer container is responsible. Such control would apply to the transfer from the outside of the container, transfer the so-called inversion of control.

  Here injection method attribute types:

  New Entity Classes Student:

public class Student {

    // basic data types of variables 
    Private String name;
     // object type of a variable, Address class code omitted 
    Private the Address address;
     // array type variable 
    Private String [] Hobby;
     // List type variable 
    Private List <String> Books ;
     // the Map type variable 
    Private the Map <String, String> Cards;
     // SET type variable 
    Private the Set <String> Movies;
     // the Properties type variable 
    Private the Properties info;
         public  void Show () {
        System.out.println("name:"+name+"地址:"+address.getAddr());
        System.out.println("爱好有:");
        for(int i=0;i<hobby.length;i++){
            System.out.println(hobby[i]);
        }
        System.out.println ( "Favorite books:" + Books);
        System.out.println ( "owned bank card" + Cards);
        System.out.println ( "Favorite Movies" + Movies);
        System.out.println ( "identity:" + info);
    }
// SET method omits 
}

Profile beans.xml:

<?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.xsd"> <bean id="address" class="com.wang.entity.Address"> <property name="addr" value="青岛"></property> </bean> <bean id="student" class="com.wang.entity.Student"> <!--配置基本数据类型 --> <property name="name" value="张三丰"></ ->Configuration object type <-!>Property <property name="address" ref="address"></property> <!--配置数组类型 --> <property name="hobby" > <array> <value>吃饭</value> <value>睡觉</value> <value>打豆豆</value> </array> </property> <!- <->Configuration type list Property name = "Books" > < List > < value > Time Traveler's Wife </ value > < value > Bride With White Hair </ value > < value > youth who are not confused </ value > </ List > < / Property > <-! configuration map types -> < Property name = "Cards" > < map > < entry Key = "ABC card" value="62284832128534"> </ Entry > < entry Key = "CCB card" value = "62,284,867,867,984" > </ entry > </ the Map > </ Property > <-! Configuration set type -> < Property name = "Movies" > < the SET > < value > crazy animals City </ value > < value > Ip Man 3 </ value > < value > Kung Fu Panda </value> </set> </ Property >
<-! Type Configuration properties ->
< Property name = "info" > < props > < prop Key = "Student ID" > 20.12428 million </ prop > < prop Key = "class" > three years second class </ prop > < prop Key = "gender" > unknown </ prop > </ The props > </ Property > </bean> </beans>

测试代码:

@Test
    public void test1(){
        ApplicationContext context=new ClassPathXmlApplicationContext("beans.xml");
        Student s=(Student)context.getBean(Student.class);
        s.show();
    }

打印结果:

name:张三丰地址:青岛
爱好有:
吃饭
睡觉
打豆豆
喜欢的书籍:[时光旅行者的妻子, 白发魔女传, 谁的青春不迷茫]
拥有的银行卡{农行卡=62284832128534, 建行卡=62284867867984}
喜欢的电影[疯狂动物城, 叶问3, 功夫熊猫]
身份信息:{学号=20124280, 性别=不详, 班级=三年二班}

转载于:https://www.cnblogs.com/fingerboy/p/5271914.html

Guess you like

Origin blog.csdn.net/weixin_30919571/article/details/94790517