spring hello world

In this post I am gonna start a new maven project and then I will springify it(making it as spring project) by adding required jars to maven pom.xml. Create a new maven project and start with maven quick start and then add group id and Artifact id(its the project name).

giving group id and artifact idmaven quick start new maven project  so, we created a new project springhelloworld.

pom.xml


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.sourceforopensource.springcore</groupId>
 <artifactId>springhelloworld</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <dependencies>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.2.3.RELEASE</version>
 </dependency>
 </dependencies>
</project>
Person.java
package com.sourceforopensource.springcore;

public class Person {
public void speak() {
System.out.println("Hello! I'm a person.");
}
}
beans.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.xsd">


 <bean id="person"
 class="com.sourceforopensource.springcore.Person">
 </bean>
</beans>
App.java
package com.sourceforopensource.springcore;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;&amp;lt;/p&amp;gt;
public class App {
public static void main(String[] args) {
ApplicationContext context = new FileSystemXmlApplicationContext("beans.xml");

Person person = (Person)context.getBean("person");
person.speak();
}

}

output: Hello! I’m a person.

what we have done is, we created a new project and then created pom.xml which it contains all spring related libraries to getting started with spring, and then create a simple pojo Person.java and then to make a spring project we need an beans.xml file which is read by spring beans container. At last we created App.java what happens in this file is we are trying to call person pojo and its methods but you can see we are not created any object for that(creating objects for pojo or instantiating the pojos is seen by beans container which in case it is ApplicationContext interface.

And also, we can load beans.xml from classpath by using ClassPathXmlApplicationContext interface.

App.java


package com.sourceforopensource.springcore;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("com/sourceforopensource.springcore/spring/test/beans/beans.xml");

Person person = (Person)context.getBean("person");
person.speak();

((ClassPathXmlApplicationContext)context).close();
}
}

output: Hello! I’m a person.

Balakrishna Pendyala

Author: Balakrishna Pendyala

Balakrishna is a Software Engineer working in Media Streaming Domain, and also worked on Banking and Health Care domains. His areas of interest include Object Oriented Design, SOLID Design principles, Restful Web Services and Open Source softwares including Spring, Spring Boot, and Hibernate.

If you found an error, highlight it and press Shift + Enter or click here to inform us.

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *