50 Siddhesh Umate Advance Java Lab
50 Siddhesh Umate Advance Java Lab
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>
<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>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.springAopPract1</groupId>
<artifactId>beforeAdvice</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>central</id>
<url>https://repo.maven.apache.org/maven2</url>
</rep
Address.java
return street;
}
public void setStreet(String street) {
this.street = street;
return city;
this.city = city;
return state;
this.state = state;
return zip;
this.zip = zip;
@Override
Emolyee.java
package com.example;
this.name = name;
this.address = address;
if (address != null) {
address.showAddress();
} else {
Main.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
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">
</bean>
<!-- Define the Employee bean and inject Address bean via setter -->
</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;
if ("error".equals(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
@Before("execution(* com.example.EmployeeService.*(..))")
@After("execution(* com.example.EmployeeService.*(..))")
}
@AfterReturning(pointcut = "execution(* com.example.EmployeeService.getEmployee(..))", returning =
"result")
}
App.java
package com.example;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
try {
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) {
} finally {
((ClassPathXmlApplicationContext) context).close();
OUTPUT:-