Client/Standalone applications are possible with Spring framework and maven build tool. After this article you are getting knowledge about, how to integrate Spring IOC module with maven and use of Spring IOC framework in standalone applications.
If you are new for Maven build tool, please refer my previous article and create a java standalone project.
Step-1: Creating my-beans.xml file in src/main/resources directory.
Step-2: Creating Bean class in com.gurugubellitech.bean directory.
Step-3: Creating TestApp class in com.gurugubellitech.client directory.
Step-4: Adding required maven dependencies in pom.xml.
Step-5: Output of the Application.
If you are new for Maven build tool, please refer my previous article and create a java standalone project.
Step-1: Creating my-beans.xml file in src/main/resources directory.
Step-2: Creating Bean class in com.gurugubellitech.bean directory.
Step-3: Creating TestApp class in com.gurugubellitech.client directory.
Step-4: Adding required maven dependencies in pom.xml.
Step-5: Output of the Application.
Step-1: Creating my-beans.xml file in src/main/resources directory:
my-beans.xml:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans | |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> | |
<bean class="com.gurugubellitech.bean.Employee" id="employee1"> | |
<property name="empNo" value="Emp1234"></property> | |
<property name="firstName" value="Gurugubelli"></property> | |
<property name="lastName" value="Technologies"></property> | |
<property name="deptNo" value="Dept01"></property> | |
<property name="deptName" value="Tech Developer"></property> | |
</bean> | |
<bean class="com.gurugubellitech.bean.Employee" id="employee2"> | |
<property name="empNo" value="Emp1235"></property> | |
<property name="firstName" value="Technologies"></property> | |
<property name="lastName" value="Gurugubelli"></property> | |
<property name="deptNo" value="Dept02"></property> | |
<property name="deptName" value="Tech Tester"></property> | |
</bean> | |
</beans> |
Step-2: Creating Bean class in com.gurugubellitech.bean directory:
Employee.java:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gurugubellitech.bean; | |
public class Employee { | |
private String empNo; | |
private String firstName; | |
private String lastName; | |
private String deptNo; | |
private String deptName; | |
/** | |
* @return the empNo | |
*/ | |
public String getEmpNo() { | |
return empNo; | |
} | |
/** | |
* @param empNo | |
* the empNo to set | |
*/ | |
public void setEmpNo(String empNo) { | |
this.empNo = empNo; | |
} | |
/** | |
* @return the firstName | |
*/ | |
public String getFirstName() { | |
return firstName; | |
} | |
/** | |
* @param firstName | |
* the firstName to set | |
*/ | |
public void setFirstName(String firstName) { | |
this.firstName = firstName; | |
} | |
/** | |
* @return the lastName | |
*/ | |
public String getLastName() { | |
return lastName; | |
} | |
/** | |
* @param lastName | |
* the lastName to set | |
*/ | |
public void setLastName(String lastName) { | |
this.lastName = lastName; | |
} | |
/** | |
* @return the deptNo | |
*/ | |
public String getDeptNo() { | |
return deptNo; | |
} | |
/** | |
* @param deptNo | |
* the deptNo to set | |
*/ | |
public void setDeptNo(String deptNo) { | |
this.deptNo = deptNo; | |
} | |
/** | |
* @return the deptName | |
*/ | |
public String getDeptName() { | |
return deptName; | |
} | |
/** | |
* @param deptName | |
* the deptName to set | |
*/ | |
public void setDeptName(String deptName) { | |
this.deptName = deptName; | |
} | |
} |
Step-3: Creating TestApp class in com.gurugubellitech.client directory:
TestApp.java:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gurugubellitech.client; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.support.ClassPathXmlApplicationContext; | |
import com.gurugubellitech.bean.Employee; | |
public class TestApp { | |
private static ApplicationContext context; | |
public static void main(String[] args) { | |
String configFile = "my-beans.xml"; | |
context = new ClassPathXmlApplicationContext(configFile); | |
Employee empDeveloper = context.getBean("employee1", Employee.class); | |
Employee empTester = context.getBean("employee2", Employee.class); | |
System.out.println("======= Tech Developer Details ======="); | |
System.out.println("Employee ID : " + empDeveloper.getEmpNo()); | |
System.out.println("First Name : " + empDeveloper.getFirstName()); | |
System.out.println("Last Name : " + empDeveloper.getLastName()); | |
System.out.println("Dept NO : " + empDeveloper.getDeptName()); | |
System.out.println("Dept Name : " + empDeveloper.getDeptName()); | |
System.out.println("\n\n======= Tech Tester Details ======="); | |
System.out.println("Employee ID : " + empTester.getEmpNo()); | |
System.out.println("First Name : " + empTester.getFirstName()); | |
System.out.println("Last Name : " + empTester.getLastName()); | |
System.out.println("Dept NO : " + empTester.getDeptNo()); | |
System.out.println("Dept Name : " + empTester.getDeptName()); | |
} | |
} |
Step-4: Adding required maven dependencies in pom.xml:
pom.xml:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<project xmlns="http://maven.apache.org/POM/4.0.0" xsi:schemalocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> | |
<modelversion>4.0.0</modelversion> | |
<groupid>com.gurugubellitech</groupid> | |
<artifactid>MavenClientApp</artifactid> | |
<version>0.0.1-SNAPSHOT</version> | |
<dependencies> | |
<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> | |
<dependency> | |
<groupid>org.springframework</groupid> | |
<artifactid>spring-beans</artifactid> | |
<version>5.0.2.RELEASE</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> | |
<dependency> | |
<groupid>org.springframework</groupid> | |
<artifactid>spring-core</artifactid> | |
<version>5.0.2.RELEASE</version> | |
</dependency> | |
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> | |
<dependency> | |
<groupid>org.springframework</groupid> | |
<artifactid>spring-context</artifactid> | |
<version>5.0.2.RELEASE</version> | |
</dependency> | |
</dependencies> | |
</project> |
Step-5: Output of the Application:
Employee ID : Emp1234
First Name : Gurugubelli
Last Name : Technologies
Dept NO : Tech Developer
Dept Name : Tech Developer
======= Tech Tester Details =======
Employee ID : Emp1235
First Name : Technologies
Last Name : Gurugubelli
Dept NO : Dept02
Dept Name : Tech Tester
Maven and Spring Client Application using setter injection
Reviewed by Gurugubelli Technologies
on
November 30, 2017
Rating:

Very Useful article, Thanks For Sharing With Us
ReplyDeleteDocker Online Training
Kubernetes Online Training
Thanks for sharing i would you like to more updates.
ReplyDeleteDevOps Online Training
After reading this web site I am very satisfied simply because this site is providing comprehensive knowledge for you to audience.Really very happy to say, your post is very interesting to read.
ReplyDeleteDevOps Training in Chennai
DevOps Online Training in Chennai
DevOps Training in Bangalore
DevOps Training in Hyderabad
DevOps Training in Coimbatore
DevOps Training
DevOps Online Training
Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge as updated, keep blogging. This article is very much helpful and i hope this will be an useful information for the needed one.Keep on updating these kinds of informative things.
ReplyDeleteoracle training in chennai
oracle training institute in chennai
oracle training in bangalore
oracle training in hyderabad
oracle training
oracle online training
hadoop training in chennai
hadoop training in bangalore