0% found this document useful (0 votes)
9 views4 pages

Java

The document is an assignment on implementing the MVC architecture using the Spring Framework, specifically focusing on Java Web Frameworks. It includes code for configuring a Spring application with data source, JDBC template, transaction management, and AOP logging. Additionally, it demonstrates a REST controller that returns a greeting message and tests the database connection using an EmployeeService class.

Uploaded by

Prachi Dhere
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)
9 views4 pages

Java

The document is an assignment on implementing the MVC architecture using the Spring Framework, specifically focusing on Java Web Frameworks. It includes code for configuring a Spring application with data source, JDBC template, transaction management, and AOP logging. Additionally, it demonstrates a REST controller that returns a greeting message and tests the database connection using an EmployeeService class.

Uploaded by

Prachi Dhere
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

Assignment No :8

Roll No :357
:392
Batch :E3
:E3
PRN No :202502040008
:202502040043
Title of the Java Web Frameworks:Spring
Assignment

Problem Statement 1 : Write a program to implement MVC using Spring Framework.

Code:-

package [Link];

import [Link].*;
import [Link];
import [Link];
import [Link].*;
import [Link];
import [Link].*;
import [Link].*;
import [Link].*;

import [Link];

@Configuration
@ComponentScan(basePackages = "[Link]")
@EnableAspectJAutoProxy
@EnableTransactionManagement
class AppConfig {

@Bean
public DataSource dataSource() {
DriverManagerDataSource ds = new DriverManagerDataSource();
[Link]("[Link]");
[Link]("jdbc:oracle:thin:@localhost:1521:xe");
[Link]("system");
[Link]("root");
return ds;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource ds) {
return new JdbcTemplate(ds);
}

// Transaction Manager (Important)


@Bean
public PlatformTransactionManager transactionManager(DataSource ds) {
return new [Link](ds);
}
}

@Aspect
@Component
class LoggingAspect {

// Runs before any method in EmployeeService


@Before("execution(* [Link].*(..))")
public void logBefore() {
[Link]("\n[AOP NOTIFICATION]: Logging service activity...");
}
}

@Service
class EmployeeService {

private final JdbcTemplate jdbcTemplate;

public EmployeeService(JdbcTemplate jdbcTemplate) {


[Link] = jdbcTemplate;
}

@Transactional
public void testDatabase() {

// Simple query to test DB connection


int count = [Link](
"SELECT count(*) FROM dual",
[Link]
);
[Link](
"Oracle Database Connection Successful! Count from dual: " + count
);
}
}

@RestController
@RequestMapping("/api")
class MyRestController {

@GetMapping("/greet")
public String sayHello() {
return "Hello Yogesh! Your Spring REST Service is working.";
}
}

public class SpringApp {

public static void main(String[] args) {

// Start Spring Container


AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext([Link]);

// Test AOP + JDBC


EmployeeService service = [Link]([Link]);
[Link]();

[Link]("\nSpring Framework Practical initialized successfully!");

[Link]();
}
}
Output:-

You might also like