Ads Here

Saturday, June 4, 2022

Spring Core, Maven/Autowiring

 ApplicationConfig.java 

package com.spring.app;


import java.util.Calendar;

import java.util.Date;


import org.springframework.context.annotation.Configuration;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Bean;


@Configuration

@ComponentScan

public class ApplicationConfig {


@Bean

public Employee employee() {

// Creating an 'Employee' bean

Employee employee = new Employee();


// Setter-based injection:-


// Similar to <property name="empId" value="1234" />

employee.setEmpId(1234);


// Similar to <property name="empName" value="Abhishek" />

employee.setEmpName("Abhishek");


// The below statement isn't required due to 'autowiring'

// employee.setPassObj(passObj());


return employee;

}


@Bean

public Passport passObj() {


// Creating a 'Passport' bean

Passport passport = new Passport();


Date todayDate = new Date();


// Using 'Calendar' class to add 84 days to todayDate (expiryDate)

Calendar cal = Calendar.getInstance();

cal.setTime(todayDate);

cal.add(Calendar.DATE, 84);


Date expiryDate = cal.getTime();


// Similar to <property name="passNum" value="9876" />

passport.setPassNum(9876);


// The below 2 setters cannot be used in an xml config file

// as they are dynamic values

passport.setDateOfIssue(todayDate);

passport.setDateOfExpiry(expiryDate);


return passport;

}


}

Driver.java 

package com.spring.app;


import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;


public class Driver {

public static void main(String[] args) {

ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(ApplicationConfig.class);


Employee employee = (Employee) context.getBean(Employee.class);


System.out.println("EmpID : " + employee.getEmpId());

System.out.println("EmpName : " + employee.getEmpName());

System.out.println(employee.getPassObj().toString());

context.close();

}


}

Employee.java 

package com.spring.app;


import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Component;


@Component

public class Employee {

private int empId;

private String empName;


@Autowired

private Passport passObj;


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 Passport getPassObj() {

return passObj;

}


public void setPassObj(Passport passObj) {

this.passObj = passObj;

}


}


Passport.java 

package com.spring.app;


import java.text.SimpleDateFormat;

import java.util.Date;

import org.springframework.stereotype.Component;


@Component

public class Passport {

private int passNum;

private Date dateOfIssue;

private Date dateOfExpiry;


public int getPassNum() {

return passNum;

}


public void setPassNum(int passNum) {

this.passNum = passNum;

}


public Date getDateOfIssue() {

return dateOfIssue;

}


public void setDateOfIssue(Date dateOfIssue) {

this.dateOfIssue = dateOfIssue;

}


public Date getDateOfExpiry() {

return dateOfExpiry;

}


public void setDateOfExpiry(Date dateOfExpiry) {

this.dateOfExpiry = dateOfExpiry;

}


@Override

public String toString() {

// Defining the format in which the dates should be printed

SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");


return "Passport [passNum=" + passNum + ", dateOfIssue=" + sdf.format(dateOfIssue) + ", dateOfExpiry="

+ sdf.format(dateOfExpiry) + "]";

}


}


No comments:

Post a Comment