CodesJava

Easy learning with example program codes

Spring hello world example in eclipse


Let us create by following below steps:

1. Download spring jar files and add into project class path. Or use the maven, see our Maven Eclipse Spring example.
2. Create java class (bean).
3. Create spring configuration file which contains all bean configurations.
4. Create spring test class.
5. Load spring configuration file into Resource Interface object.
Syntax: Resource resource = new ClassPathResource(“spring configuration file”);
6. Create BeanFactory (Spring IOC container) object by reading spring configuration file through resource object.
Syntax: BeanFactory beanFactory = new XmlBeanFactory(resource);
7. Get bean object from beanFactory (spring ioc container) by passing beanId.
TestBean testBean = beanFactory.getBean(“testBeanId″);

Note:

1. We can also use ApplicationContext object for getting bean object as:
Synyax:

ApplicationContext context =
new ClassPathXmlApplicationContext("spring configuration file");
TestBean testBean = (TestBean) context.getBean("testBeanId");

2. Spring IOC container is responsible for creating bean objects and dependency injection.
3. Spring uses singleton design pattern for beans, so every spring bean is singleton class by default.

Example explanation:

First create a java bean class named “HelloWorld.java” which have a propoerty userName and a method sayHello. Next create a spring configuration file which defines the bean and its properties. Here we define HelloWorld.java bean with id helloWorld. This id will be used later to get the bean object from ApplicationContext object. Next create the Test.java class as a main program. Framework creates the ApplicationContext object by using spring configuration file. Then get the HelloWorld bean object from ApplicationContext by using getBean() method. We have to pass beanId in the getBean() method. At the time of object creation IoC container sets the bean propoerties value defined in spring configuration file i.e. userName property value will be set to jai.

Example:

HelloWorld.java

package com.codesjava.business;
 
/**
 * This class will be used as a bean class.
 * @author codesjava
 */
public class HelloWorld {
	private String userName;	
 
	public void setUserName(String userName) {
		this.userName = userName;
	}
 
	public void sayHello(){
		System.out.println("Hello: " + userName);
	}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 
   <bean id="helloWorld" class="com.codesjava.business.HelloWorld">
       <property name="userName" value="jai"/>
   </bean>
 
</beans>

Test.java

package com.codesjava.business;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class Test {
 public static void main(String[] args) {
  //Get ApplicationContext using spring configuration file.
  ApplicationContext context = 
	new ClassPathXmlApplicationContext("applicationContext.xml");
 
  //Get HelloWorld bean object from ApplicationContext instance. 
  HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
 
  //Call sayHello method of HelloWorld bean.
  helloWorld.sayHello();
 } 
}

Output:

Hello: jai

 
Download this example.
 

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap