Category: Spring Core
-
Spring Bean Scopes
1. Introduction A bean definition acts like a recipie for creating the actual instances of the the class. You can control dependencies, configuration values and the scope of the objects created from a particular bean definition. The Spring Framework supports six scopes, four of which are available only if you use a web-aware ApplicationContext. You…
-
Java Based Container Configuration
1. Introduction In this tutorial, we’ll discuss the Java based container configuration in Spring. Two central artifacts in Spring’s Java configuration support are @Configuration annotated classes and @Bean annotated classes. 2. @Bean annotation The @Bean annotation signifies that a method creates, configures, and initializes a new object to be managed by the Spring IoC container.…
-
AnnotationConfigApplicationContext in Spring
1. Introduction Spring Container can be instantiated using AnnotationConfigApplicationContext. AnnotationConfigApplicationContext was introduced in Spring 3.0. AnnotationConfigApplicationContext is an implementation of ApplicationContext. This accepts @Configuration classes, @Component classes and classes annotated with JSR-330 metadata. When @Configuration classes are supplied, the @Configuration class is registered as a bean definition, and all its declared @Bean methods are also…
-
Spring @EntityScan vs. @ComponentScan
1. Introduction When creating Spring applications we might need to specify a list of packages that contain our entity classes. Similarly, we might need to specify list of Spring beans to be initialized. We use the annotations @EntityScan and @ComponentScan annotations. In this short tutorial, we’ll discuss the cases where @EntityScan and @ComponentScan annotations are…
-
Spring @Bean annotation
1. Introduction In this tutorial, we’ll discuss @Bean annotation which is a very important annotation to know. A Java object instance managed by Spring container is called bean. This annotation is typically used to define methods to create beans. You can use the @Bean annotation in a @Configuration-annotated or in a @Component-annotated class. A method annotated with @Bean registers…
-
@Controller and @RestController annotation in Spring
1. Introduction In this tutorial, we’ll discuss the difference between @Controller and @RestController annotations in Spring MVC. The @Controller annotation is available since Spring 2.5 whereas @RestController was introduced in Spring 4.0 to simplify creation of RESTful web services. 2. @Controller annotation This annotation is a specialization of @Component and indicates that the annotated class…
-
Spring @Primary annotation
1. Introduction In this quick tutorial, we’ll discuss Spring’s @Primary annotation. The @Primary annotation indicates that a bean should be given preference when multiple candidates are qualified to autowire a single-valued dependency. If there is exactly one bean annotated with @Primary among the candidates, it will be used for autowiring. This annotation may be used…
-
Spring @Qualifier annotation
1. Introduction In this tutorial, we’ll discuss what is the use of @Qualifier annotation and which problem it solves. In this tutorial we’ll also discuss how @Qualifier is different from @Primary annotation. 2. What problem does @Qualifier solve? While autowiring beans, Spring by default searches by types and does the bean injection. However, Spring may…
-
Circular dependency in Spring with example
Overview In this tutorial, we’ll discuss circular dependency in Spring with example. Circular dependency generate exception while creating beans. We’ll create a circular dependency and will see how to fix this. Circular dependency in Spring Before looking at circular dependency, we need to understand how beans are created by container. Container is intelligent enough to…
-
Overview of dependency injection in Spring
Overview In this tutorial, we’ll take a look at the concept of Dependency Injection and IoC (Inversion of Control). We’ll see types of Dependency Injection in Spring and how it is implemented in Spring. We’ll have separate tutorials for each type of Dependency Injection. In this tutorial, we’ll look at all these topics briefly. Inversion…