0% found this document useful (0 votes)
3 views

Annotation_Based_Configuration_-Notes_lyst8457

The document explains annotation-based configuration in the Spring Framework, allowing developers to define bean wiring using annotations instead of XML. It details the process of setting up a Spring application context with component scanning, using annotations like @Component to declare beans, and demonstrates dependency injection through example classes. The document also includes code snippets for configuring beans and retrieving them from the application context.

Uploaded by

seshuswaraj143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Annotation_Based_Configuration_-Notes_lyst8457

The document explains annotation-based configuration in the Spring Framework, allowing developers to define bean wiring using annotations instead of XML. It details the process of setting up a Spring application context with component scanning, using annotations like @Component to declare beans, and demonstrates dependency injection through example classes. The document also includes code snippets for configuring beans and retrieving them from the application context.

Uploaded by

seshuswaraj143
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Annotation Based Configuration:

In Spring Framework annotation-based configuration instead of using XML for


describing the bean wiring, you have the choice to move the bean
configuration into component class. It is done by using annotations on the
relevant class, method or the field declaration.

Below is the configuration file in case you want to use annotation in your
application:

<?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
">

<context:component-scan
base-package=””></context:component-scan>
</beans>

<context:component-scan> detects the annotations by package scanning. To


put it differently, it tells Spring which packages need to be scanned to look for
the annotated beans or components.
@Component, @Repository, @Service, @Controller, @RestController, and
@Configuration are several ones that <context:component-scan> can detect.
In base-package attribute we need to specify the package it need to scan for
annotations

Now let’s see Inversion Of control using annotations

Steps to Make annotation based Configuration


Step 1: Mention the base package in xml file

<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.

Add @component(mention the id that is specified in the configuration


file) to the class to represent it as bean to avoid xml configuration that
we used to do for bmw class as shown below

<bean id="bmw" class="com.tapacad.spring.BMW">


<constructor-arg ref ="engine"></constructor-arg>
</bean>

BMW.java
@Component("bmw") // Now BMW class is a bean
public class BMW implements Car
{
//Logic
}

Bean represented for nano in xml is replaced by @component(“nano”) as


shown below

<bean id="nano" class="com.tapacad.spring.Nano">


<constructor-arg ref ="engine"></constructor-arg>
</bean>

Nano.java
@Component("nano")
public class Nano implements Car
{

}
Bean represented for audi in xml is replaced by @component(“audi”) as
shown below

<bean id ="audi" class="com.tapacad.spring.Audi" >


<property name="rocketEngine" ref =
"engine"></property>
<property name="colour" value =
"${colour}"></property>
<property name="price" value = "${price}"></property>
</bean>

Audi.java
@Component("audi")
public class Audi implements Car
{
//Logic
}

Bean represented for Rocket Engine in xml is replaced by


@component(“engine”) as shown below

<bean id ="engine" class="com.tapacad.spring.RocketEngine">


</bean>

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;

public class MyApp8 {

public static void main(String[] args) {


// Load Application context
ClassPathXmlApplicationContext context =
new
ClassPathXmlApplicationContext("annotateApplicationContext.
xml");

// Get bean
Car car = context.getBean("bmw", Car.class);

// call getCarDetails() and getEngineDetail


System.out.println(car.getCarDetails());
System.out.println(car.getEngineDetail());

// close context
context.close();

}
Output:

If you observe from the above output getCarDetail() and getEngineDetail()


executed successfully .
The only reason for this is we have declared the attribute as RocketEngine
in bmw class so it is creating a bean of RocketEngine as well.

You might also like