Annotation_Based_Configuration_-Notes_lyst8457
Annotation_Based_Configuration_-Notes_lyst8457
Below is the configuration file in case you want to use annotation in your
application:
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
">
<context:component-scan
base-package=””></context:component-scan>
</beans>
<context:component-scan
base-package="com.tapacad.spring"></context:component-scan>
// com.tapacad.spring is the package name where all the
classes are available
annotationApplicationContext.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/contex
t"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xs
d
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-contex
t.xsd">
<context:component-scan
base-package="com.tapacad.spring"></context:component-scan>
</beans>
Step 2: Now we need to make class as bean using @component
annotation
@Component is an annotation that allows Spring to automatically
detect our custom beans.
BMW.java
@Component("bmw") // Now BMW class is a bean
public class BMW implements Car
{
//Logic
}
Nano.java
@Component("nano")
public class Nano implements Car
{
}
Bean represented for audi in xml is replaced by @component(“audi”) as
shown below
Audi.java
@Component("audi")
public class Audi implements Car
{
//Logic
}
RocketEngine.java
@Component("engine")
public class RocketEngine implements Engine
{
//Logic
}
Step 3: Now let’s try to load annotationApplicationContext.xml file and
print object reference
MyApp7.java
package com.tapacad.spring;
import
org.springframework.context.support.ClassPathXmlApplication
Context;
public class MyApp7 {
public static void main(String[] args) {
// Load annotation Application context
ClassPathXmlApplicationContext context = new
ClassPathXmlApplicationContext("annotateApplicationContext.
xml");
// Get bean
Car car = context.getBean("audi", Car.class);
// call getCarDetails()
System.out.println(car);
// close context
context.close();
}
}
Output:
If you observe from the above output you have got object reference of audi.
This is only inversion of control in case of annotations
Dependency Injection Using Annotations
Scenario:
Now let’s try to get bmw bean and rocket engine bean that we have declared
in bmw
MyApp8.java
package com.tapacad.spring;
import
org.springframework.context.support.ClassPathXmlApplication
Context;
// Get bean
Car car = context.getBean("bmw", Car.class);
// close context
context.close();
}
Output: