Introduction
This article is showing how to create a simple Java Application using Spring Framework using Netbeans IDE. The process exist as an example in a device running using Microsoft Windows 10 operating system.
Creating a Simple Java Spring Application using Netbeans IDE
There are several parts of the process for creating that simple Java Spring Application. The first part is just creating a simple Java Application. Following after is the continuation of it. It is focusing on configuring the Java Application for providing Spring support.
Creating a Simple Java Application using Netbeans IDE
So, in order to create a simple Java Spring Application using NetBeans IDE, the following are the steps to follow :
-
Just access File > New Project. The following window dialog will appear :
Just select Maven for the Categories and Java Application for the Projects. Don’t forget to click the Next button after.
-
After clicking the Next button, the following image will appear :
Fill the field of the Project Name in the above window dialog. For an example, just type ‘spring’. The content of the fields after will adjust the value accordingly. Next, just just click the Finish button as in the above window dialog to start creating the project.
-
The following display of the NetBeans IDE will appear :
The project with the name ‘spring’ exist and it appear in the NetBeans IDE as in the above image.
-
In order to check that the Java Application is working just right click at the project. Click the menu of Clean and Build to check that the build process is a success.
Configuring Simple Java Application with Spring Support using Netbeans IDE
Following after the previous part, just configure the Java Application to have Spring support. Just continue on from the previous part as follows :
-
Create a new class file. In this example, it will exist in ‘com.test’ package. Just right click at the project and choose New > Java Class….. Below is the appearance of the process :
-
After clicking the menu, the following window dialog will appear :
Just, fill the name in the Class Name field. In this context, it is ‘SpringMain’ for an example. After typing the name, just click Finish to create the Java file. The following display will appear :
The content of the file with the name of ‘SpringMain’ exist as follows :
-
After that, edit the file so that it will have a main method as follows :
public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); Employee employeeObject = (Employee) context.getBean("employeeBean"); employeeObject.identifyID(); }
Normally, the above source code will generate errors. The solution for solving the above errors is very simple. Just add the necessary package dependency in the pom.xml file definition. It is the solution since this Java Application is associated with maven build tool. The following is the additional definition :
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> </dependencies>
-
Do not forget to create another class for representing the Bean with the name of ‘Employee’. The following is the content of that file :
package com.test.spring; public class Employee { public void identifyID(){ System.out.println("I am an employee !"); } }
-
Last but not least, do not forget to define the spring configuration file in the project. For an example, in this context it is a file with the name of ‘spring-config.xml’. The following is the content of the file :
<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="employeeBean" class="com.test.spring.Employee"> </bean> </beans>
-
Finally, execute and run the project. If it is the first time, the following window dialog will appear :
-
If there are no further errors appear, the following output will appear :
So, the above is the end of the execution of the simple Java Spring application. It ends successfully printing the desired output of a printed string with the text of ‘I am an employee !’.
One thought on “How to Create a Simple Java Spring Application using NetBeans IDE”