Factory Beans in Spring using Annotations Example

Factory design patterns help you create objects without using the new keyword directly within your code. The factory intern will create object for you. You can read more about factory pattern @ Factory Design Patterns.

Important Annotations for Spring Core Tasks

This is a quick summary of important annotations in Spring for doing core tasks in Spring. Some of them might have already been covered in examples. Some of them might be covered later. Some of them might be a variation of the already covered ones, so please feel free to try them out and let us know if you face any issues.


@Configuration, @Bean

You can use @Configuration to specify a Java configuration file. 

Important Questions and Observations in Spring Framework 4 Essentials

  1. If you invoke getBean("writer") and there are two beans defined with the name writer in your xml configuration file or Java configuration file. What will happen?
    • It will return the second one defined.

Creating Your First Simple Maven Project in Eclipse

Creating a simple maven project using eclipse is very simple. Just a few clicks and you are done creating your first simple maven project. We will also quickly see some observations on the folders and files created by default after creating our simple project.


First, create a new Maven project using the eclipse new project wizard. You can either right click on the project explorer or go from the File menu item, to go to create new project wizard.

Autowiring in Spring

Through the autowiring feature, spring will intelligently guess some of the bean injection without needing to configure explicitly. Spring can do autowiring based on the type of the bean, name of the bean, and constructor of the bean. Default is autowire by type.

Your First Spring Boot Application

A Spring Boot standalone application uses the @SpringBootApplication  annotation over a class with a main  method that will execute the application. The method call accepts two parameters — the class that actually contains the annotated @SpringBootApplication annotation and any application arguments. The ApplicationArguments interface allows you to access any application arguments.


JUNIT Testing of Spring Code with Profiles Example

We can enable profiles from within a JUNIT test using @ActiveProfiles annotation. If you don't specify one, profile will be activated based on property value. This excercise writing JUNIT is applicable for code without profiles also: you just have to not activate any profiles.

Enabling Spring Profiles Through Property Example

We can use the property '' within a properties file to activate a profile and is the most preferred approach. 

In last note, we had seen enabling Spring profiles from the main method itself by calling getEnvironment().setActiveProfiles method on the AnnotationConfigApplicationContext object.

Your First Spring Profiles Based Program

@Profile annotation allow you to register components that will be selected only if its associated profile is active. You can selectively activate a set of beans in a class or all beans .

We will see how to do this using a Java configuration file using the @Profile annotation, and then we will also discuss about how to do it using XML configuration.


Important points about Spring Profiles

Your First Spring Program Using Component and ComponentScan Annotations

 We will create a simple class, configure it and test it. You can configure a Spring application through xml, or Java configuration or annotations along with xml or java configuration. In this example we will use @Component and @ComponentScan annotations along with Java configuration. Note that these annotations can also be used along with xml configuration and we will see how to do that in the end.


Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within!!!