Hello world project in Spring
This is My first post on Spring. Happy Learning !!
1.Download Eclipse from https://www.eclipse.org/downloads/2. Once you install the Eclipse navigate to Help->Eclipse Marketplace and download Spring tools
Spring uses IOC (Inversion of Control) container to create, initialize, manages objects and you don't need to create the java objects from the class which avoids tight coupling.
In this blog, you will learn about the different ways of creating the java objects through spring core container IOC.
- Create a java project
- Create a bean class Hello.java- Spring uses setter methods to initialize the objects
package com.practice;
public class Hello {
public Hello() {
System.out.println("Hello constructor");
}
public String message;
public void getMessage() {
System.out.println("Your Message"+ message);
}
public void setMessage(String message) {
this.message = message;
}
}
Create a spring bean configuration file - This file 'bean.xml' is being used by IOC during runtime to create, initialize and manages the bean.
Here Hello.java class is registered in the configuration file and we are using a property called name to pass the message value. IOC uses setter method to initialize the objects,
<?xml version = "1.0" encoding = "UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd ">
<bean id = "Hello" class = "com.practice.Hello">
<property name = "message" value = "Hello World!"/>
</bean>
</beans>
Create a test class to test the IOC behavior.
There are two ways to use the spring container to create the objects/
1.ClassPathXmlApplicationContext which is extended from BeanFactory and it Creates the object without being called.
2. ClassPathResource, which uses BeanFactory. this will create the bean objects only when it is called.
package com.practice;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class AOPTest {
public static void main(String[] args){
/*ApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
Hello obj=(Hello) context.getBean("Hello");
obj.getMessage();*/
Resource resource =new ClassPathResource("bean.xml");
BeanFactory factory=new XmlBeanFactory(resource);
Hello obj=(Hello)factory.getBean("Hello");
obj.getMessage();
}
}
Run the project to test the application
You can download the project from
https://github.com/gobig89/HelloSpringProject.git
Comments
Post a Comment