Categories
Spring Spring Core

Java-Based Container Configuration

In our previous Articles we saw XML Based container configuration and Annotation based container configuration in detail. In this Article we are going to look at how Spring applications can be configured using Java classes and few annotations with a simple example.  Two main annotations on Java based container configuration in Spring are @Configuration and […]

Categories
Spring Spring Core

Dependency Injection using @Inject and @Named Annotations

We have many ways to Inject Dependencies in Spring. XML Configuration, @Autowired Annotation, @Resource annotation are few of them. Among these methods we have JSR 330 Standard annotations as well. We can choose any one of these based on our application needs. Key Points : @Inject can be used instead of @Autowired and @Resource annotations […]

Categories
Spring Spring Core

Generics as Auto-wiring Qualifiers

When we are using more than one implementation for a Type, we use @Qualifier annotation as explained in this article. We don’t want to use @Qualifier annotation when using Generics as shown in the below example. Step 1 : Create Maven project and name it as GenericsAsQualifiers and copy below dependencies in pom.xml file. Step […]

Categories
Spring Spring Core

Spring Annotation Based container configuration

In Spring Framework we have three different ways to configure an IoC Container. XML Based Container Configuration Bean configuration will be in the form of XML meta-data. Refer this example for better understanding. Annotation Based Container Configuration Annotations will be used to configure each bean and its properties. Java Based Container Configuration Using Java Based […]

Categories
Spring Spring Core

Stereotype Annotations in Spring and Spring Boot

The @Component, @Controller, @Service and @Repository are the Stereotype annotations of Spring and Spring Boot Instead of providing Bean Definitions in XML Configuration file Spring provides these Stereotype annotations that can be defined in Java class itself.  Spring Automatically detects Stereotyped classes and configured with the ApplicationContext. As per the above diagram, @Controller, @Service and […]

Categories
Spring Spring Core

Custom Qualifier Annotation

In this article I’m going to explain how to create customized Qualifiers in spring with example code. Instead of using @Qualifier annotation with name as a parameter everywhere, you can customize the Qualifier annotation for you based on the nature of your application. For Example : Mine is a Course management application for colleges. Every […]

Categories
Spring Spring Core

Dependency Injection with @Resource annotation

Key Terms As like Autowired annotation Resource will resolve and inject dependencies.  Unlike Autowired annotation, it will search by Name. Means, It will take Field Name and Setter-Method parameter name to resolve dependencies. Name or ID of the Bean should be provided in XML or @Bean configuration. XML <bean id=”…” class=”…”></bean> @Bean @Bean(“…”) You can […]

Categories
Spring Spring Core

Spring Qualifier Annotation

Generally Autowired annotation will resolve dependencies based on the Type. If you are having only one Class implementing an interface, then Spring will make use of the Class implementation to resolve dependency. But, in most of the scenarios we have more than one Implementation classes for an Interface. If you use @Autowired in this case, […]

Categories
Spring Spring Core

Auto-wiring with Example

In Spring you can Inject Dependencies either in the form of XML or Annotation or Java Code. In this Article we are going to look at how Injection made possible using Autowired annotation and different possible types of Autowiring in Spring with working examples. Auto-wiring in Spring Types Autowiring by Constructor Autowiring by Setter-Method Autowiring […]

Categories
Spring Spring Core

Bean Definition Inheritance

Inheritance in Java : To Inherit all the Properties and Methods of a Parent Class to Sub-Class. The same definition applies to the Spring Bean Definition Inheritance concept. Child Bean Definition can inherit the Constructor-args, property values, static factory method name, init-method name and so on from the Parent Bean Definition. From the above snippet […]

Categories
Spring Spring Core

Spring Bean Life Cycle Callback Methods

As per our Bean Definition in the form of “XML, Java Code or Annotation” the IoC Container will Create, Assemble and Instantiate the Bean Objects for you and it destroys when the Container is Shutdown in case of Singleton and it destroys when Object is no longer needed in case of Prototype Scope.  If you […]

Categories
Spring Spring Core

Singleton vs Prototype Scope

In this Article we will look into the Difference Between Singleton and Prototype Scoped Beans in Spring with Example. Bean Scopes means, to Define the Lifecycle and Visibility of a Bean in the Spring application context.  Spring Framework Supports 6 different Scopes, among these Singleton and Prototype Scope can be used in Spring Core and […]

Categories
Spring Spring Core

Lazy Initialization

The Term Lazy initialization means to initialize the Object When needed or when requested.  Spring IoC Container will create and initialize all the Singleton Beans while Creating the IoC Container itself.  If we don’t want to initialize our Bean at the time of Container initialization, we can use Lazy-Init concept in Spring.  Key Points IoC […]

Categories
Spring Spring Core

Collection as Dependency Injection

As like Primitives and User Defined classes we can provide Collections as Dependencies in Spring Framework.  We will look at Collections as Dependencies in Spring with Constructor and Setter Injection in a Simple working example. In the below example I included all the possible Collections with Static and Dynamic values.  Before walkin to the Example […]

Categories
Spring Spring Core

Primitives as Dependency in Spring

In Spring Based application all the Objects are Created and Managed by IoC Container. For any Object we have dependency of another Object or Primitives in general. We are going to see a Detailed explanation of “How Primitives are used as Dependencies in Spring Framework?” in this Article. Constructor-args The above code having all available […]

Categories
Spring Spring Core

Control Bean Creation Order in Spring

Generally we provide dependencies of a Bean using the <ref/> attribute in Spring Configuration Meta-Data as like below. The “employeeController” bean is directly depending on the “employeeManager” bean. But, all the dependencies of a Bean will not direct like this. Some dependencies will be indirectly referred into the Bean Object. For Example : EmployeeController class […]

Categories
Spring Spring Core

How Dependency Injection works in Spring

We saw What is DI and its different form of Injection in our Previous Post, here we look at how Spring Dependency Injection works in Detail with Diagrammatic Representation. Step 1 : ApplicationContext will be Created and Configured by Reading Configuration Meta-Data.  Step 2 :  ApplicationContext will create all the Beans defined in Configuration Meta-Data. […]

Categories
Spring Spring Core

Instance Factory Method in Spring

The IoC container will invoke a non-static method of an Existing Bean to Instantiate an Object.  In our previous example we saw how Instantiation of Bean using Constructor args and Static-Factory Method with an Example. Now we will look at how Instantiation of Instance Factory Method works in Spring with an Example. Example of Instance […]

Categories
Spring Spring Core

Static Factory Method in Spring

In our previous article we saw how to instantiate a Bean with Constructor in Spring Framework. Now we will look at how we can instantiate a Static Factory Method.  Instantiate a Static Factory Method in Java  In a plain Java application we can instantiate the above Static Factory class as like below from another class […]

Categories
Spring Spring Core

Dependency Injection in Spring

Dependency Injection is a process to change the Binding of Objects from Compile time to Runtime. It focuses on creating loosely coupled objects and to make decoupling easier, Objects define their own dependent Objects and there will not be any tight-coupling between them. IoC container is responsible to create a Dependent objects and Inject where […]