What is IoC or Spring IOC?

There is a lot of confusion about the definition of the IoC container – some equate it with a design pattern called Dependency Injection – but in reality IoC is much larger than dependency injection.

Inversion of Control (IoC) is an object-oriented programming practice where the object coupling is bound at run time by an assembler object and is typically not known at compile time using static analysis.

The basic concept of the Inversion of Control pattern (also known as dependency injection) is that you do not create your objects but describe how they should be created. You don’t directly connect your components and services together in code but describe which services are needed by which components in a configuration file. A container (in the case of the Spring framework, the IOC container) is then responsible for hooking it all up.

Steps to use the inversion of control (IOC) with spring framework.

=> Create one .xml file to configure the components and services.

=> We need to define classes and its properties.

=> Mention the service with its component as per springs framework
guidelines so that Spring container can create a object for us.

Explanation:

Class

package com.techartifact.vinay;

public class RefClass {

                private RefClass newRefClass;

                public RefClass getRefClass() {

                                return newRefClass;

                }

                public void setNewRefClass(RefClass newRefClass) {

                                this.newRefClass = newRefClass;

                }

}

Configuration file


 <bean id="createRefClass" class="com.techartifact.vinay.RefClass">

        <property name="newRefClass"/>

 </bean>


Here RefClass is a class which has getter and setter for newRefClass property.

Now spring container takes the responsibility to create the object of RefClass class with object name newRefClass which defined in the property tag with name attribute and set the value automatically when it is used.

How to get Hotmail Contacts using Windows Delegated Authentication

From past one week i have been scratching head to fulfill one of my projects business requirement to get Hotmail Contacts email address using Windows Live Connect API but the LIVE connect developer SDK says due to privacy reasons we do not expose users contact email address. The Hotmail Contact API returns contact basic info along with EMAIL HASH which is of no use 🙁 .

After digging deep i found that there are couple of sites which are extracting Hotmail users contact Email Address. Like LinkedIn, EduGraph etc. On getting even deeper to understand how these sites are doing using some of the popular developer digging tools like the FIDDLER i got to know about the an OBSOLETE Microsoft API named DELEGATED AUTHENTICATION.

The Links related to Delegated Authentication clearly says :

This topic describes functionality that will be obsolete. This functionality is provided only to support legacy applications. Live Connect incorporates features that provide equivalent functionality.

Please note the last statement “Live Connect incorporates features that provide equivalent functionality”. BIG LIERS …….

On visiting LIVE connect developer guide reference it says:

Note  For user privacy reasons, we do not provide access to the email and address info of a user’s contacts.

Anyways somebody has to consolidate all this info for the benefit of others so let me take this opportunity for the sake of other developers…. 🙂

Steps to get Hotmail Contacts using Delegated Authentication:

Register Application

  1. Register your application using the link: RegisterApp.
  • Provide Application Name
  • Privacy Url – Note: The Url domain should be FQDN(Fully qualified domain name). You cannot provide “localhost” or IP in the Url.
  • Redirect domain – This Url will be the callback url of your application where microsoft will redirect user back after successful sign in and getting the necessary user consent for the data access in this case it will be “delauth-handler.aspx“. Note: The Url domain should be FQDN(Fully qualified domain name). You cannot provide “localhost” or IP in the Url.
  • Get the ClientId and ClientSecret. This will be the Gate pass for your application to communicate with Microsoft Contacts API.
  • Creating Sample App
    1. Create sample Website in IIS with fully qualified domain name. The domain name should be same as you provided in the RedirectUrl at the time of registering application. for ex: “www.HotmailLiveContacts.com”.
    2. Update the web.config file with:
    • wll_appid – This is the client Id you got after registering your application.
    • wll_secret – This is the client secret you got after registering your application.
    • wll_returnurl – This is the Redirect Url provided while registering your application.
    • wll_policyurl – This is the Policy Url provided while registering your application.
  • Get the consent url(login url) by initializing the “WindowsLiveLogin” class “GetConsentUrl” method. This is required to make the user login and provide necessary consent for the data access.
  • Extract the consent token from the response by calling”WindowsLiveLogin” class “ProcessConsent” method.
  • Screen shot for the Concent:

    ConsentToken from Microsoft

    Get Hotmail Contacts

    Now comes the final process for which the blog has been created. Getting the Hotmail contacts.

      1. Create a HttpWebRequest with the below URL
    • HttpWebRequest request = (HttpWebRequest)WebRequest.Create(string.Format(“https://livecontacts.services.live.com/users/@[email protected]{0}/rest/livecontacts”, intlid)); //intlid refers to LocationId
    • request.Headers.Add(“Authorization”, “DelegatedToken dt=\”” + token + “\”“); //token refers to Delegation Token.
  • Get the ResponseStream into the StreamReader in the form of XML.
  • There are couple of handy links which you can use:
    1. Manage registered applications: Click Here
    2. Delegated Authentication Error Codes: Click Here
    3. Manage you shared information. You can revoke access to this information at any time using: Click Here
    4. Link to the source code: Click Here
    Hope the article would be helpful!!! 🙂


    How to configure Circular dependencies in Spring

    Suppose you have a scenario like this :

    you have a class A which requires an instance of class B and class B requires instance of class A . So this is called circular dependencies and you are configuring this using constructor injection like this :

    Eg:

    public class A {	
    	private B b;	
    	
    	public A(B b){
            this.b=b;
           }		
    }
    
    public class B {
    	
    	private A a;
    	private int age;
    	
    	public B(A a, int age){
            this.b=b;
            this.age=age;
           }
    	public void show(){
    		 System.out.println("vaue of age is "+age);
    	}
    
    }
    
    

    And configuration will be like this :

    When you will try to run this program, it will throw BeanCurrentlyInCreationException.

    One possible solution to this issue is to edit the source code of some of your classes to be configured via setters instead of via constructors. Another solution is not to use constructor injection and stick to setter injection only.

    Solution :

    public class A {
    	
    	private B b;	
    	
    	
    
    	public B getB() {
    		return b;
    	}
    
    	public void setB(B b) {
    		this.b = b;
    	}
    	
    }
    
    public class B {
    	
    	private A a;
    	private int age;
    	
    		
    	public A getA() {
    		return a;
    	}
    
    	public void setA(A a) {
    		this.a = a;
    	}
    
    	public int getAge() {
    		return age;
    	}
    
    	public void setAge(int age) {
    		this.age = age;
    	}
    
    	public void show(){
    		 System.out.println("vaue of age is "+age);
    	}
    
    }
    
    

    Configuration will be like this.

    <?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">
    
           <bean id="a" class="study.spring.beans.A">
           <property name="b" ref="b"></property>        
           </bean>
           
           
           <bean id="b" class="study.spring.beans.B">
            <property name="a" ref="a"></property>
            <property name="age" value="24"></property>
           </bean>
    
    </beans>
    

    Enjoy Coding….

    Thanks