Spring Core Prepapred
Spring Core Prepapred
@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
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;
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().
@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 {
//...
}
-----------------------------------------------------
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.
@Component Annotation