Starter Project

One of the most important features of Spring Framework is Dependency Injection.

Spring Framework helps in creating loosely coupled applications.

To be able to appreciate dependency injection, you should understand Tight coupling and how to create loosely coupled applications.

To create a simple Spring Project :

Go to start.spring.io and this is called Spring Initializr. [Spring Boot : Recommended version >= 2.0.0]

Give the Group Id as : org.rdayala.spring.basics
Artifact Id as : springboot-starter-project

Click on ‘Generate Project’ –> a zip file generated. Unzip the file to a folder and then import the project in to Eclipse.

File -> Import -> Existing Maven Projects -> select the folder where you unzipped zip file

src/main/java – where all Java sources will be written.
src/main/resources – where all the application properties will be residing.
src/test/java – unit tests will be written here

The good thing about creating a Spring Boot project is that, Spring is directly added in as a dependency.

In case if you get this error, when you import the Spring Boot project into Eclipse :

org.apache.maven.archiver.MavenArchiver.getManifest(org.apache.maven.project.MavenProject, org.apache.maven.archiver.MavenArchiveConfiguration)

add the following to the pom.xml :

<properties>            
      <maven-jar-plugin.version>2.6</maven-jar-plugin.version>
</properties>

Spring project without Spring Boot :

In pom.xml, we need to add spring related dependencies.

 <properties>
     <springframework.version>4.3.6.RELEASE</springframework.version>
 </properties>
 
 <dependencies>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-core</artifactId>
       <version>${springframework.version}</version>
    </dependency>
    <dependency>
       <groupId>org.springframework</groupId>
       <artifactId>spring-context</artifactId>
       <version>${springframework.version}</version>
    </dependency>
 </dependencies>

@Configuration annotation is a core Spring annotation. It is a core entry point to configuring Spring-based application using Java config instead of XML config.

Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions.

@Configuration is used when ApplicationContext has been initialized and bean registration.

When we are using Spring Boot, the application class is defined as follows :

@SpringBootApplication
public class SpringBootProjectApplication {
     .....
}

However, when we are not using Spring Boot, we need to make the class use @Configuration annotation to indicate that this class provides the Bean configuration.

Also, @SpringBootApplication annotation, internally does component scan. When we are not using Spring Boot, we need to do Component scan manually. So, add @ComponentScan annotation.

@Configuration
@ComponentScan("org.rdayala.basics.spring")
public class SpringBasicsProjectApplication {
    ......
}

When we are using Spring Boot, the ApplicationContext is created by SpringApplication :

ApplicationContext applicationContext = SpringApplication.run(SpringBootProjectApplication.class, args);

However, when we are not using Spring Boot, we need to use AnnotationConfigApplicationContext class to get ApplicationContext.

AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringBasicsProjectApplication.class);
....
applicationContext.close();