0% found this document useful (0 votes)
16 views5 pages

Spring Core Prepapred

The document outlines various Dependency Injection (DI) and Context Configuration annotations in the Spring Framework, including @Autowired, @Qualifier, @Primary, and @Profile. It explains how these annotations facilitate the management of bean dependencies and configurations, providing examples for clarity. Additionally, it discusses stereotype annotations like @Component, @Service, @Repository, and @Controller, which are used to define Spring-managed components in an application.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

Spring Core Prepapred

The document outlines various Dependency Injection (DI) and Context Configuration annotations in the Spring Framework, including @Autowired, @Qualifier, @Primary, and @Profile. It explains how these annotations facilitate the management of bean dependencies and configurations, providing examples for clarity. Additionally, it discusses stereotype annotations like @Component, @Service, @Repository, and @Controller, which are used to define Spring-managed components in an application.

Uploaded by

Bl Gocher
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

DI-Related Annotations

Context Configuration Annotations


1. DI-Related Annotations
Some of the DI-Related Annotations that are available in Spring Framework are
listed below

@Autowired
@Qualifier
@Primary
@Bean
@Lazy
@Required
@Value
@Scope
@Lookup, etc.
@Autowired Annotation
@Autowired annotation is applied to the fields, setter methods, and constructors.
It injects object dependency implicitly. We use @Autowired to mark the dependency
that will be injected by the Spring container.

Examples

1.1: Field injection

Java
class Student {
@Autowired
Address address;
}
1.2: Constructor injection

Java
class Student {
Address address;

@Autowired
Student(Address address) {
this.address = address;
}
}
1.3: Setter injection

Java
class Student {
Address address;

@Autowired
void setaddress(Address address) {
this.address = address;
}
}
@Qualifier Annotation
The @Qualifier annotation is used to resolve the autowiring conflict when there are
multiple beans of the same type. The @Qualifier annotation can be used on any class
annotated with @Component or on methods annotated with @Bean. This annotation can
also be applied to constructor arguments or method parameters.

Illustration:
Java
public interface Vehicle {
public void start();
}
Implementation:

Suppose there are two beans, Car and Bike implements Vehicle interface

Example:

Java
@Component(value="car")
public class Car implements Vehicle {

@Override
public void start() {
System.out.println("Car started");
}

@Component(value="bike")
public class Bike implements Vehicle {

@Override
public void start() {
System.out.println("Bike started");
}

}
So in this case, if you want to inject the Bike bean in VehicleService then you
must use @Autowired with @Qualifier annotation. If you didn’t use @Qualifier, it
will throw NoUniqueBeanDefinitionException.

Example:

Java
@Component
public class VehicleService {

@Autowired
@Qualifier("bike")
private Vehicle vehicle;

public void service() {


vehicle.start();
}
}
@Primary Annotation
This indicates that a particular bean should be given preference when multiple
beans are candidates to be autowired to a single-valued dependency. If exactly one
‘primary’ bean exists among the candidates, it will be the autowired value. Let’s
understand this statement with an example

Example: In some cases, we need to register more than one bean of the same type. In
this example Employee1() and Employee2() beans of the Employee type:

Java
@Configuration
public class Config {

@Bean
public Employee Employee1() {
return new Employee("Employee1");
}

@Bean
public Employee Employee2() {
return new Employee("Employee2");
}
}
In this case, if we try to run the application Spring will throw
NoUniqueBeanDefinitionException. To resolve this issue Spring offers the @Primary
annotation.

Java
@Configuration
public class Config {

@Bean
public Employee Employee1() {
return new Employee("Employee1");
}

@Bean
@Primary
public Employee Employee2() {
return new Employee("Employee2");
}
}
In the above code, we mark Employee2() bean with @Primary. Spring will inject
Employee2() bean preferentially over the Employee1().

2. Context Configuration Annotations


Some of the Context Configuration Annotations that are available in Spring
Framework are listed below

@Profile
@Import
@ImportResource
@PropertySource, etc.
@Profile Annotation
If you want Spring to use a @Component class or a @Bean method only when a specific
profile is active then you can mark it with @Profile.

Example

Java
@Component
@Profile("developer")
public class Employee {}
@Import Annotation
Spring @Import annotation imports one or more @Configuration classes. It is
equivalent to <import> element in Spring XML. If the application is using
@ComponentScan, then all @Configuration classes will automatically be scanned and
no need to use @Import annotation. But in some cases, we want to scan only selected
@Configuration classes and in this case, @Import annotation is useful.
Example:

Java
@Configuration
@Import(Conf1.class)
public class Config {
------
}
We can also import more than one configuration class as follows.

Java
@Configuration
@Import({Conf1.class, Conf2.class})
public class Config {
------
}
@PropertySource Annotation
Spring @PropertySource annotation is used to provide properties file to Spring
Environment. This annotation is used with @Configuration classes.

Example:

Java
@Configuration
@PropertySource("classpath:db.properties")
public class DBConfiguration {
//...
}
This annotation is repeatable, which means we can have multiple PropertySource on a
Configuration class. This feature is available if you are using Java 8 or higher
versions.

Example:

Java
@Configuration
@PropertySource("classpath:db.properties")
@PropertySource("classpath:root.properties")
public class DBConfiguration {
//...
}

-----------------------------------------------------

Spring – Stereotype Annotations

Spring is one of the most popular Java EE frameworks. It is an open-source


lightweight framework that allows Java EE 7 developers to build simple, reliable,
and scalable enterprise applications. This framework mainly focuses on providing
various ways to help you manage your business objects. Now talking about Spring
Annotation, Spring Annotations are a form of metadata that provides data about a
program. Annotations are used to provide supplemental information about a program.
It does not have a direct effect on the operation of the code they annotate. It
does not change the action of the compiled program.

Stereotype Annotations
Spring Framework provides us with some special annotations. These annotations are
used to create Spring beans automatically in the application context. @Component
annotation is the main Stereotype Annotation. There are some Stereotype meta-
annotations which is derived from @Component those are

@Service
@Repository
@Controller
1: @Service: We specify a class with @Service to indicate that they’re holding the
business logic. Besides being used in the service layer, there isn’t any other
special use for this annotation. The utility classes can be marked as Service
classes.

2: @Repository: We specify a class with @Repository to indicate that they’re


dealing with CRUD operations, usually, it’s used with DAO (Data Access Object) or
Repository implementations that deal with database tables.

3: @Controller: We specify a class with @Controller to indicate that they’re front


controllers and responsible to handle user requests and return the appropriate
response. It is mostly used with REST Web Services.

So the stereotype annotations in spring are @Component, @Service, @Repository, and


@Controller.

@Component Annotation

@Component is a class-level annotation. It is used to denote a class as a


Component.
We can use @Component across the application to mark the beans as Spring’s managed
components.
A component is responsible for some operations.

You might also like