top of page
Writer's pictureMIIT Training and Placements

Spring simple helloworld example with three different ways(XML, Annotation and XML + Annotations)

Updated: Aug 29, 2023

This blog is very useful for the novice to Spring Framework. Before we jump to the code we need to understand few concepts:

Spring Bean : Spring bean is nothing but simple POJO class, which object life cycle is managed by Spring framework

DI : Stands for Dependency Injection

IOC : Inversion of control, rather than you create the object and manage the dependency in code as developer we tell framework to do so for us, so ultimately we are giving the control back to framework.


Spring framework is nothing but IOC based DI and lightweight framework


Spring Scope:

Singleton : by default spring will create single object of any given bean, it is very important top understand the singleton design pattern for that please read our blogs for more details

Prototype : Framework will create new object each time when you ask

request : For each request it will be creating a new object

Session: Every new session there will be a new object

Global session : This scope mainly associated with Portlet.


Spring Wiring:

There are different ways spring will resolve the dependency as we all are familiar with @Autowired annotations. By Name, By Type, Constructor and Autodetect


Let us look at the different ways to create Spring hello world example.


Note: Source code is attached for more details:

1) With XML ways


beans.xml as below


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:flow="http://www.springframework.org/schema/webflow-config"

xmlns:util="http://www.springframework.org/schema/util"

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

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/webflow-config

http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<bean id="helloWorld" class="com.miit.springdemo.HelloWorld" init-method="" /></beans>



HelloWorld.java

package com.miit.springdemo;


import org.springframework.stereotype.Component;


public class HelloWorld {

public void sayHello() {

System.out.println("Hello Spring!!");

}

}


AppHelloWorld -- Main


package com.miit.springdemo;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppHelloWorld {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

HelloWorld helloWorldBean = context.getBean("helloWorld", HelloWorld.class);

helloWorldBean.sayHello();

HelloWorld helloWorldBean1 = context.getBean("helloWorld", HelloWorld.class);

if(helloWorldBean == helloWorldBean1) {

System.out.println("both are equals::");

}else {

System.out.println("both are not equals::");

}

}

}




2) With XML + Annotations ways


beans.xml as below


<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:flow="http://www.springframework.org/schema/webflow-config"

xmlns:util="http://www.springframework.org/schema/util"

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

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

http://www.springframework.org/schema/webflow-config

http://www.springframework.org/schema/webflow-config/spring-webflow-config-2.0.xsd

http://www.springframework.org/schema/util

http://www.springframework.org/schema/util/spring-util-2.0.xsd">

<context:annotation-config base-package="com.miit" />

<context:component-scan base-package="com.miit" />

</beans>



HelloWorld.java

package com.miit.springdemo;


import org.springframework.stereotype.Component;

@Component

public class HelloWorld {

public void sayHello() {

System.out.println("Hello Spring!!");

}

}


AppHelloWorld -- Main


package com.miit.springdemo;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppHelloWorld {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

HelloWorld helloWorldBean = context.getBean("helloWorld", HelloWorld.class);

helloWorldBean.sayHello();

HelloWorld helloWorldBean1 = context.getBean("helloWorld", HelloWorld.class);

if(helloWorldBean == helloWorldBean1) {

System.out.println("both are equals::");

}else {

System.out.println("both are not equals::");

}

}

}



3) No XML - Only Annotations


AppConfig.java


@Configuration

public class AppConfig {


@Bean(name = "myBean")

public Toyota getHelloWorld() {

return new Toyota(new Engine());

}


}



package com.miit.springdemo;


import org.springframework.stereotype.Component;


public class HelloWorld {

public void sayHello() {

System.out.println("Hello Spring!!");

}

}



package com.miit.springdemo;


import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


public class AppHelloWorld {

public static void main(String[] args) {

ApplicationContext context = new AnnotationConfigApplicationContext(AppConfigHelloWorld.class);


HelloWorld helloWorldBean = context.getBean("helloWorld", HelloWorld.class);

helloWorldBean.sayHello();

HelloWorld helloWorldBean1 = context.getBean("helloWorld", HelloWorld.class);

if(helloWorldBean == helloWorldBean1) {

System.out.println("both are equals::");

}else {

System.out.println("both are not equals::");

}

}

}
























585 views0 comments

Recent Posts

See All

Battle of the Backends: Java vs Node.js

Comparing Java and Node.js involves contrasting two distinct platforms commonly used in backend development. Here’s a breakdown of their...

Comments


bottom of page