0% found this document useful (0 votes)
10 views10 pages

50 Siddhesh Umate Advance Java Lab

The document provides a practical guide for implementing dependency injection in a Spring application using setter methods, featuring an Employee and Address class. It includes the necessary Maven configuration, Java classes, and XML configuration for Spring context. Additionally, it demonstrates Spring AOP with various advice types for an EmployeeService class, showcasing method execution and exception handling.

Uploaded by

sidumate11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

50 Siddhesh Umate Advance Java Lab

The document provides a practical guide for implementing dependency injection in a Spring application using setter methods, featuring an Employee and Address class. It includes the necessary Maven configuration, Java classes, and XML configuration for Spring context. Additionally, it demonstrates Spring AOP with various advice types for an EmployeeService class, showcasing method execution and exception handling.

Uploaded by

sidumate11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Advanced java Lab Practical.

1. Write a Spring program to demonstrate dependency injection


using setter methods for injecting a dependent object. Consider
two objects—Employee class object emp1 and Address class
object add1. Here, add1 object is injected into emp1 object.

Input:-

pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<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.example</groupId>

<artifactId>SpringDIExample</artifactId>

<version>1.0-SNAPSHOT</version>

<packaging>jar</packaging>

<name>Spring Dependency Injection Example</name>

<url>http://maven.apache.org</url>

<dependencies>

<!-- Spring Core and Spring Context (for Dependency Injection) -->

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>
<version>5.3.24</version> <!-- Use the latest stable version -->

</dependency>

<!-- SLF4J API (for logging) -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-api</artifactId>

<version>1.7.32</version> <!-- Latest stable version -->

</dependency>

<!-- SLF4J Simple (for logging implementation) -->

<dependency>

<groupId>org.slf4j</groupId>

<artifactId>slf4j-simple</artifactId>

<version>1.7.32</version> <!-- Latest stable version -->

</dependency>

<!-- JUnit (for unit testing, optional) -->

<dependency>

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>4.13.2</version> <!-- Latest version -->

<scope>test</scope>

</dependency>

<dependency>

<groupId>com.springAopPract1</groupId>

<artifactId>beforeAdvice</artifactId>

<version>0.0.1-SNAPSHOT</version>

</dependency>

</dependencies>
<build>

<plugins>

<!-- Maven Compiler Plugin for Java compilation -->

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.8.1</version>

<configuration>

<source>1.8</source> <!-- Use Java 8 -->

<target>1.8</target> <!-- Use Java 8 -->

</configuration>

</plugin>

</plugins>

</build>

<repositories>

<repository>

<id>central</id>

<url>https://repo.maven.apache.org/maven2</url>

</rep

Address.java

public class Address {

private String street;

private String city;

private String state;

private String zip;

// Getters and Setters

public String getStreet() {

return street;

}
public void setStreet(String street) {

this.street = street;

public String getCity() {

return city;

public void setCity(String city) {

this.city = city;

public String getState() {

return state;

public void setState(String state) {

this.state = state;

public String getZip() {

return zip;

public void setZip(String zip) {

this.zip = zip;

@Override

public String toString() {


return "Address [street=" + street + ", city=" + city + ", state=" + state + ", zip=" + zip + "]";

Emolyee.java

package com.example;

public class Employee {

private String name;

private Address address;

public void setName(String name) {

this.name = name;

public void setAddress(Address address) {

this.address = address;

public void showEmployeeDetails() {

System.out.println("Employee: " + name);

if (address != null) {

address.showAddress();

} else {

System.out.println("No address available.");

Main.java

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {

public static void main(String[] args) {

// Load the Spring context from the beans.xml configuration

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

// Retrieve the Employee bean from the context

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

// Display employee information

employee.displayEmployeeInfo();

ApplicationContext.xml
<?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-4.0.xsd">

<!-- Define the Address bean -->

<bean id="address" class="com.example.Address">

<property name="street" value="1234 Elm Street" />

<property name="city" value="Somewhere" />

<property name="zipCode" value="12345" />

</bean>

<!-- Define the Employee bean and inject Address bean via setter -->

<bean id="employee" class="com.example.Employee">

<property name="name" value="John Doe" />

<property name="address" ref="address" />

</bean>
</beans>

Output:-

2.Build Spring application to demonstrate spring AOP – Before, after-running, around an d after-throwing
adivces.

EmployeeServices.java
package com.example;

public class EmployeeService {

public void addEmployee(String name) {

System.out.println("Adding employee: " + name);

public String getEmployee(String name) {

System.out.println("Getting employee details for: " + name);

if ("error".equals(name)) {

throw new RuntimeException("Error occurred while retrieving employee details");

return "Employee: " + name;

public void updateEmployee(String name) {


System.out.println("Updating employee: " + name);

public void deleteEmployee(String name) {

System.out.println("Deleting employee: " + name);

EmployeeServiceAspects.java

package com.example;

import org.aspectj.lang.annotation.After;

import org.aspectj.lang.annotation.AfterReturning;

import org.aspectj.lang.annotation.Around;

import org.aspectj.lang.annotation.Before;

import org.aspectj.lang.annotation.AfterThrowing;

import org.aspectj.lang.annotation.Aspect;

@Aspect

public class EmployeeServiceAspect {

@Before("execution(* com.example.EmployeeService.*(..))")

public void beforeMethod() {

System.out.println("Before method execution");

@After("execution(* com.example.EmployeeService.*(..))")

public void afterMethod() {

System.out.println("After method execution (Regardless of success or failure)");

}
@AfterReturning(pointcut = "execution(* com.example.EmployeeService.getEmployee(..))", returning =
"result")

public void afterReturning(Object result) {

System.out.println("After method execution (Success), result: " + result);

@AfterThrowing(pointcut = "execution(* com.example.EmployeeService.getEmployee(..))", throwing =


"error")

public void afterThrowing(Throwable error) {

System.out.println("Method threw an exception: " + error.getMessage());

}
App.java

package com.example;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

try {

EmployeeService employeeService = (EmployeeService) context.getBean("employeeService");

employeeService.addEmployee("Siddhesh Umate");

System.out.println(employeeService.getEmployee("Siddhesh Umate"));

employeeService.updateEmployee("Siddhesh Umate");

employeeService.deleteEmployee("Siddhesh Umate");

try {
employeeService.getEmployee("error");

} catch (RuntimeException e) {

System.out.println("Caught exception: " + e.getMessage());

} finally {

if (context instanceof ClassPathXmlApplicationContext) {

((ClassPathXmlApplicationContext) context).close();

OUTPUT:-

You might also like