Sunday, 25 February 2018

6.0 IoC using Java Annotations

What are Java Annotations ?




  1. special labels/markers added to java class
  2. provide meta-data about the class
  3. Processed at compile time or run time


//below annotation will make sure that you are overriding the display method.
@Override
public void display()
{
    System.out.println("Hello World");
}
  • Annotations minimizes the xml configurations.

Example
-----------------------------------------------------------------------------------------
BaseBallCoach.java
-----------------------------------------------------------------------------------------

package com.demo.spring;

import org.springframework.stereotype.Component;

@Component("MyCoach")
//instead of this we can use onl @Component then the bean id will be baseBallCoach
public class BaseBallCoach implements Coach {
@Override
public String getDailyWorkout() {
return "practice 30 mins daily";
}

}


-----------------------------------------------------------------------------------------
Coach.java
-----------------------------------------------------------------------------------------
package com.demo.spring;

public interface Coach {
public String getDailyWorkout();
}


-----------------------------------------------------------------------------------------
MyApp.java
-----------------------------------------------------------------------------------------
package com.demo.spring;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MyApp {

public static void main(String[] args) {
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
Coach obj=context.getBean("MyCoach",Coach.class);
System.out.println(obj.getDailyWorkout());
context.close();
}

}


-----------------------------------------------------------------------------------------
applicationContext.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" 
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd">

 <!-- initializing component scanning -->
 <context:component-scan base-package="com.demo.spring"></context:component-scan>

</beans>



-----------------------------------------------------------------------------------------
OUTPUT
-----------------------------------------------------------------------------------------

Feb 25, 2018 10:54:56 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 22:54:56 IST 2018]; root of context hierarchy
Feb 25, 2018 10:54:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
practice 30 mins daily
Feb 25, 2018 10:54:58 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@41975e01: startup date [Sun Feb 25 22:54:56 IST 2018]; root of context hierarchy



No comments:

Post a Comment