Spring – Dependency Injection Auto Wiring Demo

Project Snapshot

upload(18)

Program


package com.wordpress.sreeharshasite.model;
public class Employee {
private int empId;
private String empName;
private String city;
private String designation;
public Employee()
{
}
public Employee(int empId,String empName,String city,String designation)
{
this.empId=empId;
this.empName=empName;
this.city=city;
this.designation=designation;
}
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getDesignation() {
return designation;
}
public void setDesignation(String designation) {
this.designation = designation;
}
}


package com.wordpress.sreeharshasite.services;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public interface EmployeeService {
List<Employee> listAllEmployees();
}


package com.wordpress.sreeharshasite.services;
import java.util.ArrayList;
import java.util.List;
import com.wordpress.sreeharshasite.model.Employee;
public class EmployeeServiceImpl implements EmployeeService {
public List<Employee> listAllEmployees() {
Employee emp1=new Employee(100,"John","Delhi","Associate");
Employee emp2=new Employee(101,"Ravi","Hyderabad","SeniorAssociate");
Employee emp3=new Employee(102,"Rajesh","Delhi","JuniorAssociate");
Employee emp4=new Employee(103,"Ramesh","Delhi","AssociateDirector");
List<Employee> employeeList=new ArrayList<Employee>();
employeeList.add(emp1);
employeeList.add(emp2);
employeeList.add(emp3);
employeeList.add(emp4);
return employeeList;
}
}


package com.wordpress.sreeharshasite.controller;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
import java.util.List;
import com.wordpress.sreeharshasite.model.*;
public class MyController {
@Autowired
private EmployeeServiceImpl employeeServiceImpl;
public EmployeeServiceImpl getEmployeeServiceImpl() {
return employeeServiceImpl;
}
public void setEmployeeServiceImpl(EmployeeServiceImpl employeeServiceImpl) {
this.employeeServiceImpl = employeeServiceImpl;
}
public List<Employee> listAllEmployees()
{
return employeeServiceImpl.listAllEmployees();
}
}


package com.wordpress.sreeharshasite.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.model.Employee;
public class DIDemo {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);
MyController controller = (MyController) ctx.getBean("myController");
List<Employee> employees = controller.getEmployeeServiceImpl().listAllEmployees();
for(Employee employee : employees){
System.out.println("Employee Details");
System.out.print(employee.getEmpId()+" ");
System.out.print(employee.getEmpName()+" ");
System.out.print(employee.getCity()+" ");
System.out.print(employee.getDesignation()+" ");
System.out.println();
}
}
}


<?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-2.5.xsd">
<bean id="myController" class="com.wordpress.sreeharshasite.controller.MyController" autowire="byName"/>
<bean id="employeeServiceImpl" class="com.wordpress.sreeharshasite.services.EmployeeServiceImpl"/>
</beans>


<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.wordpress.sreeharshasite.spring</groupId>
<artifactId>SpringDIDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringDIDemo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.5.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

Output

Jul 20, 2017 5:02:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@300ffa5d: startup date [Thu Jul 20 17:02:41 IST 2017]; root of context hierarchy
Jul 20, 2017 5:02:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Employee Details
100 John Delhi Associate 
Employee Details
101 Ravi Hyderabad SeniorAssociate 
Employee Details
102 Rajesh Delhi JuniorAssociate 
Employee Details
103 Ramesh Delhi AssociateDirector
Advertisements

I am Sreeharsha from Bengaluru. I am working in java for the past 6+ years. In this blog i will be sharing posts regarding java , advanced java , monthly challenges, collection of quotes , my themes and many more. If you like my posts please follow my blog.

Tagged with: , ,
Posted in Frameworks, Spring

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow My Diary on WordPress.com
Archives

Enter your email address to follow this blog and receive notifications of new posts by email.

%d bloggers like this: