Spring Auto-wiring Java configuration demo

Project Snapshot

upload(19)

Programs


package com.wordpress.sreeharshasite.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
@Configuration
public class ApplicationConfigurationAdapter
{
@Bean (name="myController")
public MyController myController()
{
return new MyController();
}
@Bean (name="employeeServiceImpl")
public EmployeeServiceImpl employeeServiceImpl()
{
return new EmployeeServiceImpl();
}
}


package com.wordpress.sreeharshasite.controller;
import com.wordpress.sreeharshasite.services.EmployeeServiceImpl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
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.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.test;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import com.wordpress.sreeharshasite.configuration.ApplicationConfigurationAdapter;
import com.wordpress.sreeharshasite.controller.MyController;
import com.wordpress.sreeharshasite.model.Employee;
public class DIDemo {
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(ApplicationConfigurationAdapter.class);
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();
}
}
}


<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>
</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>
</project>

Output

Jul 21, 2017 6:56:11 AM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5e9f23b4: startup date [Fri Jul 21 06:56:11 IST 2017]; root of context hierarchy
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: