Wednesday, 17 July 2019

Getting started in to Spring Framework

Spring Framework Overview.

Spring Framework makes developing of enterprise java applications easier. It supports groovy,kotlin as alternate apart from java.Spring actually means "Different things in different context". It is divided in to modules and we can utilize the modules based on our needs to the application. Spring Framework also provides the foundation support for different application architectures including messaging, transaction data and persistence and web.

IOC Container (Inversion of Control)

IOC is also known as dependency Injection(DI).It is a process where by objects define their dependencies only through the Constructor arguments,arguments to a factory method, or properties that are set on the object instance after it is constructed or returned from a factory method.The container then injects those dependencies when it creates the bean.

Bean.

The Objects that form the backbone of your application and that are managed by the spring IOC container are called beans.A bean is an object that is instantiated assembled and otherwise managed by a spring IOC Container.

Application context.

Application context interface represents the spring IOC container and is responsible for instantiating configuring and assembling the beans.The Container gets its instructions on what objects to instantiate configure and assemble by reading the configuration meta data.Several implementations of the ApplicationContext interface are supplied out-of-thebox with Spring. In standalone
applications it is common to create an instance of ClassPathXmlApplicationContext or FileSystemXmlApplicationContext

This Configuration metadata is represented in XML,Java annotations or the Java Code.

Here we can see how to create the application.

Create a New Maven Project and select the option to skip the architypes. If we are creating the Maven Project then we should be knowing of the below terms.

GroupId.

uniquely identifies your project across all projects. A group ID should follow Java's package name rules. This means it starts with a reversed domain name you control. For example,

com.searchendeca

A good way to determine the groupId is to use project structure thst is if the current project has a multiple module project, it should append a new identifier to the parents.

eg: com.searchendeca.plugin
    com.searchendeca.reporting.
Artifact Id.

It is the name of the jar without version. If you created it then you can choose whatever name you want with the lower case letters
and no strange symbols.

Eg: maven,commons-math

Version.

If you distribute then you can choose any typical version with number and dots.

Step 1.Based on the above information create the project. Add the Following the POM.xml

 <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<spring.version>3.2.3.RELEASE</spring.version>
</properties>
Once you add the below then the maven will download the required jars and put in the lib.

Step 2.To create the Configuration file in the path. /src/main/resources named applicationContext.xml

with the following contents.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">


<bean id="helloWorldService"
class="com.searchendeca.application.HelloWorld">
<property name="Message" value="Welcome to Search Endeca" />
</bean>
</beans>

Step 3.Next Create a bean.

package com.searchendeca.application;

public class HelloWorld {

private String mMessage;

public String getMessage() {
return mMessage;
}

public void setMessage(String pMessage) {
this.mMessage = pMessage;
}
}

Step 4.Create a Main class.

package com.searchendeca.application;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class Hello {
@SuppressWarnings("resource")
public static void main(String[] args) {
// loading the definitions from the given XML file
//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/resources/applicationContext.xml");
HelloWorld service = (HelloWorld) context.getBean("helloWorldService");
String message = service.getMessage();
System.out.println(message);
//set a new name
service.setMessage("Spring");
message = service.getMessage();
System.out.println(message);
}
}

//ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

Look at the ApplicationContext created using the ClassPathXMlApplication context the other way to remember or
create the context is like below using the FileSystemXmlApplicationContext.

//ApplicationContext context = new FileSystemXmlApplicationContext("/src/main/resources/applicationContext.xml");

After creating the application run as the java application.

It will print the following output.

Output:

Welcome to Search Endeca
Spring

Happy Learning !!!

No comments:
Write comments